i18n.py 18 KB

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