forked from mobilejazz-contrib/xcode-i18n
-
Notifications
You must be signed in to change notification settings - Fork 0
/
localize.py.orig
executable file
·205 lines (161 loc) · 7.03 KB
/
localize.py.orig
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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# This program is free software. It comes without any warranty, to
# the extent permitted by applicable law. You can redistribute it
# and/or modify it under the terms of the Do What The Fuck You Want
# To Public License, Version 2, as published by Sam Hocevar. See
# http://sam.zoy.org/wtfpl/COPYING for more details.
#
# Localize.py - Incremental localization on XCode projects
# João Moreno 2009
# http://joaomoreno.com/
# Mobile Jazz, taken from:
# https://github.com/joaomoreno/Green-Apples/blob/master/localize.py
# and adapted to also use ibtool to localize XIB and storyboard files.
# Add it as build phase in Xcode or run manually every time you need.
# TROUBLESHOOTING
# If ibtool complains about not finding Xcode after migration to version 4.3, run:
# sudo xcode-select -switch /Applications/Xcode.app/Contents/Developer/
from sys import argv
from codecs import open
from re import compile
from copy import copy
import os
re_translation = compile(r'^"(.+)" = "(.+)";$')
re_comment_single = compile(r'^/\*.*\*/$')
re_comment_start = compile(r'^/\*.*$')
re_comment_end = compile(r'^.*\*/$')
def print_help():
print u"""Usage: localize.py
Xcode localizable strings merger script. João Moreno 2009 & Mobile Jazz 2013.
Set up your XIBs/Storyboards in English language, provide translations in Localizable.strings and have this script create/update localized XIBs/Storyboards for you."""
class LocalizedString():
def __init__(self, comments, translation):
self.comments, self.translation = comments, translation
self.key, self.value = re_translation.match(self.translation).groups()
def __unicode__(self):
return u'%s%s\n' % (u''.join(self.comments), self.translation)
class LocalizedFile():
def __init__(self, fname=None, auto_read=False):
self.fname = fname
self.strings = []
self.strings_d = {}
if auto_read:
self.read_from_file(fname)
def read_from_file(self, fname=None):
fname = self.fname if fname == None else fname
try:
f = open(fname, encoding='utf-8', mode='r')
except UnicodeDecodeError:
f = open(fname, encoding='utf-16', mode='r')
except:
print 'File %s does not exist.' % fname
exit(-1)
line = f.readline()
while line:
comments = [line]
if not re_comment_single.match(line):
while line and not re_comment_end.match(line):
line = f.readline()
comments.append(line)
line = f.readline()
if line and re_translation.match(line):
translation = line
else:
raise Exception('invalid file')
line = f.readline()
while line and line == u'\n':
line = f.readline()
string = LocalizedString(comments, translation)
self.strings.append(string)
self.strings_d[string.key] = string
f.close()
def save_to_file(self, fname=None):
fname = self.fname if fname == None else fname
try:
f = open(fname, encoding='utf-8', mode='w')
except:
print 'Couldn\'t open file %s.' % fname
exit(-1)
for string in self.strings:
f.write(string.__unicode__())
f.close()
def merge_with(self, new):
merged = LocalizedFile()
for string in new.strings:
if self.strings_d.has_key(string.key):
new_string = copy(self.strings_d[string.key])
new_string.comments = string.comments
string = new_string
merged.strings.append(string)
merged.strings_d[string.key] = string
return merged
def merge(merged_fname, old_fname, new_fname):
try:
old = LocalizedFile(old_fname, auto_read=True)
new = LocalizedFile(new_fname, auto_read=True)
except Exception, err:
print 'Error: input files have invalid format:', err
merged = old.merge_with(new)
merged.save_to_file(merged_fname)
TEMPLATE_LANG = 'en.lproj'
def localizeXibs(language):
xibs = [name for name in os.listdir(language) if name.endswith('.xib') or name.endswith('.storyboard')]
for xib in xibs:
xibFile = language + '/' + xib
tmpFile = xibFile + '.strings.old'
templateFile = TEMPLATE_LANG + '/' + xib
# apply only if template file exists as well, and if templateFile is newer
if os.path.isfile(templateFile) and os.stat(templateFile).st_mtime > os.stat(xibFile).st_mtime:
print('ibtool "%s" --generate-strings-file "%s"' % (xibFile, tmpFile))
os.system('ibtool "%s" --generate-strings-file "%s"' % (xibFile, tmpFile))
print('ibtool --write "%s" --strings-file "%s" "%s"' % (xibFile, tmpFile, templateFile))
os.system('ibtool --write "%s" --strings-file "%s" "%s"' % (xibFile, tmpFile, templateFile))
def localize(path):
languages = [name for name in os.listdir(path) if name.endswith('.lproj') and os.path.isdir(name)]
for language in languages:
for filename in os.listdir(language):
<<<<<<< HEAD
# move aside existing files
original = merged = language + os.path.sep + filename
new = original + '.new'
old = original + '.old'
if os.path.isfile(old):
os.remove(old)
if os.path.isfile(new):
os.remove(new)
if filename.endswith('.strings'):
os.rename(original, old)
# generate new strings files
os.system('genstrings -q -o "%s" `find . -name "*.m"`' % language)
# merge and/or convert new files
=======
# move aside existing files
if filename.endswith('.strings'):
original = language + os.path.sep + filename
old = original + '.old'
os.rename(original, old)
# generate new strings fiels
os.system('genstrings -q -o "%s" `find . -name "*.m"`' % language)
# merge and/or convert new files
>>>>>>> FETCH_HEAD
for filename in os.listdir(language):
if filename.endswith('.strings'):
original = merged = language + os.path.sep + filename
new = original + '.new'
old = original + '.old'
if os.path.isfile(old):
os.system('iconv -f UTF-16 -t UTF-8 "%s" > "%s"' % (original, new))
merge(merged, old, new)
else:
os.rename(original, old)
os.system('iconv -f UTF-16 -t UTF-8 "%s" > "%s"' % (old, original))
if os.path.isfile(old):
os.remove(old)
if os.path.isfile(new):
os.remove(new)
# also localize xibs, based on TEMPLATE_LANG
if language != TEMPLATE_LANG:
localizeXibs(language);
if __name__ == '__main__':
localize(os.getcwd())