mirror of
https://github.com/kovidgoyal/calibre.git
synced 2025-07-07 10:14:46 -04:00
Add country name translations from the iso-codes project
This commit is contained in:
parent
d9d2e798ca
commit
aaeeb51573
@ -370,7 +370,7 @@ class Translations(POT): # {{{
|
|||||||
files = []
|
files = []
|
||||||
skip_iso = {
|
skip_iso = {
|
||||||
'si', 'te', 'km', 'en_GB', 'en_AU', 'en_CA', 'yi', 'ku', 'my', 'uz@Latn', 'fil', 'hy', 'ltg', 'km_KH', 'km',
|
'si', 'te', 'km', 'en_GB', 'en_AU', 'en_CA', 'yi', 'ku', 'my', 'uz@Latn', 'fil', 'hy', 'ltg', 'km_KH', 'km',
|
||||||
'ur', 'ml', 'fo', 'ug', 'nds', 'jv'
|
'ur', 'ml', 'fo', 'ug', 'jv'
|
||||||
}
|
}
|
||||||
|
|
||||||
def handle_stats(f, data):
|
def handle_stats(f, data):
|
||||||
@ -394,6 +394,26 @@ class Translations(POT): # {{{
|
|||||||
self.compile_group(files, make_translated_strings_unique=True, handle_stats=handle_stats,
|
self.compile_group(files, make_translated_strings_unique=True, handle_stats=handle_stats,
|
||||||
keyfunc=lambda x: os.path.join(self.d(self.SRC), 'iso639', os.path.basename(x)))
|
keyfunc=lambda x: os.path.join(self.d(self.SRC), 'iso639', os.path.basename(x)))
|
||||||
|
|
||||||
|
self.info('Compiling ISO3166 files...')
|
||||||
|
files = []
|
||||||
|
skip_iso = {
|
||||||
|
'en_GB', 'en_AU', 'en_CA', 'yi', 'ku', 'uz@Latn', 'ltg', 'nds', 'jv'
|
||||||
|
}
|
||||||
|
with tempfile.TemporaryDirectory() as tdir:
|
||||||
|
iso_data.extract_po_files('iso_3166-1', tdir)
|
||||||
|
for f, (locale, dest) in iteritems(fmap):
|
||||||
|
pofile = self.j(tdir, f'{locale}.po')
|
||||||
|
if os.path.exists(pofile):
|
||||||
|
files.append((pofile, self.j(self.d(dest), 'iso3166.mo')))
|
||||||
|
else:
|
||||||
|
pofile = self.j(tdir, f'{locale.partition("_")[0]}.po')
|
||||||
|
if os.path.exists(pofile):
|
||||||
|
files.append((pofile, self.j(self.d(dest), 'iso3166.mo')))
|
||||||
|
elif locale not in skip_iso:
|
||||||
|
self.warn('No ISO 3166 translations for locale:', locale)
|
||||||
|
self.compile_group(files, make_translated_strings_unique=True, handle_stats=lambda f,d:None,
|
||||||
|
keyfunc=lambda x: os.path.join(self.d(self.SRC), 'iso3166', os.path.basename(x)))
|
||||||
|
|
||||||
dest = self.stats
|
dest = self.stats
|
||||||
base = self.d(dest)
|
base = self.d(dest)
|
||||||
try:
|
try:
|
||||||
|
@ -122,7 +122,7 @@ def zf_exists():
|
|||||||
allow_user_override=False))
|
allow_user_override=False))
|
||||||
|
|
||||||
|
|
||||||
_lang_trans = None
|
_lang_trans = _country_trans = None
|
||||||
|
|
||||||
|
|
||||||
def get_all_translators():
|
def get_all_translators():
|
||||||
@ -211,7 +211,7 @@ def load_po(path):
|
|||||||
|
|
||||||
|
|
||||||
def translator_for_lang(lang):
|
def translator_for_lang(lang):
|
||||||
t = buf = iso639 = lcdata = None
|
t = buf = iso639 = iso3166 = lcdata = None
|
||||||
if 'CALIBRE_TEST_TRANSLATION' in os.environ:
|
if 'CALIBRE_TEST_TRANSLATION' in os.environ:
|
||||||
buf = load_po(os.path.expanduser(os.environ['CALIBRE_TEST_TRANSLATION']))
|
buf = load_po(os.path.expanduser(os.environ['CALIBRE_TEST_TRANSLATION']))
|
||||||
|
|
||||||
@ -232,6 +232,11 @@ def translator_for_lang(lang):
|
|||||||
iso639 = io.BytesIO(zf.read(isof))
|
iso639 = io.BytesIO(zf.read(isof))
|
||||||
except:
|
except:
|
||||||
pass # No iso639 translations for this lang
|
pass # No iso639 translations for this lang
|
||||||
|
isof = mpath + '/iso3166.mo'
|
||||||
|
try:
|
||||||
|
iso3166 = io.BytesIO(zf.read(isof))
|
||||||
|
except:
|
||||||
|
pass # No iso3166 translations for this lang
|
||||||
if buf is not None:
|
if buf is not None:
|
||||||
from calibre.utils.serialize import msgpack_loads
|
from calibre.utils.serialize import msgpack_loads
|
||||||
try:
|
try:
|
||||||
@ -254,11 +259,19 @@ def translator_for_lang(lang):
|
|||||||
else:
|
else:
|
||||||
if t is not None:
|
if t is not None:
|
||||||
t.add_fallback(iso639)
|
t.add_fallback(iso639)
|
||||||
|
if iso3166 is not None:
|
||||||
|
try:
|
||||||
|
iso3166 = GNUTranslations(iso3166)
|
||||||
|
except Exception:
|
||||||
|
iso3166 = None
|
||||||
|
else:
|
||||||
|
if t is not None:
|
||||||
|
t.add_fallback(iso3166)
|
||||||
|
|
||||||
if t is None:
|
if t is None:
|
||||||
t = NullTranslations()
|
t = NullTranslations()
|
||||||
|
|
||||||
return {'translator': t, 'iso639_translator': iso639, 'lcdata': lcdata}
|
return {'translator': t, 'iso639_translator': iso639, 'iso3166_translator': iso3166, 'lcdata': lcdata}
|
||||||
|
|
||||||
|
|
||||||
default_translator = NullTranslations()
|
default_translator = NullTranslations()
|
||||||
@ -281,7 +294,7 @@ def pgettext(context: str, msg: str) -> str:
|
|||||||
|
|
||||||
|
|
||||||
def set_translators():
|
def set_translators():
|
||||||
global _lang_trans, lcdata, default_translator
|
global _lang_trans, _country_trans, lcdata, default_translator
|
||||||
# To test different translations invoke as
|
# To test different translations invoke as
|
||||||
# CALIBRE_OVERRIDE_LANG=de_DE.utf8 program
|
# CALIBRE_OVERRIDE_LANG=de_DE.utf8 program
|
||||||
lang = get_lang()
|
lang = get_lang()
|
||||||
@ -290,6 +303,7 @@ def set_translators():
|
|||||||
q = translator_for_lang(lang)
|
q = translator_for_lang(lang)
|
||||||
default_translator = q['translator']
|
default_translator = q['translator']
|
||||||
_lang_trans = q['iso639_translator']
|
_lang_trans = q['iso639_translator']
|
||||||
|
_country_trans = q['iso3166_translator']
|
||||||
if q['lcdata']:
|
if q['lcdata']:
|
||||||
lcdata = q['lcdata']
|
lcdata = q['lcdata']
|
||||||
else:
|
else:
|
||||||
@ -456,6 +470,22 @@ def calibre_langcode_to_name(lc, localize=True):
|
|||||||
return lc
|
return lc
|
||||||
|
|
||||||
|
|
||||||
|
def countrycode_to_name(cc, localize=True):
|
||||||
|
iso3166 = load_iso3166()
|
||||||
|
q = cc.upper()
|
||||||
|
if len(q) == 3:
|
||||||
|
q = iso3166['three_map'].get(q, q)
|
||||||
|
try:
|
||||||
|
name = iso3166['names'][q]
|
||||||
|
except Exception:
|
||||||
|
return cc
|
||||||
|
translate = _ if localize else lambda x: x
|
||||||
|
try:
|
||||||
|
return translate(name)
|
||||||
|
except Exception:
|
||||||
|
return name
|
||||||
|
|
||||||
|
|
||||||
def canonicalize_lang(raw):
|
def canonicalize_lang(raw):
|
||||||
if not raw:
|
if not raw:
|
||||||
return None
|
return None
|
||||||
|
Loading…
x
Reference in New Issue
Block a user