本文整理汇总了Python中babel.messages.Catalog类的典型用法代码示例。如果您正苦于以下问题:Python Catalog类的具体用法?Python Catalog怎么用?Python Catalog使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了Catalog类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: convert_xlsm_content
def convert_xlsm_content(contents):
catalog = Catalog()
for content in contents:
resid = content[0]
res_org_content = content[1]
rescontent = content[2]
if resid == None:
continue
if rescontent == None:
rescontent = u""
if match(r"\((.+)(::(.+))+\)", res_org_content):
msg_id = res_org_content[1:-1].split("::")
msg_string = rescontent[1:-1].split("::")
catalog.add(msg_id, context=resid, string=msg_string)
else:
if match(r"\".+\"", res_org_content):
res_org_content = res_org_content[1:-1]
if match(r"\".+\"", rescontent):
rescontent = rescontent[1:-1]
catalog.add(res_org_content, context=resid, string=rescontent)
return catalog
示例2: extract_messages
def extract_messages(dirs):
catalog = Catalog(
project='Open Library',
copyright_holder='Internet Archive'
)
METHODS = [
("**.py", "python"),
("**.html", "openlibrary.i18n:extract_templetor")
]
COMMENT_TAGS = ["NOTE:"]
for d in dirs:
if '.html' in d:
extracted = [(d,) + extract for extract in extract_from_file("openlibrary.i18n:extract_templetor", d)]
else:
extracted = extract_from_dir(d, METHODS, comment_tags=COMMENT_TAGS, strip_comment_tags=True)
for filename, lineno, message, comments, context in extracted:
catalog.add(message, None, [(filename, lineno)], auto_comments=comments)
path = os.path.join(root, 'messages.pot')
f = open(path, 'w')
write_po(f, catalog)
f.close()
print('wrote template to', path)
示例3: _extract
def _extract(self, app):
catalog = Catalog(domain="django", charset="utf8")
files = {}
for dirpath, dirnames, filenames in filtered_walk(app.path):
for filename in filenames:
filename = os.path.join(dirpath, filename)
if ACCEPTABLE_FILENAMES_RE.match(filename):
rel_filename = filename[len(os.path.commonprefix((app.path, filename))) + 1:].replace(os.sep, "/")
files[rel_filename] = filename
self.log.info("%s: %d translatable files found", app.label, len(files))
extractors = self.get_extractors()
for rel_filename, filename in sorted(files.items()):
extractor_tup = extractors.get(os.path.splitext(filename)[1])
if not extractor_tup:
self.log.warning("Not sure how to extract messages from %s", filename)
continue
extractor, options = extractor_tup
with open(filename, "rb") as fp:
for (lineno, message, comments, context) in extract(extractor, fp, options=options):
catalog.add(message, locations=[(rel_filename, 0)], auto_comments=comments)
if len(catalog):
pot_path = self._get_pot_path(app)
with open(pot_path, "w") as outf:
pofile.write_po(outf, catalog, width=1000, omit_header=True, sort_output=True)
self.log.info("%s: %d messages in %s", app.label, len(catalog), pot_path)
return catalog
示例4: test_translation_plugins
def test_translation_plugins(app, tmpdir):
session.lang = 'fr_FR'
plugin = MockPlugin(plugin_engine, app)
app.extensions['pluginengine'].plugins['dummy'] = plugin
plugin.root_path = tmpdir.strpath
french_core_str = DICTIONARIES['fr_FR']['This is not a string']
french_plugin_str = "This is not le french string"
trans_dir = os.path.join(plugin.root_path, 'translations', 'fr_FR', 'LC_MESSAGES')
os.makedirs(trans_dir)
# Create proper *.mo file for plugin translation
with open(os.path.join(trans_dir, 'messages.mo'), 'wb') as f:
catalog = Catalog(locale='fr_FR', domain='plugin')
catalog.add("This is not a string", "This is not le french string")
write_mo(f, catalog)
gettext_plugin = make_bound_gettext('dummy')
assert _(u'This is not a string') == french_core_str
assert gettext_context(u"This is not a string") == french_core_str
assert gettext_plugin(u"This is not a string") == french_plugin_str
with plugin.plugin_context():
assert _(u'This is not a string') == french_core_str
assert gettext_context(u"This is not a string") == french_plugin_str
assert gettext_plugin(u"This is not a string") == french_plugin_str
示例5: assert_convert
def assert_convert(cls, po, xml=None, namespaces={}):
"""Helper that passes the string in ``po`` through our po
to xml converter, and checks the resulting xml string value
against ``xml``.
If ``xml`` is not given, we check against ``po`` instead, i.e.
expect the string to remain unchanged.
"""
key = 'test'
catalog = Catalog()
catalog.add(po, po, context=key)
warnfunc = TestWarnFunc()
dom = write_xml(po2xml(catalog, warnfunc=warnfunc), warnfunc=warnfunc)
elem = dom.xpath('/resources/string[@name="%s"]' % key)[0]
elem_as_text = etree.tostring(elem, encoding=unicode)
value = re.match("^[^>]+>(.*)<[^<]+$", elem_as_text).groups(1)[0]
match = xml if xml is not None else po
print "'%s' == '%s'" % (value, match)
print repr(value), '==', repr(match)
assert value == match
# If ``namespaces`` are set, the test expects those to be defined
# in the root of the document
for prefix, uri in namespaces.items():
assert dom.nsmap[prefix] == uri
# In this case, the reverse (converting back to po) always needs to
# give us the original again, so this allows for a nice extra check.
if not match: # Skip this if we are doing a special, custom match.
TestFromXML.assert_convert(match, po, namespaces)
return warnfunc
示例6: test
def test(self):
p = self.setup_project()
c = Catalog(locale='de')
c.add('en1', 'de1', flags=('fuzzy',), context='foo')
c.add('en2', 'de2', context='bar')
p.write_po(c, 'de.po')
p.program('import', {'--ignore-fuzzy': True})
xml = p.get_xml('de')
assert not 'foo' in xml
assert 'bar' in xml
示例7: mkcatalog
def mkcatalog(locale='de'):
"""Helper that returns a gettext catalog with one message
already added.
Tests can add a broken message and then ensure that at least
the valid message still was processed.
"""
c = Catalog(locale='de')
c.add('valid_message', 'valid_value', context='valid_message')
return c
示例8: test_invalid_xhtml
def test_invalid_xhtml():
"""Ensure we can deal with broken XML in messages.
"""
c = Catalog()
c.add('Foo', '<i>Tag is not closed', context="foo")
# [bug] This caused an exception in 16263b.
dom = write_xml(po2xml(c))
# The tag was closed automatically (our loose parser tries to fix errors).
assert etree.tostring(dom) == b'<resources><string name="foo"><i>Tag is not closed</i></string></resources>'
示例9: test_load
def test_load(self):
tempdir = tempfile.mkdtemp()
try:
messages_dir = os.path.join(tempdir, 'fr', 'LC_MESSAGES')
os.makedirs(messages_dir)
catalog = Catalog(locale='fr', domain='messages')
catalog.add('foo', 'bar')
write_mo(file(os.path.join(messages_dir, 'messages.mo'), 'wb'), catalog)
translations = support.Translations.load(tempdir, locales=('fr',), domain='messages')
self.assertEqual('bar', translations.gettext('foo'))
finally:
shutil.rmtree(tempdir)
示例10: test_load
def test_load(self):
tempdir = tempfile.mkdtemp()
try:
messages_dir = os.path.join(tempdir, "fr", "LC_MESSAGES")
os.makedirs(messages_dir)
catalog = Catalog(locale="fr", domain="messages")
catalog.add("foo", "bar")
with open(os.path.join(messages_dir, "messages.mo"), "wb") as f:
write_mo(f, catalog)
translations = support.Translations.load(tempdir, locales=("fr",), domain="messages")
self.assertEqual("bar", translations.gettext("foo"))
finally:
shutil.rmtree(tempdir)
示例11: _create_catalog
def _create_catalog(self, path_id, domain=u'foo', locale=u'de', **messages):
i18n_dir = os.path.join(self._tempdir, path_id)
path = os.path.join(i18n_dir, locale, 'LC_MESSAGES')
os.makedirs(path)
mo_filename = os.path.join(path, '%s.mo' % domain)
assert_false(os.path.exists(mo_filename))
catalog = Catalog(locale=locale, domain=domain, fuzzy=False)
for message_id, translation in messages.items():
catalog.add(message_id, translation)
mo_fp = file(mo_filename, 'wb')
write_mo(mo_fp, catalog)
mo_fp.close()
return i18n_dir
示例12: test_untranslated
def test_untranslated():
"""Test that by default, untranslated strings are not included in the
imported XML.
"""
catalog = Catalog()
catalog.add('green', context='color1')
catalog.add('red', 'rot', context='color2')
assert po2xml(catalog) == {'color2': 'rot'}
# If with_untranslated is passed, then all strings are included.
# Note that arrays behave differently (they always include all
# strings), and this is tested in test_string_arrays.py).
assert po2xml(catalog, with_untranslated=True) ==\
{'color1': 'green', 'color2': 'rot'}
示例13: _update
def _update(self, app, template_catalog, langs):
if template_catalog is None:
with open(_get_pot_path(app)) as infp:
template_catalog = pofile.read_po(infp, charset="utf8")
for lang in langs:
po_path = _get_po_path(app, language=lang)
if os.path.isfile(po_path):
with open(po_path) as infp:
lang_catalog = pofile.read_po(infp, charset="utf8")
else:
lang_catalog = Catalog(locale=lang, charset="utf8")
lang_catalog.update(template_catalog)
if len(lang_catalog):
with open(po_path, "w") as outf:
pofile.write_po(outf, lang_catalog, width=1000, omit_header=True, sort_output=True)
self.log.info("%s: updated %s", app.label, po_path)
示例14: test_export
def test_export(self):
"""Test that the export command maintains the proper plural form,
and actually replaces an incorrect one."""
p = self.setup_project()
p.write_xml(data="""<resources></resources>""")
p.write_xml(data="""<resources></resources>""", lang='ja')
# Generate a catalog with different plural rules than we expect
catalog = Catalog('ja')
catalog._num_plurals, catalog._plural_expr = 2, '(n < 2)'
p.write_po(catalog)
# Export should override the info
assert 'Plural-Forms header' in p.program('export')
catalog = p.get_po('ja.po')
assert catalog.num_plurals == 1
assert catalog.plural_expr == '(0)'
示例15: test_update
def test_update(self):
template = Catalog()
template.add("1")
template.add("2")
template.add("3")
tmpl_file = os.path.join(self._i18n_dir(), "temp-template.pot")
with open(tmpl_file, "wb") as outfp:
write_po(outfp, template)
po_file = os.path.join(self._i18n_dir(), "temp1.po")
self.cli.run(sys.argv + ["init", "-l", "fi", "-o", po_file, "-i", tmpl_file])
with open(po_file, "r") as infp:
catalog = read_po(infp)
assert len(catalog) == 3
# Add another entry to the template
template.add("4")
with open(tmpl_file, "wb") as outfp:
write_po(outfp, template)
self.cli.run(sys.argv + ["update", "-l", "fi_FI", "-o", po_file, "-i", tmpl_file])
with open(po_file, "r") as infp:
catalog = read_po(infp)
assert len(catalog) == 4 # Catalog was updated