This commit is contained in:
SoSIE 2021-08-13 08:59:05 +02:00
parent 649dfe25dc
commit a351cfd60f

View File

@ -1,59 +1,49 @@
# coding:utf8 # coding:utf8
#
# ass2messages.py, dictionary to subtitle exporter to automate translation # ass2messages.py, dictionary to subtitle exporter to automate translation
# author sosie-js # version 2.0
# require my pythonfx mofied version adding fixes and del_line facility # author sosie-js - sos-productions.com
#========================================== #==========================================
# For relative imports to work # For relative imports to work, to use my pythonfx mofied version adding fixes and del_line facility
import os, sys; sys.path.append(os.path.dirname(os.path.realpath(__file__))) import os, sys; sys.path.append(os.path.dirname(os.path.realpath(__file__)))
from pyonfx import * from pyonfx import *
# will include message_en.py a dictiatary of English messages for the syncplay gui #message_en.py hold the original English messages we translte from for the syncplay gui
# https://raw.githubusercontent.com/Syncplay/syncplay/master/syncplay/messages_en.py # https://raw.githubusercontent.com/Syncplay/syncplay/master/syncplay/messages_en.py
# this will serves aslo as a template to generate dict_message_file
import messages_en import messages_en
messages=messages_en.en
#here is the only param for this script, the target language, it requires the ass file ass_message_file, holding the translated messages
# we use messages2ass.py on messages_en.py to generate messages_en.ass and then we fastrad it with google translate/deepl ...
#to have it in the targetted language
lang="fr" lang="fr"
#----------------------------------------------------------
dict_message_file="messages_"+lang dict_message_file="messages_"+lang
ass_message_file=dict_message_file+".ass" ass_message_file=dict_message_file+".ass"
dict_message_file=dict_message_file+".py" dict_message_file=dict_message_file+".py"
print("Welcome on the ass file %s to %s dictionary to exporter" % (dict_message_file,ass_message_file) ) print("Welcome on the ass file %s to %s dictionary to exporter" % (dict_message_file,ass_message_file) )
print("-------------------------------------------------------------------------------------") print("-------------------------------------------------------------------------------------")
#Generate translations dictionary from the translated messages provided by ass_message_file
io = Ass(ass_message_file) io = Ass(ass_message_file)
meta, styles, lines = io.get_data() meta, styles, lines = io.get_data()
#messages will hold the message dict
messages=messages_en.en
i=len(lines)
pos_time=0
dict={} dict={}
#the fist line of Untitled.ass, empty, will serve as template
for line in lines: for line in lines:
dict[str(line.effect)]=str(line.raw_text) dict[str(line.effect)]=str(line.raw_text)
script_dir=os.path.dirname(os.path.realpath(__file__)) script_dir=os.path.dirname(os.path.realpath(__file__))
path_input=os.path.join(script_dir,"messages_en.py") path_input=os.path.join(script_dir,"messages_en.py")
input = open(path_input, "r", encoding="utf-8-sig") input = open(path_input, "r", encoding="utf-8-sig")
template=input.read() template=input.read()
input.close() input.close()
note='# This file was mainly auto generated from ass2messages.py applied on '+ass_message_file+' to get these messages\n'
note+='# its format has been harmonized, values are always stored in doublequotes strings, \n'
note+='# if double quoted string in the value then they should be esacaped like this \\". There is\n'
note+='# thus no reason to have single quoted strings. Tabs \\t and newlines \\n need also to be escaped.\n'
note+='# whith ass2messages.py which handles these issues, this is no more a nightmare to handle. \n'
note+='# I fixed partially messages_en.py serving as template. an entry should be added in messages.py:\n'
note+='# "'+lang+'": messages_'+lang+'.'+lang+', . Produced by sosie - sos-productions.com\n\n'
template=template.replace('en = {',note+lang+' = {')
#Normalize space (and quotes!), launch the cleaner machine #Normalize space (and quotes!), launch the cleaner machine
template=template.replace('": "','": "') template=template.replace('": "','": "')
template=template.replace('": \'','": \'') template=template.replace('": \'','": \'')
@ -66,8 +56,25 @@ def escapeDoublequotes(s):
s = s.replace('"', '\\"') s = s.replace('"', '\\"')
return s return s
def decodeHTMLEntities(s):
s = s.replace(">", ">")
s = s.replace("&","&")
return s
#Now fill the template
note='# This file was mainly auto generated from ass2messages.py applied on '+ass_message_file+' to get these messages\n'
note+='# its format has been harmonized, values are always stored in doublequotes strings, \n'
note+='# if double quoted string in the value then they should be esacaped like this \\". There is\n'
note+='# thus no reason to have single quoted strings. Tabs \\t and newlines \\n need also to be escaped.\n'
note+='# whith ass2messages.py which handles these issues, this is no more a nightmare to handle. \n'
note+='# I fixed partially messages_en.py serving as template. an entry should be added in messages.py:\n'
note+='# "'+lang+'": messages_'+lang+'.'+lang+', . Produced by sosie - sos-productions.com\n\n'
template=template.replace('en = {',note+lang+' = {')
for key, value in messages.items(): for key, value in messages.items():
value=value.replace("&","&") value=decodeHTMLEntities(value)
if(key == "LANGUAGE"): if(key == "LANGUAGE"):
language=value language=value
source=('"%s": "%s"' % (key, escapeBackslashAndTabs(value))) source=('"%s": "%s"' % (key, escapeBackslashAndTabs(value)))
@ -77,8 +84,11 @@ for key, value in messages.items():
source=('"%s": \'%s\'' % (key, escapeBackslashAndTabs(value))) source=('"%s": \'%s\'' % (key, escapeBackslashAndTabs(value)))
target=('"%s": "%s"' % (key,escapeDoublequotes(dict[key]))) target=('"%s": "%s"' % (key,escapeDoublequotes(dict[key])))
template=template.replace(source,target) template=template.replace(source,target)
#this line does nothing, I don't know why :(
template=template.replace('English dictionary',language+' dictionary') template=template.replace('English dictionary',language+' dictionary')
# Save it as .ass file
path_output=os.path.join(script_dir,dict_message_file) path_output=os.path.join(script_dir,dict_message_file)
with open(path_output,"w") as f: with open(path_output,"w") as f:
f.write(template) f.write(template)