diff --git a/setup/translations.py b/setup/translations.py index 499a0d2f22..611b3b2d68 100644 --- a/setup/translations.py +++ b/setup/translations.py @@ -273,10 +273,9 @@ class GetTranslations(Translations): class ISO639(Command): description = 'Compile translations for ISO 639 codes' - XML = '/usr/lib/python2.7/site-packages/pycountry/databases/iso639.xml' def run(self, opts): - src = self.XML + src = self.j(self.d(self.SRC), 'setup', 'iso639.xml') if not os.path.exists(src): raise Exception(src + ' does not exist') dest = self.j(self.d(self.SRC), 'resources', 'localization', @@ -290,20 +289,27 @@ class ISO639(Command): by_2 = {} by_3b = {} by_3t = {} + m2to3 = {} + m3to2 = {} codes2, codes3t, codes3b = set([]), set([]), set([]) for x in root.xpath('//iso_639_entry'): name = x.get('name') 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: by_2[two] = name codes2.add(two) - by_3b[x.get('iso_639_2B_code')] = name - by_3t[x.get('iso_639_2T_code')] = name + m2to3[two] = threet + m3to2[threeb] = m3to2[threet] = two + by_3b[threeb] = name + by_3t[threet] = name codes3b.add(x.get('iso_639_2B_code')) codes3t.add(x.get('iso_639_2T_code')) from cPickle import dump 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) diff --git a/src/calibre/utils/localization.py b/src/calibre/utils/localization.py index d13c4859a5..1b3347c5bd 100644 --- a/src/calibre/utils/localization.py +++ b/src/calibre/utils/localization.py @@ -165,31 +165,33 @@ _lcase_map = {} for k in _extra_lang_codes: _lcase_map[k.lower()] = k -def get_language(lang): +def _load_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 = _ lang = _lcase_map.get(lang, lang) if lang in _extra_lang_codes: # The translator was not active when _extra_lang_codes was defined, so # re-translate return translate(_extra_lang_codes[lang]) - ip = P('localization/iso639.pickle') - if not os.path.exists(ip): - return lang - if _iso639 is None: - _iso639 = cPickle.load(open(ip, 'rb')) + iso639 = _load_iso639() ans = lang lang = lang.split('_')[0].lower() if len(lang) == 2: - ans = _iso639['by_2'].get(lang, ans) + ans = iso639['by_2'].get(lang, ans) elif len(lang) == 3: - if lang in _iso639['by_3b']: - ans = _iso639['by_3b'][lang] + if lang in iso639['by_3b']: + ans = iso639['by_3b'][lang] else: - ans = _iso639['by_3t'].get(lang, ans) + ans = iso639['by_3t'].get(lang, ans) return translate(ans) - _udc = None def get_udc():