i18n.py 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427
  1. #!/usr/bin/env python3
  2. # -*- coding: utf-8 -*-
  3. #
  4. # Script to generate the template file and update the translation files.
  5. # Copy the script into the mod or modpack root folder and run it there.
  6. #
  7. # Copyright (C) 2019 Joachim Stolberg, 2020 FaceDeer, 2020 Louis Royer
  8. # LGPLv2.1+
  9. #
  10. # See https://github.com/minetest-tools/update_translations for
  11. # potential future updates to this script.
  12. from __future__ import print_function
  13. import os, fnmatch, re, shutil, errno
  14. from sys import argv as _argv
  15. # Running params
  16. params = {"recursive": False,
  17. "help": False,
  18. "mods": False,
  19. "verbose": False,
  20. "folders": [],
  21. "no-old-file": False
  22. }
  23. # Available CLI options
  24. options = {"recursive": ['--recursive', '-r'],
  25. "help": ['--help', '-h'],
  26. "mods": ['--installed-mods'],
  27. "verbose": ['--verbose', '-v'],
  28. "no-old-file": ['--no-old-file']
  29. }
  30. # Strings longer than this will have extra space added between
  31. # them in the translation files to make it easier to distinguish their
  32. # beginnings and endings at a glance
  33. doublespace_threshold = 60
  34. def set_params_folders(tab: list):
  35. '''Initialize params["folders"] from CLI arguments.'''
  36. # Discarding argument 0 (tool name)
  37. for param in tab[1:]:
  38. stop_param = False
  39. for option in options:
  40. if param in options[option]:
  41. stop_param = True
  42. break
  43. if not stop_param:
  44. params["folders"].append(os.path.abspath(param))
  45. def set_params(tab: list):
  46. '''Initialize params from CLI arguments.'''
  47. for option in options:
  48. for option_name in options[option]:
  49. if option_name in tab:
  50. params[option] = True
  51. break
  52. def print_help(name):
  53. '''Prints some help message.'''
  54. print(f'''SYNOPSIS
  55. {name} [OPTIONS] [PATHS...]
  56. DESCRIPTION
  57. {', '.join(options["help"])}
  58. prints this help message
  59. {', '.join(options["recursive"])}
  60. run on all subfolders of paths given
  61. {', '.join(options["mods"])}
  62. run on locally installed modules
  63. {', '.join(options["no-old-file"])}
  64. do not create *.old files
  65. {', '.join(options["verbose"])}
  66. add output information
  67. ''')
  68. def main():
  69. '''Main function'''
  70. set_params(_argv)
  71. set_params_folders(_argv)
  72. if params["help"]:
  73. print_help(_argv[0])
  74. elif params["recursive"] and params["mods"]:
  75. print("Option --installed-mods is incompatible with --recursive")
  76. else:
  77. # Add recursivity message
  78. print("Running ", end='')
  79. if params["recursive"]:
  80. print("recursively ", end='')
  81. # Running
  82. if params["mods"]:
  83. print(f"on all locally installed modules in {os.path.abspath('~/.minetest/mods/')}")
  84. run_all_subfolders("~/.minetest/mods")
  85. elif len(params["folders"]) >= 2:
  86. print("on folder list:", params["folders"])
  87. for f in params["folders"]:
  88. if params["recursive"]:
  89. run_all_subfolders(f)
  90. else:
  91. update_folder(f)
  92. elif len(params["folders"]) == 1:
  93. print("on folder", params["folders"][0])
  94. if params["recursive"]:
  95. run_all_subfolders(params["folders"][0])
  96. else:
  97. update_folder(params["folders"][0])
  98. else:
  99. print("on folder", os.path.abspath("./"))
  100. if params["recursive"]:
  101. run_all_subfolders(os.path.abspath("./"))
  102. else:
  103. update_folder(os.path.abspath("./"))
  104. #group 2 will be the string, groups 1 and 3 will be the delimiters (" or ')
  105. #See https://stackoverflow.com/questions/46967465/regex-match-text-in-either-single-or-double-quote
  106. pattern_lua = re.compile(r'[\.=^\t,{\(\s]N?S\(\s*(["\'])((?:\\\1|(?:(?!\1)).)*)(\1)[\s,\)]', re.DOTALL)
  107. pattern_lua_bracketed = re.compile(r'[\.=^\t,{\(\s]N?S\(\s*\[\[(.*?)\]\][\s,\)]', re.DOTALL)
  108. # Handles "concatenation" .. " of strings"
  109. pattern_concat = re.compile(r'["\'][\s]*\.\.[\s]*["\']', re.DOTALL)
  110. pattern_tr = re.compile(r'(.+?[^@])=(.*)')
  111. pattern_name = re.compile(r'^name[ ]*=[ ]*([^ \n]*)')
  112. pattern_tr_filename = re.compile(r'\.tr$')
  113. pattern_po_language_code = re.compile(r'(.*)\.po$')
  114. #attempt to read the mod's name from the mod.conf file. Returns None on failure
  115. def get_modname(folder):
  116. try:
  117. with open(os.path.join(folder, "mod.conf"), "r", encoding='utf-8') as mod_conf:
  118. for line in mod_conf:
  119. match = pattern_name.match(line)
  120. if match:
  121. return match.group(1)
  122. except FileNotFoundError:
  123. pass
  124. return None
  125. #If there are already .tr files in /locale, returns a list of their names
  126. def get_existing_tr_files(folder):
  127. out = []
  128. for root, dirs, files in os.walk(os.path.join(folder, 'locale/')):
  129. for name in files:
  130. if pattern_tr_filename.search(name):
  131. out.append(name)
  132. return out
  133. # A series of search and replaces that massage a .po file's contents into
  134. # a .tr file's equivalent
  135. def process_po_file(text):
  136. # The first three items are for unused matches
  137. text = re.sub(r'#~ msgid "', "", text)
  138. text = re.sub(r'"\n#~ msgstr ""\n"', "=", text)
  139. text = re.sub(r'"\n#~ msgstr "', "=", text)
  140. # comment lines
  141. text = re.sub(r'#.*\n', "", text)
  142. # converting msg pairs into "=" pairs
  143. text = re.sub(r'msgid "', "", text)
  144. text = re.sub(r'"\nmsgstr ""\n"', "=", text)
  145. text = re.sub(r'"\nmsgstr "', "=", text)
  146. # various line breaks and escape codes
  147. text = re.sub(r'"\n"', "", text)
  148. text = re.sub(r'"\n', "\n", text)
  149. text = re.sub(r'\\"', '"', text)
  150. text = re.sub(r'\\n', '@n', text)
  151. # remove header text
  152. text = re.sub(r'=Project-Id-Version:.*\n', "", text)
  153. # remove double-spaced lines
  154. text = re.sub(r'\n\n', '\n', text)
  155. return text
  156. # Go through existing .po files and, if a .tr file for that language
  157. # *doesn't* exist, convert it and create it.
  158. # The .tr file that results will subsequently be reprocessed so
  159. # any "no longer used" strings will be preserved.
  160. # Note that "fuzzy" tags will be lost in this process.
  161. def process_po_files(folder, modname):
  162. for root, dirs, files in os.walk(os.path.join(folder, 'locale/')):
  163. for name in files:
  164. code_match = pattern_po_language_code.match(name)
  165. if code_match == None:
  166. continue
  167. language_code = code_match.group(1)
  168. tr_name = modname + "." + language_code + ".tr"
  169. tr_file = os.path.join(root, tr_name)
  170. if os.path.exists(tr_file):
  171. if params["verbose"]:
  172. print(f"{tr_name} already exists, ignoring {name}")
  173. continue
  174. fname = os.path.join(root, name)
  175. with open(fname, "r", encoding='utf-8') as po_file:
  176. if params["verbose"]:
  177. print(f"Importing translations from {name}")
  178. text = process_po_file(po_file.read())
  179. with open(tr_file, "wt", encoding='utf-8') as tr_out:
  180. tr_out.write(text)
  181. # from https://stackoverflow.com/questions/600268/mkdir-p-functionality-in-python/600612#600612
  182. # Creates a directory if it doesn't exist, silently does
  183. # nothing if it already exists
  184. def mkdir_p(path):
  185. try:
  186. os.makedirs(path)
  187. except OSError as exc: # Python >2.5
  188. if exc.errno == errno.EEXIST and os.path.isdir(path):
  189. pass
  190. else: raise
  191. # Converts the template dictionary to a text to be written as a file
  192. # dKeyStrings is a dictionary of localized string to source file sets
  193. # dOld is a dictionary of existing translations and comments from
  194. # the previous version of this text
  195. def strings_to_text(dkeyStrings, dOld, mod_name):
  196. lOut = [f"# textdomain: {mod_name}\n"]
  197. dGroupedBySource = {}
  198. for key in dkeyStrings:
  199. sourceList = list(dkeyStrings[key])
  200. sourceList.sort()
  201. sourceString = "\n".join(sourceList)
  202. listForSource = dGroupedBySource.get(sourceString, [])
  203. listForSource.append(key)
  204. dGroupedBySource[sourceString] = listForSource
  205. lSourceKeys = list(dGroupedBySource.keys())
  206. lSourceKeys.sort()
  207. for source in lSourceKeys:
  208. localizedStrings = dGroupedBySource[source]
  209. localizedStrings.sort()
  210. lOut.append("")
  211. lOut.append(source)
  212. lOut.append("")
  213. for localizedString in localizedStrings:
  214. val = dOld.get(localizedString, {})
  215. translation = val.get("translation", "")
  216. comment = val.get("comment")
  217. if len(localizedString) > doublespace_threshold and not lOut[-1] == "":
  218. lOut.append("")
  219. if comment != None:
  220. lOut.append(comment)
  221. lOut.append(f"{localizedString}={translation}")
  222. if len(localizedString) > doublespace_threshold:
  223. lOut.append("")
  224. unusedExist = False
  225. for key in dOld:
  226. if key not in dkeyStrings:
  227. val = dOld[key]
  228. translation = val.get("translation")
  229. comment = val.get("comment")
  230. # only keep an unused translation if there was translated
  231. # text or a comment associated with it
  232. if translation != None and (translation != "" or comment):
  233. if not unusedExist:
  234. unusedExist = True
  235. lOut.append("\n\n##### not used anymore #####\n")
  236. if len(key) > doublespace_threshold and not lOut[-1] == "":
  237. lOut.append("")
  238. if comment != None:
  239. lOut.append(comment)
  240. lOut.append(f"{key}={translation}")
  241. if len(key) > doublespace_threshold:
  242. lOut.append("")
  243. return "\n".join(lOut) + '\n'
  244. # Writes a template.txt file
  245. # dkeyStrings is the dictionary returned by generate_template
  246. def write_template(templ_file, dkeyStrings, mod_name):
  247. # read existing template file to preserve comments
  248. existing_template = import_tr_file(templ_file)
  249. text = strings_to_text(dkeyStrings, existing_template[0], mod_name)
  250. mkdir_p(os.path.dirname(templ_file))
  251. with open(templ_file, "wt", encoding='utf-8') as template_file:
  252. template_file.write(text)
  253. # Gets all translatable strings from a lua file
  254. def read_lua_file_strings(lua_file):
  255. lOut = []
  256. with open(lua_file, encoding='utf-8') as text_file:
  257. text = text_file.read()
  258. #TODO remove comments here
  259. text = re.sub(pattern_concat, "", text)
  260. strings = []
  261. for s in pattern_lua.findall(text):
  262. strings.append(s[1])
  263. for s in pattern_lua_bracketed.findall(text):
  264. strings.append(s)
  265. for s in strings:
  266. s = re.sub(r'"\.\.\s+"', "", s)
  267. s = re.sub("@[^@=0-9]", "@@", s)
  268. s = s.replace('\\"', '"')
  269. s = s.replace("\\'", "'")
  270. s = s.replace("\n", "@n")
  271. s = s.replace("\\n", "@n")
  272. s = s.replace("=", "@=")
  273. lOut.append(s)
  274. return lOut
  275. # Gets strings from an existing translation file
  276. # returns both a dictionary of translations
  277. # and the full original source text so that the new text
  278. # can be compared to it for changes.
  279. def import_tr_file(tr_file):
  280. dOut = {}
  281. text = None
  282. if os.path.exists(tr_file):
  283. with open(tr_file, "r", encoding='utf-8') as existing_file :
  284. # save the full text to allow for comparison
  285. # of the old version with the new output
  286. text = existing_file.read()
  287. existing_file.seek(0)
  288. # a running record of the current comment block
  289. # we're inside, to allow preceeding multi-line comments
  290. # to be retained for a translation line
  291. latest_comment_block = None
  292. for line in existing_file.readlines():
  293. line = line.rstrip('\n')
  294. if line[:3] == "###":
  295. # Reset comment block if we hit a header
  296. latest_comment_block = None
  297. continue
  298. if line[:1] == "#":
  299. # Save the comment we're inside
  300. if not latest_comment_block:
  301. latest_comment_block = line
  302. else:
  303. latest_comment_block = latest_comment_block + "\n" + line
  304. continue
  305. match = pattern_tr.match(line)
  306. if match:
  307. # this line is a translated line
  308. outval = {}
  309. outval["translation"] = match.group(2)
  310. if latest_comment_block:
  311. # if there was a comment, record that.
  312. outval["comment"] = latest_comment_block
  313. latest_comment_block = None
  314. dOut[match.group(1)] = outval
  315. return (dOut, text)
  316. # Walks all lua files in the mod folder, collects translatable strings,
  317. # and writes it to a template.txt file
  318. # Returns a dictionary of localized strings to source file sets
  319. # that can be used with the strings_to_text function.
  320. def generate_template(folder, mod_name):
  321. dOut = {}
  322. for root, dirs, files in os.walk(folder):
  323. for name in files:
  324. if fnmatch.fnmatch(name, "*.lua"):
  325. fname = os.path.join(root, name)
  326. found = read_lua_file_strings(fname)
  327. if params["verbose"]:
  328. print(f"{fname}: {str(len(found))} translatable strings")
  329. for s in found:
  330. sources = dOut.get(s, set())
  331. sources.add(f"### {os.path.basename(fname)} ###")
  332. dOut[s] = sources
  333. if len(dOut) == 0:
  334. return None
  335. templ_file = os.path.join(folder, "locale/template.txt")
  336. write_template(templ_file, dOut, mod_name)
  337. return dOut
  338. # Updates an existing .tr file, copying the old one to a ".old" file
  339. # if any changes have happened
  340. # dNew is the data used to generate the template, it has all the
  341. # currently-existing localized strings
  342. def update_tr_file(dNew, mod_name, tr_file):
  343. if params["verbose"]:
  344. print(f"updating {tr_file}")
  345. tr_import = import_tr_file(tr_file)
  346. dOld = tr_import[0]
  347. textOld = tr_import[1]
  348. textNew = strings_to_text(dNew, dOld, mod_name)
  349. if textOld and textOld != textNew:
  350. print(f"{tr_file} has changed.")
  351. if not params["no-old-file"]:
  352. shutil.copyfile(tr_file, f"{tr_file}.old")
  353. with open(tr_file, "w", encoding='utf-8') as new_tr_file:
  354. new_tr_file.write(textNew)
  355. # Updates translation files for the mod in the given folder
  356. def update_mod(folder):
  357. modname = get_modname(folder)
  358. if modname is not None:
  359. process_po_files(folder, modname)
  360. print(f"Updating translations for {modname}")
  361. data = generate_template(folder, modname)
  362. if data == None:
  363. print(f"No translatable strings found in {modname}")
  364. else:
  365. for tr_file in get_existing_tr_files(folder):
  366. update_tr_file(data, modname, os.path.join(folder, "locale/", tr_file))
  367. else:
  368. print("Unable to find modname in folder " + folder)
  369. # Determines if the folder being pointed to is a mod or a mod pack
  370. # and then runs update_mod accordingly
  371. def update_folder(folder):
  372. is_modpack = os.path.exists(os.path.join(folder, "modpack.txt")) or os.path.exists(os.path.join(folder, "modpack.conf"))
  373. if is_modpack:
  374. subfolders = [f.path for f in os.scandir(folder) if f.is_dir()]
  375. for subfolder in subfolders:
  376. update_mod(subfolder + "/")
  377. else:
  378. update_mod(folder)
  379. print("Done.")
  380. def run_all_subfolders(folder):
  381. for modfolder in [f.path for f in os.scandir(folder) if f.is_dir()]:
  382. update_folder(modfolder + "/")
  383. main()