[utils] Add a cache for translations
This commit is contained in:
parent
312bfeca77
commit
cedf7b1510
@ -2531,20 +2531,22 @@ class I18N(object):
|
|||||||
def __init__(self):
|
def __init__(self):
|
||||||
self.default_lang = None
|
self.default_lang = None
|
||||||
self.domain = 'youtube_dl'
|
self.domain = 'youtube_dl'
|
||||||
|
self._translation_cache = {}
|
||||||
|
|
||||||
def translate(self, s):
|
def _load_translation(self, lang):
|
||||||
if self.default_lang is not None:
|
t = self._translation_cache.get(lang)
|
||||||
lang = self.default_lang
|
|
||||||
else:
|
|
||||||
lang, _ = locale.getdefaultlocale()
|
|
||||||
|
|
||||||
for root in get_root_dirs():
|
if t is not None:
|
||||||
try:
|
return t
|
||||||
t = gettext.translation(self.domain, os.path.join(root, 'share', 'locale'), [lang])
|
|
||||||
if t is not None:
|
if t is None:
|
||||||
break
|
for root in get_root_dirs():
|
||||||
except (OSError, IOError): # OSError for 3.3+ and IOError otherwise
|
try:
|
||||||
t = None
|
t = gettext.translation(self.domain, os.path.join(root, 'share', 'locale'), [lang])
|
||||||
|
if t is not None:
|
||||||
|
break
|
||||||
|
except (OSError, IOError): # OSError for 3.3+ and IOError otherwise
|
||||||
|
t = None
|
||||||
|
|
||||||
if t is None and sys.platform == 'win32' and hasattr(sys, 'frozen'):
|
if t is None and sys.platform == 'win32' and hasattr(sys, 'frozen'):
|
||||||
locale_data_zip = _load_exe_resource('LOCALE_DATA', 'LOCALE_DATA.ZIP')
|
locale_data_zip = _load_exe_resource('LOCALE_DATA', 'LOCALE_DATA.ZIP')
|
||||||
@ -2559,6 +2561,18 @@ class I18N(object):
|
|||||||
t = gettext.GNUTranslations(mo_file)
|
t = gettext.GNUTranslations(mo_file)
|
||||||
zipf.close()
|
zipf.close()
|
||||||
|
|
||||||
|
if t is not None:
|
||||||
|
self._translation_cache[lang] = t
|
||||||
|
|
||||||
|
return t
|
||||||
|
|
||||||
|
def translate(self, s):
|
||||||
|
if self.default_lang is not None:
|
||||||
|
lang = self.default_lang
|
||||||
|
else:
|
||||||
|
lang, _ = locale.getdefaultlocale()
|
||||||
|
|
||||||
|
t = self._load_translation(lang)
|
||||||
if t is None:
|
if t is None:
|
||||||
return s
|
return s
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user