def i18n_wrapper(locale: Optional[str] = None) -> Any:
"""
Internationalize using gettext.
Returns gettext for provided locale
or default (zentropi.defaults.LOCALE).
Example:
>>> from zentropi.utils import i18n_wrapper
>>> _ = i18n_wrapper()
>>> print(_('Hello, world.'))
Hello, world.
"""
from zentropi import BASE_PATH
from zentropi.defaults import LOCALE
locale = locale or lib_locale.getlocale()[0] or LOCALE
locale_dir = os.path.join(BASE_PATH, 'locale')
locale_file = os.path.join(locale_dir, '{}.mo'.format(locale))
try:
translation = gettext.GNUTranslations(open(locale_file, 'rb'))
except IOError:
warnings.warn('Translation file for {} not found at: {}. Using default.'
''.format(locale, locale_file))
translation = gettext.NullTranslations() # type: ignore
translation.install()
return translation.gettext
评论列表
文章目录