forked from EDCD/EDMarketConnector
-
Notifications
You must be signed in to change notification settings - Fork 0
/
l10n.py
executable file
·183 lines (155 loc) · 7.15 KB
/
l10n.py
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
#!/usr/bin/python
#
# Localization with gettext is a pain on non-Unix systems. Use OSX-style strings files instead.
#
import codecs
from collections import OrderedDict
import os
from os.path import dirname, isfile, join, normpath
import re
import sys
from sys import platform
import __builtin__
# Language name
LANGUAGE_ID = '!Language'
class Translations:
FALLBACK = 'en' # strings in this code are in English
FALLBACK_NAME = 'English'
TRANS_RE = re.compile(r'\s*"([^"]+)"\s*=\s*"([^"]+)"\s*;\s*$')
COMMENT_RE = re.compile(r'\s*/\*.*\*/\s*$')
def __init__(self):
self.translations = {}
def install_dummy(self):
# For when translation is not desired or not available
self.translations = {} # not used
__builtin__.__dict__['_'] = lambda x: unicode(x).replace(u'{CR}', u'\n') # Promote strings to Unicode for consistency
def install(self, lang=None):
available = self.available()
available.add(Translations.FALLBACK)
if not lang:
for preferred in self.preferred():
if preferred in available:
lang = preferred
break
else:
for preferred in self.preferred():
preferred = preferred.split('-',1)[0] # just base language
if preferred in available:
lang = preferred
break
if lang not in self.available():
self.install_dummy()
else:
self.translations = self.contents(lang)
__builtin__.__dict__['_'] = self.translate
def contents(self, lang):
assert lang in self.available()
translations = {}
with self.file(lang) as h:
for line in h:
if line.strip():
match = Translations.TRANS_RE.match(line)
if match:
translations[match.group(1)] = match.group(2).replace(u'{CR}', u'\n')
elif __debug__ and not Translations.COMMENT_RE.match(line):
print 'Bad translation: %s' % line.strip()
if translations.get(LANGUAGE_ID, LANGUAGE_ID) == LANGUAGE_ID:
translations[LANGUAGE_ID] = unicode(lang) # Replace language name with code if missing
return translations
if __debug__:
def translate(self, x):
if not self.translations.get(x):
print 'Missing translation: "%s"' % x
return unicode(x).replace(u'{CR}', u'\n')
else:
return self.translations.get(x) or unicode(x).replace(u'{CR}', u'\n')
else:
def translate(self, x):
return self.translations.get(x) or unicode(x).replace(u'{CR}', u'\n')
# Returns list of available language codes
def available(self):
path = self.respath()
if getattr(sys, 'frozen', False) and platform=='darwin':
available = set([x[:-len('.lproj')] for x in os.listdir(path) if x.endswith('.lproj') and isfile(join(x, 'Localizable.strings'))])
else:
available = set([x[:-len('.strings')] for x in os.listdir(path) if x.endswith('.strings')])
return available
# Available language names by code
def available_names(self):
names = OrderedDict([
(None, _('Default')), # Appearance theme and language setting
])
names.update(sorted([(lang, self.contents(lang).get(LANGUAGE_ID, lang)) for lang in self.available()] +
[(Translations.FALLBACK, Translations.FALLBACK_NAME)],
key=lambda x: x[1])) # Sort by name
return names
# Returns list of preferred language codes in lowercase RFC4646 format.
# Typically "lang[-script][-region]" where lang is a 2 alpha ISO 639-1 or 3 alpha ISO 639-2 code,
# script is a 4 alpha ISO 15924 code and region is a 2 alpha ISO 3166 code
def preferred(self):
if platform=='darwin':
from Foundation import NSLocale
return [x.lower() for x in NSLocale.preferredLanguages()] or None
elif platform=='win32':
def wszarray_to_list(array):
offset = 0
while offset < len(array):
sz = ctypes.wstring_at(ctypes.addressof(array) + offset*2)
if sz:
yield sz
offset += len(sz)+1
else:
break
# https://msdn.microsoft.com/en-us/library/windows/desktop/dd318124%28v=vs.85%29.aspx
import ctypes
MUI_LANGUAGE_ID = 4
MUI_LANGUAGE_NAME = 8
GetUserPreferredUILanguages = ctypes.windll.kernel32.GetUserPreferredUILanguages
num = ctypes.c_ulong()
size = ctypes.c_ulong(0)
if (GetUserPreferredUILanguages(MUI_LANGUAGE_NAME, ctypes.byref(num), None, ctypes.byref(size)) and size.value):
buf = ctypes.create_unicode_buffer(size.value)
if GetUserPreferredUILanguages(MUI_LANGUAGE_NAME, ctypes.byref(num), ctypes.byref(buf), ctypes.byref(size)):
return [x.lower() for x in wszarray_to_list(buf)]
return None
else: # POSIX
import locale
lang = locale.getdefaultlocale()[0]
return lang and [lang.replace('_','-').lower()]
def respath(self):
if getattr(sys, 'frozen', False):
if platform=='darwin':
return normpath(join(dirname(sys.executable), os.pardir, 'Resources'))
else:
return dirname(sys.executable)
elif __file__:
return join(dirname(__file__), 'L10n')
else:
return 'L10n'
def file(self, lang):
if getattr(sys, 'frozen', False) and platform=='darwin':
return codecs.open(join(self.respath(), '%s.lproj' % lang, 'Localizable.strings'), 'r', 'utf-16')
else:
return codecs.open(join(self.respath(), '%s.strings' % lang), 'r', 'utf-8')
# generate template strings file - like xgettext
# parsing is limited - only single ' or " delimited strings, and only one string per line
if __name__ == "__main__":
import re
regexp = re.compile(r'''_\([ur]?(['"])(((?<!\\)\\\1|.)+?)\1\)[^#]*(#.+)?''') # match a single line python literal
seen = {}
for f in sorted([x for x in os.listdir('.') if x.endswith('.py')]):
with codecs.open(f, 'r', 'utf-8') as h:
lineno = 0
for line in h:
lineno += 1
match = regexp.search(line)
if match and not seen.get(match.group(2)): # only record first commented instance of a string
seen[match.group(2)] = (match.group(4) and (match.group(4)[1:].strip()) + '. ' or '') + '[%s]' % f
if seen:
template = codecs.open('L10n/en.template', 'w', 'utf-8')
template.write('/* Language name */\n"%s" = "%s";\n\n' % (LANGUAGE_ID, 'English'))
for thing in sorted(seen, key=unicode.lower):
if seen[thing]:
template.write('/* %s */\n' % (seen[thing]))
template.write('"%s" = "%s";\n\n' % (thing, thing))
template.close()