本文整理汇总了Python中babel.messages.Catalog.locale方法的典型用法代码示例。如果您正苦于以下问题:Python Catalog.locale方法的具体用法?Python Catalog.locale怎么用?Python Catalog.locale使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类babel.messages.Catalog
的用法示例。
在下文中一共展示了Catalog.locale方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: xml2po
# 需要导入模块: from babel.messages import Catalog [as 别名]
# 或者: from babel.messages.Catalog import locale [as 别名]
def xml2po(resources, translations=None, filter=None, warnfunc=dummy_warn):
"""Return ``resources`` as a Babel .po ``Catalog`` instance.
If given, ``translations`` will be used for the translated values.
In this case, the returned value is a 2-tuple (catalog, unmatched),
with the latter being a list of Android string resource names that
are in the translated file, but not in the original.
Both ``resources`` and ``translations`` must be ``ResourceTree``
objects, as returned by ``read_xml()``.
From the application perspective, it will call this function with
a ``translations`` object when initializing a new .po file based on
an existing resource file (the 'init' command). For 'export', this
function is called without translations. It will thus generate what
is essentially a POT file (an empty .po file), and this will be
merged into the existing .po catalogs, as per how gettext usually
"""
assert not translations or translations.language
catalog = Catalog()
if translations is not None:
catalog.locale = translations.language.locale
# We cannot let Babel determine the plural expr for the locale by
# itself. It will use a custom list of plural expressions rather
# than generate them based on CLDR.
# See http://babel.edgewall.org/ticket/290.
set_catalog_plural_forms(catalog, translations.language)
for name, org_value in resources.iteritems():
if filter and filter(name):
continue
trans_value = None
if translations:
trans_value = translations.pop(name, trans_value)
if isinstance(org_value, StringArray):
# a string-array, write as "name:index"
if len(org_value) == 0:
warnfunc("Warning: string-array '%s' is empty" % name, "warning")
continue
if not isinstance(trans_value, StringArray):
if trans_value:
warnfunc(
('""%s" is a string-array in the reference ' "file, but not in the translation.") % name,
"warning",
)
trans_value = StringArray()
for index, item in enumerate(org_value):
item_trans = trans_value[index].text if index < len(trans_value) else u""
# If the string has formatting markers, indicate it in
# the gettext output
flags = []
if item.formatted:
flags.append("c-format")
ctx = "%s:%d" % (name, index)
catalog.add(item.text, item_trans, auto_comments=item.comments, flags=flags, context=ctx)
elif isinstance(org_value, Plurals):
# a plurals, convert to a gettext plurals
if len(org_value) == 0:
warnfunc("Warning: plurals '%s' is empty" % name, "warning")
continue
if not isinstance(trans_value, Plurals):
if trans_value:
warnfunc(
('""%s" is a plurals in the reference ' "file, but not in the translation.") % name, "warning"
)
trans_value = Plurals()
# Taking the Translation objects for each quantity in ``org_value``,
# we build a list of strings, which is how plurals are represented
# in Babel.
#
# Since gettext only allows comments/flags on the whole
# thing at once, we merge the comments/flags of all individual
# plural strings into one.
formatted = False
comments = []
for _, translation in org_value.items():
if translation.formatted:
formatted = True
comments.extend(translation.comments)
# For the message id, choose any two plural forms, but prefer
# "one" and "other", assuming an English master resource.
temp = org_value.copy()
singular = (
temp.pop("one") if "one" in temp else temp.pop("other") if "other" in temp else temp.pop(temp.keys()[0])
)
plural = temp.pop("other") if "other" in temp else temp[temp.keys()[0]] if temp else singular
msgid = (singular.text, plural.text)
del temp, singular, plural
#.........这里部分代码省略.........