def get_builtin_gnu_translations(languages=None):
"""
Get a gettext.GNUTranslations object pointing at the
included translation files.
:param languages:
A list of languages to try, in order. If omitted or None, then
gettext will try to use locale information from the environment.
"""
import gettext
return gettext.translation('wtforms', messages_path(), languages)
python类GNUTranslations()的实例源码
def get_builtin_gnu_translations(languages=None):
"""
Get a gettext.GNUTranslations object pointing at the
included translation files.
:param languages:
A list of languages to try, in order. If omitted or None, then
gettext will try to use locale information from the environment.
"""
import gettext
return gettext.translation('wtforms', messages_path(), languages)
def get_builtin_gnu_translations(languages=None):
"""
Get a gettext.GNUTranslations object pointing at the
included translation files.
:param languages:
A list of languages to try, in order. If omitted or None, then
gettext will try to use locale information from the environment.
"""
import gettext
return gettext.translation('wtforms', messages_path(), languages)
def get_translations(languages=None):
"""
Get a WTForms translation object which wraps the builtin GNUTranslations object.
"""
translations = get_builtin_gnu_translations(languages)
if hasattr(translations, 'ugettext'):
return DefaultTranslations(translations)
else:
# Python 3 has no ugettext/ungettext, so just return the translations object.
return translations
def __init__(self, locale_code):
"""
Initialize GetText
:param locale_code selected locale
"""
try:
filename = os.path.join('/usr/share/harbour-sailcron/python/cron_descriptor/locale', '{}.mo'.format(locale_code))
trans = gettext.GNUTranslations(open(filename, "rb"))
logger.debug('{} Loaded'.format(filename))
except IOError:
logger.debug('Failed to found locale {}'.format(locale_code))
trans = gettext.NullTranslations()
trans.install()
def get_builtin_gnu_translations(languages=None):
"""
Get a gettext.GNUTranslations object pointing at the
included translation files.
:param languages:
A list of languages to try, in order. If omitted or None, then
gettext will try to use locale information from the environment.
"""
import gettext
return gettext.translation('wtforms', messages_path(), languages)
def test_plural_forms2(self):
eq = self.assertEqual
with open(self.mofile, 'rb') as fp:
t = gettext.GNUTranslations(fp)
x = t.ngettext('There is %s file', 'There are %s files', 1)
eq(x, 'Hay %s fichero')
x = t.ngettext('There is %s file', 'There are %s files', 2)
eq(x, 'Hay %s ficheros')
# Examples from http://www.gnu.org/software/gettext/manual/gettext.html
def test_plural_form_error_issue17898(self):
with open(MOFILE, 'wb') as fp:
fp.write(base64.decodestring(GNU_MO_DATA_ISSUE_17898))
with open(MOFILE, 'rb') as fp:
# If this runs cleanly, the bug is fixed.
t = gettext.GNUTranslations(fp)
def setUp(self):
GettextBaseTest.setUp(self)
with open(UMOFILE, 'rb') as fp:
self.t = gettext.GNUTranslations(fp)
self._ = self.t.ugettext
def setUp(self):
GettextBaseTest.setUp(self)
with open(MMOFILE, 'rb') as fp:
try:
self.t = gettext.GNUTranslations(fp)
except:
self.tearDown()
raise
def test_plural_forms2(self):
eq = self.assertEqual
with open(self.mofile, 'rb') as fp:
t = gettext.GNUTranslations(fp)
x = t.ngettext('There is %s file', 'There are %s files', 1)
eq(x, 'Hay %s fichero')
x = t.ngettext('There is %s file', 'There are %s files', 2)
eq(x, 'Hay %s ficheros')
# Examples from http://www.gnu.org/software/gettext/manual/gettext.html
def test_plural_form_error_issue17898(self):
with open(MOFILE, 'wb') as fp:
fp.write(base64.decodestring(GNU_MO_DATA_ISSUE_17898))
with open(MOFILE, 'rb') as fp:
# If this runs cleanly, the bug is fixed.
t = gettext.GNUTranslations(fp)
def setUp(self):
GettextBaseTest.setUp(self)
with open(UMOFILE, 'rb') as fp:
self.t = gettext.GNUTranslations(fp)
self._ = self.t.ugettext
def setUp(self):
GettextBaseTest.setUp(self)
with open(MMOFILE, 'rb') as fp:
try:
self.t = gettext.GNUTranslations(fp)
except:
self.tearDown()
raise
def merge(self, translations):
"""Merge the given translations into the catalog.
Message translations in the specified catalog override any messages
with the same identifier in the existing catalog.
:param translations: the `Translations` instance with the messages to
merge
"""
if isinstance(translations, gettext.GNUTranslations):
self._catalog.update(translations._catalog)
if isinstance(translations, Translations):
self.files.extend(translations.files)
return self
def get_builtin_gnu_translations(languages=None):
"""
Get a gettext.GNUTranslations object pointing at the
included translation files.
:param languages:
A list of languages to try, in order. If omitted or None, then
gettext will try to use locale information from the environment.
"""
import gettext
return gettext.translation('wtforms', messages_path(), languages)
def get_builtin_gnu_translations(languages=None):
"""
Get a gettext.GNUTranslations object pointing at the
included translation files.
:param languages:
A list of languages to try, in order. If omitted or None, then
gettext will try to use locale information from the environment.
"""
import gettext
return gettext.translation('wtforms', messages_path(), languages)
def get_builtin_gnu_translations(languages=None):
"""
Get a gettext.GNUTranslations object pointing at the
included translation files.
:param languages:
A list of languages to try, in order. If omitted or None, then
gettext will try to use locale information from the environment.
"""
import gettext
return gettext.translation('wtforms', messages_path(), languages)
def test_plural_forms2(self):
eq = self.assertEqual
with open(self.mofile, 'rb') as fp:
t = gettext.GNUTranslations(fp)
x = t.ngettext('There is %s file', 'There are %s files', 1)
eq(x, 'Hay %s fichero')
x = t.ngettext('There is %s file', 'There are %s files', 2)
eq(x, 'Hay %s ficheros')
def setUp(self):
GettextBaseTest.setUp(self)
with open(UMOFILE, 'rb') as fp:
self.t = gettext.GNUTranslations(fp)
self._ = self.t.gettext