This commit is contained in:
Kovid Goyal 2011-08-12 14:31:50 -06:00
parent 4a59776488
commit f77672a596
2 changed files with 24 additions and 16 deletions

View File

@ -273,10 +273,9 @@ class GetTranslations(Translations):
class ISO639(Command): class ISO639(Command):
description = 'Compile translations for ISO 639 codes' description = 'Compile translations for ISO 639 codes'
XML = '/usr/lib/python2.7/site-packages/pycountry/databases/iso639.xml'
def run(self, opts): def run(self, opts):
src = self.XML src = self.j(self.d(self.SRC), 'setup', 'iso639.xml')
if not os.path.exists(src): if not os.path.exists(src):
raise Exception(src + ' does not exist') raise Exception(src + ' does not exist')
dest = self.j(self.d(self.SRC), 'resources', 'localization', dest = self.j(self.d(self.SRC), 'resources', 'localization',
@ -290,20 +289,27 @@ class ISO639(Command):
by_2 = {} by_2 = {}
by_3b = {} by_3b = {}
by_3t = {} by_3t = {}
m2to3 = {}
m3to2 = {}
codes2, codes3t, codes3b = set([]), set([]), set([]) codes2, codes3t, codes3b = set([]), set([]), set([])
for x in root.xpath('//iso_639_entry'): for x in root.xpath('//iso_639_entry'):
name = x.get('name') name = x.get('name')
two = x.get('iso_639_1_code', None) two = x.get('iso_639_1_code', None)
threeb = x.get('iso_639_2B_code')
threet = x.get('iso_639_2T_code')
if two is not None: if two is not None:
by_2[two] = name by_2[two] = name
codes2.add(two) codes2.add(two)
by_3b[x.get('iso_639_2B_code')] = name m2to3[two] = threet
by_3t[x.get('iso_639_2T_code')] = name m3to2[threeb] = m3to2[threet] = two
by_3b[threeb] = name
by_3t[threet] = name
codes3b.add(x.get('iso_639_2B_code')) codes3b.add(x.get('iso_639_2B_code'))
codes3t.add(x.get('iso_639_2T_code')) codes3t.add(x.get('iso_639_2T_code'))
from cPickle import dump from cPickle import dump
x = {'by_2':by_2, 'by_3b':by_3b, 'by_3t':by_3t, 'codes2':codes2, x = {'by_2':by_2, 'by_3b':by_3b, 'by_3t':by_3t, 'codes2':codes2,
'codes3b':codes3b, 'codes3t':codes3t} 'codes3b':codes3b, 'codes3t':codes3t, '2to3':m2to3,
'3to2':m3to2}
dump(x, open(dest, 'wb'), -1) dump(x, open(dest, 'wb'), -1)

View File

@ -165,31 +165,33 @@ _lcase_map = {}
for k in _extra_lang_codes: for k in _extra_lang_codes:
_lcase_map[k.lower()] = k _lcase_map[k.lower()] = k
def get_language(lang): def _load_iso639():
global _iso639 global _iso639
if _iso639 is None:
ip = P('localization/iso639.pickle', allow_user_override=False)
with open(ip, 'rb') as f:
_iso639 = cPickle.load(f)
return _iso639
def get_language(lang):
translate = _ translate = _
lang = _lcase_map.get(lang, lang) lang = _lcase_map.get(lang, lang)
if lang in _extra_lang_codes: if lang in _extra_lang_codes:
# The translator was not active when _extra_lang_codes was defined, so # The translator was not active when _extra_lang_codes was defined, so
# re-translate # re-translate
return translate(_extra_lang_codes[lang]) return translate(_extra_lang_codes[lang])
ip = P('localization/iso639.pickle') iso639 = _load_iso639()
if not os.path.exists(ip):
return lang
if _iso639 is None:
_iso639 = cPickle.load(open(ip, 'rb'))
ans = lang ans = lang
lang = lang.split('_')[0].lower() lang = lang.split('_')[0].lower()
if len(lang) == 2: if len(lang) == 2:
ans = _iso639['by_2'].get(lang, ans) ans = iso639['by_2'].get(lang, ans)
elif len(lang) == 3: elif len(lang) == 3:
if lang in _iso639['by_3b']: if lang in iso639['by_3b']:
ans = _iso639['by_3b'][lang] ans = iso639['by_3b'][lang]
else: else:
ans = _iso639['by_3t'].get(lang, ans) ans = iso639['by_3t'].get(lang, ans)
return translate(ans) return translate(ans)
_udc = None _udc = None
def get_udc(): def get_udc():