本文整理汇总了Python中gettext.find方法的典型用法代码示例。如果您正苦于以下问题:Python gettext.find方法的具体用法?Python gettext.find怎么用?Python gettext.find使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类gettext
的用法示例。
在下文中一共展示了gettext.find方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: cached_find
# 需要导入模块: import gettext [as 别名]
# 或者: from gettext import find [as 别名]
def cached_find(domain, localedir=None, languages=None, all=0):
"""A version of gettext.find using a cache.
gettext.find looks for mo files on the disk using os.path.exists. Those
don't tend to change over time, but the system calls pile up with a
long-running service. This caches the result so that we return the same mo
files, and only call find once per domain.
"""
key = (domain,
localedir,
tuple(languages) if languages is not None else None,
all)
if key in _FIND_CACHE:
return _FIND_CACHE[key]
result = _original_find(domain, localedir, languages, all)
_FIND_CACHE[key] = result
return result
示例2: check_for_language
# 需要导入模块: import gettext [as 别名]
# 或者: from gettext import find [as 别名]
def check_for_language(lang_code):
"""
Checks whether there is a global language file for the given language
code. This is used to decide whether a user-provided language is
available.
lru_cache should have a maxsize to prevent from memory exhaustion attacks,
as the provided language codes are taken from the HTTP request. See also
<https://www.djangoproject.com/weblog/2007/oct/26/security-fix/>.
"""
# First, a quick check to make sure lang_code is well-formed (#21458)
if lang_code is None or not language_code_re.search(lang_code):
return False
for path in all_locale_paths():
if gettext_module.find('django', path, [to_locale(lang_code)]) is not None:
return True
return False
示例3: load
# 需要导入模块: import gettext [as 别名]
# 或者: from gettext import find [as 别名]
def load(cls, dirname=None, locales=None, domain=None):
"""Load translations from the given directory.
:param dirname: the directory containing the ``MO`` files
:param locales: the list of locales in order of preference (items in
this list can be either `Locale` objects or locale
strings)
:param domain: the message domain (default: 'messages')
"""
if locales is not None:
if not isinstance(locales, (list, tuple)):
locales = [locales]
locales = [str(locale) for locale in locales]
if not domain:
domain = cls.DEFAULT_DOMAIN
filename = gettext.find(domain, dirname, locales)
if not filename:
return NullTranslations()
with open(filename, 'rb') as fp:
return cls(fp=fp, domain=domain)
示例4: gettext
# 需要导入模块: import gettext [as 别名]
# 或者: from gettext import find [as 别名]
def gettext(message):
"""
Translate the 'message' string. It uses the current thread to find the
translation object to use. If no current translation is activated, the
message will be run through the default translation object.
"""
global _default
eol_message = message.replace('\r\n', '\n').replace('\r', '\n')
if len(eol_message) == 0:
# Return an empty value of the corresponding type if an empty message
# is given, instead of metadata, which is the default gettext behavior.
result = type(message)("")
else:
_default = _default or translation(settings.LANGUAGE_CODE)
translation_object = getattr(_active, "value", _default)
result = translation_object.gettext(eol_message)
if isinstance(message, SafeData):
return mark_safe(result)
return result
示例5: check_for_language
# 需要导入模块: import gettext [as 别名]
# 或者: from gettext import find [as 别名]
def check_for_language(lang_code):
"""
Check whether there is a global language file for the given language
code. This is used to decide whether a user-provided language is
available.
lru_cache should have a maxsize to prevent from memory exhaustion attacks,
as the provided language codes are taken from the HTTP request. See also
<https://www.djangoproject.com/weblog/2007/oct/26/security-fix/>.
"""
# First, a quick check to make sure lang_code is well-formed (#21458)
if lang_code is None or not language_code_re.search(lang_code):
return False
for path in all_locale_paths():
if gettext_module.find('django', path, [to_locale(lang_code)]) is not None:
return True
return False
示例6: _update
# 需要导入模块: import gettext [as 别名]
# 或者: from gettext import find [as 别名]
def _update(self) -> None:
"""Fill the catalogue by loading the translated texts from file (again)."""
if not self.__application:
self.__require_update = True
return
if not self.__name:
self.__name = self.__application.getApplicationName()
if self.__language == "default":
self.__language = self.__application.getApplicationLanguage()
# Ask gettext for all the translations in the .mo files.
for path in Resources.getAllPathsForType(Resources.i18n):
if gettext.find(cast(str, self.__name), path, languages = [self.__language]):
try:
self.__translation = gettext.translation(cast(str, self.__name), path, languages = [self.__language])
except OSError:
Logger.warning("Corrupt or inaccessible translation file: {fname}".format(fname = self.__name))
self.__require_update = False
示例7: gettext
# 需要导入模块: import gettext [as 别名]
# 或者: from gettext import find [as 别名]
def gettext(message):
"""
Translate the 'message' string. It uses the current thread to find the
translation object to use. If no current translation is activated, the
message will be run through the default translation object.
"""
global _default
eol_message = message.replace('\r\n', '\n').replace('\r', '\n')
if eol_message:
_default = _default or translation(settings.LANGUAGE_CODE)
translation_object = getattr(_active, "value", _default)
result = translation_object.gettext(eol_message)
else:
# Return an empty value of the corresponding type if an empty message
# is given, instead of metadata, which is the default gettext behavior.
result = type(message)('')
if isinstance(message, SafeData):
return mark_safe(result)
return result
示例8: check_for_language
# 需要导入模块: import gettext [as 别名]
# 或者: from gettext import find [as 别名]
def check_for_language(lang_code):
"""
Check whether there is a global language file for the given language
code. This is used to decide whether a user-provided language is
available.
lru_cache should have a maxsize to prevent from memory exhaustion attacks,
as the provided language codes are taken from the HTTP request. See also
<https://www.djangoproject.com/weblog/2007/oct/26/security-fix/>.
"""
# First, a quick check to make sure lang_code is well-formed (#21458)
if lang_code is None or not language_code_re.search(lang_code):
return False
return any(
gettext_module.find('django', path, [to_locale(lang_code)]) is not None
for path in all_locale_paths()
)
示例9: to_locale
# 需要导入模块: import gettext [as 别名]
# 或者: from gettext import find [as 别名]
def to_locale(language, to_lower=False):
"""
Turns a language name (en-us) into a locale name (en_US). If 'to_lower' is
True, the last component is lower-cased (en_us).
"""
p = language.find('-')
if p >= 0:
if to_lower:
return language[:p].lower() + '_' + language[p + 1:].lower()
else:
# Get correct locale for sr-latn
if len(language[p + 1:]) > 2:
return language[:p].lower() + '_' + language[p + 1].upper() + language[p + 2:].lower()
return language[:p].lower() + '_' + language[p + 1:].upper()
else:
return language.lower()
示例10: to_locale
# 需要导入模块: import gettext [as 别名]
# 或者: from gettext import find [as 别名]
def to_locale(language, to_lower=False):
"""
Turns a language name (en-us) into a locale name (en_US). If 'to_lower' is
True, the last component is lower-cased (en_us).
"""
p = language.find('-')
if p >= 0:
if to_lower:
return language[:p].lower()+'_'+language[p+1:].lower()
else:
# Get correct locale for sr-latn
if len(language[p+1:]) > 2:
return language[:p].lower()+'_'+language[p+1].upper()+language[p+2:].lower()
return language[:p].lower()+'_'+language[p+1:].upper()
else:
return language.lower()
示例11: do_translate
# 需要导入模块: import gettext [as 别名]
# 或者: from gettext import find [as 别名]
def do_translate(message, translation_function):
"""
Translates 'message' using the given 'translation_function' name -- which
will be either gettext or ugettext. It uses the current thread to find the
translation object to use. If no current translation is activated, the
message will be run through the default translation object.
"""
global _default
# str() is allowing a bytestring message to remain bytestring on Python 2
eol_message = message.replace(str('\r\n'), str('\n')).replace(str('\r'), str('\n'))
t = getattr(_active, "value", None)
if t is not None:
result = getattr(t, translation_function)(eol_message)
else:
if _default is None:
from django.conf import settings
_default = translation(settings.LANGUAGE_CODE)
result = getattr(_default, translation_function)(eol_message)
if isinstance(message, SafeData):
return mark_safe(result)
return result
示例12: do_translate
# 需要导入模块: import gettext [as 别名]
# 或者: from gettext import find [as 别名]
def do_translate(message, translation_function):
"""
Translates 'message' using the given 'translation_function' name -- which
will be either gettext or ugettext. It uses the current thread to find the
translation object to use. If no current translation is activated, the
message will be run through the default translation object.
"""
eol_message = message.replace('\r\n', '\n').replace('\r', '\n')
global _default, _active
t = _active.get(currentThread(), None)
if t is not None:
result = getattr(t, translation_function)(eol_message)
else:
if _default is None:
from google.appengine._internal.django.conf import settings
_default = translation(settings.LANGUAGE_CODE)
result = getattr(_default, translation_function)(eol_message)
if isinstance(message, SafeData):
return mark_safe(result)
return result
示例13: load_locale
# 需要导入模块: import gettext [as 别名]
# 或者: from gettext import find [as 别名]
def load_locale():
import gettext
from importlib.resources import path
# Load pre-installed translation
with path('tuijam', 'lang') as locale_path:
locale = gettext.find('tuijam', locale_path)
if locale is not None:
gettext.bindtextdomain('tuijam', locale_path)
gettext.textdomain('tuijam')
# Then load user translation
locale = gettext.find('tuijam', LOCALE_DIR)
if locale is not None:
gettext.bindtextdomain('tuijam', LOCALE_DIR)
gettext.textdomain('tuijam')
示例14: test_cached_find
# 需要导入模块: import gettext [as 别名]
# 或者: from gettext import find [as 别名]
def test_cached_find(self):
domain = 'my-unique-domain'
key = (domain, None, None, 0)
self.assertNotIn(key, _gettextutils._FIND_CACHE)
gettext.find(domain)
self.assertIn(key, _gettextutils._FIND_CACHE)
_gettextutils._FIND_CACHE[key] = "spoof result"
self.assertEqual("spoof result", gettext.find(domain))
_gettextutils._FIND_CACHE.pop(key)
示例15: get_available_languages
# 需要导入模块: import gettext [as 别名]
# 或者: from gettext import find [as 别名]
def get_available_languages(domain):
"""Lists the available languages for the given translation domain.
:param domain: the domain to get languages for
"""
if domain in _AVAILABLE_LANGUAGES:
return copy.copy(_AVAILABLE_LANGUAGES[domain])
localedir = os.environ.get(_locale.get_locale_dir_variable_name(domain))
def find(x):
return gettext.find(domain, localedir=localedir, languages=[x])
# NOTE(mrodden): en_US should always be available (and first in case
# order matters) since our in-line message strings are en_US
language_list = ['en_US']
locale_identifiers = set(locale.windows_locale.values())
language_list.extend(
language for language in locale_identifiers if find(language)
)
language_list.extend(
alias for alias, _ in _BABEL_ALIASES.items() if find(alias)
)
_AVAILABLE_LANGUAGES[domain] = language_list
return copy.copy(language_list)