diff options
author | Sergey Yershov <syershov@maps.me> | 2016-10-12 17:59:12 +0300 |
---|---|---|
committer | GitHub <noreply@github.com> | 2016-10-12 17:59:12 +0300 |
commit | d0212978dd4c7245a9717900d7d5dc62d0aa9b31 (patch) | |
tree | 32704b49f5499e64436ef8141b97320709fc2625 /tools | |
parent | c0f1cea5affeb1f9cd8a6e40005d1f197626544b (diff) | |
parent | 031f41c56df776a458f57dae33e98c6f198e24e4 (diff) |
Merge pull request #4482 from therearesomewhocallmetim/po_format_parser
Added the parser for .po format.
Diffstat (limited to 'tools')
-rwxr-xr-x | tools/python/find_untranslated_strings.py | 14 | ||||
-rw-r--r-- | tools/python/po_parser.py | 63 |
2 files changed, 55 insertions, 22 deletions
diff --git a/tools/python/find_untranslated_strings.py b/tools/python/find_untranslated_strings.py index a5ec529eb7..74051f3580 100755 --- a/tools/python/find_untranslated_strings.py +++ b/tools/python/find_untranslated_strings.py @@ -27,7 +27,7 @@ class StringsTxt: else: self.strings_path = strings_path - self.translations = defaultdict(dict) # dict<key, dict<lang, translation>> + self.translations = defaultdict(lambda: defaultdict(str)) # dict<key, dict<lang, translation>> self.translations_by_language = defaultdict(dict) # dict<lang, dict<key, translation>> self.comments_and_tags = defaultdict(dict) self.with_english = [] @@ -36,6 +36,7 @@ class StringsTxt: self.keys_in_order = [] self._read_file() + def process_file(self): self._populate_translations_by_langs() self._find_duplicates() @@ -45,6 +46,17 @@ class StringsTxt: self._find_most_similar() + 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 + self.all_langs.add(lang) + + + def append_to_translation(self, key, lang, tail): + self.translations[key][lang] = self.translations[key][lang] + tail + + def _read_file(self): with open(self.strings_path) as strings: for line in strings: diff --git a/tools/python/po_parser.py b/tools/python/po_parser.py index 04f1538daf..39e268ab0c 100644 --- a/tools/python/po_parser.py +++ b/tools/python/po_parser.py @@ -1,27 +1,30 @@ +#!/usr/bin/env python2.7 +#coding: utf8 from __future__ import print_function from collections import defaultdict from argparse import ArgumentParser from os import listdir from os.path import isfile, join +from find_untranslated_strings import StringsTxt TRANSFORMATION_TABLE = { - "zh_CN": "zh_Hans", - "zh_TW": "zh_Hant", - "no_NO": "no", - "en_GB": "en_GB" + "zh_CN": "zh-Hans", + "zh_TW": "zh-Hant", + "no_NO": "nb", + "en_GB": "en-GB" } #msgid #msgstr class PoParser: - def __init__(self, folder_path): + def __init__(self): args = self.parse_args() self.folder_path = args.folder - - all_po_files = self.find_all_po_files() + self.all_po_files = self.find_all_po_files() + self.strings_txt = StringsTxt(args.strings_txt) def find_all_po_files(self): @@ -32,16 +35,22 @@ class PoParser: def parse_files(self): - for key, tr in self.translations.iteritems(): - print(" [{}]\n en = {}".format(key, tr)) + for po_file in self.all_po_files: + self._parse_one_file( + join(self.folder_path, po_file), + self.lang_from_filename(po_file) + ) def lang_from_filename(self, filename): - # strings_ru_RU.po + # file names are in this format: strings_ru_RU.po lang = filename[len("strings_"):-len(".po")] + if lang in TRANSFORMATION_TABLE: + return TRANSFORMATION_TABLE[lang] + return lang[:2] - def _parse_one_file(self, filepath): + def _parse_one_file(self, filepath, lang): self.translations = defaultdict(str) current_key = None string_started = False @@ -54,19 +63,23 @@ class PoParser: continue translation = self.clean_line(line, "msgstr") if not translation: - print("No translation for key {}".format(current_key)) + print("No translation for key {} in file {}".format(current_key, filepath)) continue - self.translations[current_key] = translation + self.strings_txt.add_translation( + translation, + key="[{}]".format(current_key), + lang=lang + ) string_started = True + elif not line or line.startswith("#"): string_started = False current_key = None + else: if not string_started: continue - self.translations[current_key] = "{}{}".format(self.translations[current_key], self.clean_line(line)) - - + self.strings_txt.append_to_translation(current_key, lang, self.clean_line(line)) def clean_line(self, line, prefix=""): @@ -87,13 +100,21 @@ class PoParser: help="""Path to the folder where the PO files are. Required.""" ) + parser.add_argument( + "-s", "--strings", + dest="strings_txt", required=True, + help="""The path to the strings.txt file. The strings from the po + files will be added to that strings.txt file.""" + ) + + return parser.parse_args() def main(): - # parser = PoParser("en.po", "en") - # for key, tr in parser.translations.iteritems(): - # print(" [{}]\n en = {}".format(key, tr)) - pass + parser = PoParser() + parser.parse_files() + parser.strings_txt.write_formatted() + if __name__ == "__main__": - main()
\ No newline at end of file + main() |