1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
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()
|