forked from organicmaps/organicmaps
Merge pull request #4497 from therearesomewhocallmetim/cat_to_str_txt
A script for converting categories.txt:
This commit is contained in:
commit
93734c1369
4 changed files with 3396 additions and 3252 deletions
2
.gitignore
vendored
2
.gitignore
vendored
|
@ -18,7 +18,7 @@ data/*.mwmmeta
|
|||
data/[!W]*.mwm
|
||||
|
||||
# Compiled Python
|
||||
crawler/*.pyc
|
||||
*.pyc
|
||||
|
||||
# Symbian
|
||||
.obj/*
|
||||
|
|
6485
data/categories.txt
6485
data/categories.txt
File diff suppressed because it is too large
Load diff
129
tools/python/categories_converter.py
Executable file
129
tools/python/categories_converter.py
Executable file
|
@ -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()
|
32
tools/python/po_parser.py
Normal file → Executable file
32
tools/python/po_parser.py
Normal file → Executable file
|
@ -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__":
|
||||
|
|
Loading…
Add table
Reference in a new issue