diff options
author | Timofey <t.danshin@corp.mail.ru> | 2016-10-13 18:21:09 +0300 |
---|---|---|
committer | Timofey <t.danshin@corp.mail.ru> | 2016-10-14 17:05:13 +0300 |
commit | 4b5823f0ae48dc7d01c2d3e6e96c2225817f3fe9 (patch) | |
tree | 8ad03d6fd653243584812f8e3605d977f87f4024 /tools/python | |
parent | 98bae6bdfea22757996876c909dc839ebd22fa54 (diff) |
A script for converting categories.txt:
1) to strings txt, so that we can send it to our translation partners.
2) for sorting categories.txt so that it is easier to work with, esp with future additions to it (if any)
3) for merging new strings from po files into a given categories.txt file
Diffstat (limited to 'tools/python')
-rwxr-xr-x | tools/python/categories_converter.py | 129 | ||||
-rwxr-xr-x[-rw-r--r--] | tools/python/po_parser.py | 32 |
2 files changed, 152 insertions, 9 deletions
diff --git a/tools/python/categories_converter.py b/tools/python/categories_converter.py new file mode 100755 index 0000000000..5c6a810382 --- /dev/null +++ b/tools/python/categories_converter.py @@ -0,0 +1,129 @@ +#!/usr/bin/env python2.7 +#coding: utf8 +from __future__ import print_function + +from argparse import ArgumentParser +from collections import defaultdict +from find_untranslated_strings import ITUNES_LANGS + + +class CategoriesConverter: + def __init__(self): + args = self.parse_args() + self.categories = CategoriesTxt(args.categories) + self.should_format = args.format + self.output = args.output + + + def process(self): + if self.should_format: + self.categories.write_formatted() + else: + self.categories.write_as_strings(self.output) + + + def parse_args(self): + parser = ArgumentParser( + description=""" + A script for converting categories.txt into the strings.txt format + and back, as well as for autoformatting categories.txt. This is + useful for interacting with the translation partners. + """ + ) + + parser.add_argument( + "-c", "--categories", + required=True, + dest="categories", + help="""Path to the categories file to be converted into the strings.txt format.""" + ) + + parser.add_argument( + "-o", "--output", + dest="output", + help="""The destination file.""" + ) + + parser.add_argument( + "-f", "--format", + dest="format", action="store_true", default=False, + help="""Format the file and exit""" + ) + + return parser.parse_args() + + +class CategoriesTxt: + """For now, let's allow comments only at the beginning of the file.""" + def __init__(self, filepath): + self.translations = defaultdict(lambda: defaultdict(str)) + self.keys_in_order = [] + self.comments = [] + self.filepath = filepath + self.all_langs = set() + self.parse_file() + + + def parse_file(self): + current_key = "" + this_line_is_key = True + with open(self.filepath) as infile: + for line in map(str.strip, infile): + if line.startswith("#"): + self.comments.append(line) + this_line_is_key = True + elif not line: + this_line_is_key = True + elif this_line_is_key: + self.keys_in_order.append(line) + current_key = line + this_line_is_key = False + else: + pos = line.index(':') + lang = line[:pos] + translation = line[pos + 1:] + self.translations[current_key][lang] = translation + + + def write_as_categories(self, outfile): + self.write_strings_formatted(outfile, "\n{}\n", "{}:{}\n") + + + def write_as_strings(self, filepath): + with open(filepath, "w") as outfile: + self.write_strings_formatted(outfile, key_format="\n [{}]\n", line_format=" {} = {}\n") + + + def write_strings_formatted(self, outfile, key_format, line_format): + for key in self.keys_in_order: + outfile.write(key_format.format(key.strip("[]"))) + pair = self.translations[key] + for lang in ITUNES_LANGS: + if lang in pair: + outfile.write(line_format.format(lang, pair[lang])) + remaining_langs = sorted(list(set(pair.keys()) - set(ITUNES_LANGS))) + for lang in remaining_langs: + outfile.write(line_format.format(lang, pair[lang])) + + + def add_translation(self, translation, key, lang): + if key not in self.keys_in_order: + self.keys_in_order.append(key) + self.translations[key][lang] = translation + + + def append_to_translation(self, translation, key, lang): + self.translations[key][lang] += translation + + + def write_formatted(self): + with open(self.filepath, "w") as outfile: + for comment in self.comments: + outfile.write(comment + "\n") + + self.write_as_categories(outfile) + + +if __name__ == "__main__": + c = CategoriesConverter() + c.process() diff --git a/tools/python/po_parser.py b/tools/python/po_parser.py index 39e268ab0c..5b03f3c078 100644..100755 --- a/tools/python/po_parser.py +++ b/tools/python/po_parser.py @@ -2,11 +2,12 @@ #coding: utf8 from __future__ import print_function -from collections import defaultdict from argparse import ArgumentParser +from categories_converter import CategoriesTxt +from collections import defaultdict +from find_untranslated_strings import StringsTxt from os import listdir from os.path import isfile, join -from find_untranslated_strings import StringsTxt TRANSFORMATION_TABLE = { @@ -24,7 +25,13 @@ class PoParser: args = self.parse_args() self.folder_path = args.folder self.all_po_files = self.find_all_po_files() - self.strings_txt = StringsTxt(args.strings_txt) + + if (args.strings_txt): + self.dest_file = StringsTxt(args.strings_txt) + elif (args.categories_txt): + self.dest_file = CategoriesTxt(args.categories_txt) + else: + raise RuntimeError("You must specify either -s or -c") def find_all_po_files(self): @@ -65,7 +72,7 @@ class PoParser: if not translation: print("No translation for key {} in file {}".format(current_key, filepath)) continue - self.strings_txt.add_translation( + self.dest_file.add_translation( translation, key="[{}]".format(current_key), lang=lang @@ -79,7 +86,7 @@ class PoParser: else: if not string_started: continue - self.strings_txt.append_to_translation(current_key, lang, self.clean_line(line)) + self.dest_file.append_to_translation(current_key, lang, self.clean_line(line)) def clean_line(self, line, prefix=""): @@ -89,8 +96,9 @@ class PoParser: def parse_args(self): parser = ArgumentParser( description=""" - A script for parsing stirngs in the PO format, which is used by our - translation partners. + A script for parsing strings in the PO format, which is used by our + translation partners. The script can pull strings from .po files into + categories.txt or strings.txt. """ ) @@ -102,18 +110,24 @@ class PoParser: parser.add_argument( "-s", "--strings", - dest="strings_txt", required=True, + dest="strings_txt", help="""The path to the strings.txt file. The strings from the po files will be added to that strings.txt file.""" ) + parser.add_argument( + "-c", "--categories", + dest="categories_txt", + help="""The path to the categories.txt file. The strings from the po + files will be added to that categories.txt file.""" + ) return parser.parse_args() def main(): parser = PoParser() parser.parse_files() - parser.strings_txt.write_formatted() + parser.dest_file.write_formatted() if __name__ == "__main__": |