本文整理汇总了Python中babel.messages.catalog.Catalog.add方法的典型用法代码示例。如果您正苦于以下问题:Python Catalog.add方法的具体用法?Python Catalog.add怎么用?Python Catalog.add使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类babel.messages.catalog.Catalog
的用法示例。
在下文中一共展示了Catalog.add方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_no_wrap_and_width_behaviour_on_comments
# 需要导入模块: from babel.messages.catalog import Catalog [as 别名]
# 或者: from babel.messages.catalog.Catalog import add [as 别名]
def test_no_wrap_and_width_behaviour_on_comments(self):
catalog = Catalog()
catalog.add("Pretty dam long message id, which must really be big "
"to test this wrap behaviour, if not it won't work.",
locations=[("fake.py", n) for n in range(1, 30)])
buf = BytesIO()
pofile.write_po(buf, catalog, width=None, omit_header=True)
self.assertEqual(b"""\
#: fake.py:1 fake.py:2 fake.py:3 fake.py:4 fake.py:5 fake.py:6 fake.py:7
#: fake.py:8 fake.py:9 fake.py:10 fake.py:11 fake.py:12 fake.py:13 fake.py:14
#: fake.py:15 fake.py:16 fake.py:17 fake.py:18 fake.py:19 fake.py:20 fake.py:21
#: fake.py:22 fake.py:23 fake.py:24 fake.py:25 fake.py:26 fake.py:27 fake.py:28
#: fake.py:29
msgid "pretty dam long message id, which must really be big to test this wrap behaviour, if not it won't work."
msgstr ""
""", buf.getvalue().lower())
buf = BytesIO()
pofile.write_po(buf, catalog, width=100, omit_header=True)
self.assertEqual(b"""\
#: fake.py:1 fake.py:2 fake.py:3 fake.py:4 fake.py:5 fake.py:6 fake.py:7 fake.py:8 fake.py:9 fake.py:10
#: fake.py:11 fake.py:12 fake.py:13 fake.py:14 fake.py:15 fake.py:16 fake.py:17 fake.py:18 fake.py:19
#: fake.py:20 fake.py:21 fake.py:22 fake.py:23 fake.py:24 fake.py:25 fake.py:26 fake.py:27 fake.py:28
#: fake.py:29
msgid ""
"pretty dam long message id, which must really be big to test this wrap behaviour, if not it won't"
" work."
msgstr ""
""", buf.getvalue().lower())
示例2: test_write_incomplete_plural
# 需要导入模块: from babel.messages.catalog import Catalog [as 别名]
# 或者: from babel.messages.catalog.Catalog import add [as 别名]
def test_write_incomplete_plural():
"""Test behaviour with incompletely translated plurals in .po."""
catalog = Catalog()
catalog.language = Language('bs') # Bosnian
catalog.add(('foo', 'foos'), ('one', '', 'many', ''), context='foo')
assert po2xml(catalog) == {'foo': {
'few': '', 'many': 'many', 'other': '', 'one': 'one'}}
示例3: run
# 需要导入模块: from babel.messages.catalog import Catalog [as 别名]
# 或者: from babel.messages.catalog.Catalog import add [as 别名]
def run(self):
mappings = self._get_mappings()
with open(self.output_file, 'wb') as outfile:
catalog = Catalog(project=self.project,
version=self.version,
msgid_bugs_address=self.msgid_bugs_address,
copyright_holder=self.copyright_holder,
charset=self.charset)
for path, method_map, options_map in mappings:
def callback(filename, method, options):
if method == 'ignore':
return
# If we explicitly provide a full filepath, just use that.
# Otherwise, path will be the directory path and filename
# is the relative path from that dir to the file.
# So we can join those to get the full filepath.
if os.path.isfile(path):
filepath = path
else:
filepath = os.path.normpath(os.path.join(path, filename))
optstr = ''
if options:
optstr = ' (%s)' % ', '.join(['%s="%s"' % (k, v) for
k, v in options.items()])
self.log.info('extracting messages from %s%s', filepath, optstr)
if os.path.isfile(path):
current_dir = os.getcwd()
extracted = check_and_call_extract_file(
path, method_map, options_map,
callback, self.keywords, self.add_comments,
self.strip_comments, current_dir
)
else:
extracted = extract_from_dir(
path, method_map, options_map,
keywords=self.keywords,
comment_tags=self.add_comments,
callback=callback,
strip_comment_tags=self.strip_comments
)
for fname, lineno, msg, comments, context, flags in extracted:
if os.path.isfile(path):
filepath = fname # already normalized
else:
filepath = os.path.normpath(os.path.join(path, fname))
catalog.add(msg, None, [(filepath, lineno)], flags=flags,
auto_comments=comments, context=context)
self.log.info('writing PO template file to %s', self.output_file)
write_po(outfile, catalog, width=self.width,
no_location=self.no_location,
omit_header=self.omit_header,
sort_output=self.sort_output,
sort_by_file=self.sort_by_file,
include_lineno=self.include_lineno)
示例4: test_write
# 需要导入模块: from babel.messages.catalog import Catalog [as 别名]
# 或者: from babel.messages.catalog.Catalog import add [as 别名]
def test_write():
"""Test writing a basic catalog.
"""
catalog = Catalog()
catalog.add('green', context='colors:0')
catalog.add('red', context='colors:1')
assert po2xml(catalog) == {'colors': ['green', 'red']}
示例5: test_po_with_multiline_obsolete_message
# 需要导入模块: from babel.messages.catalog import Catalog [as 别名]
# 或者: from babel.messages.catalog.Catalog import add [as 别名]
def test_po_with_multiline_obsolete_message(self):
catalog = Catalog()
catalog.add(u'foo', u'Voh', locations=[('main.py', 1)])
msgid = r"""Here's a message that covers
multiple lines, and should still be handled
correctly.
"""
msgstr = r"""Here's a message that covers
multiple lines, and should still be handled
correctly.
"""
catalog.obsolete[msgid] = Message(msgid, msgstr,
locations=[('utils.py', 3)])
buf = BytesIO()
pofile.write_po(buf, catalog, omit_header=True)
self.assertEqual(b'''#: main.py:1
msgid "foo"
msgstr "Voh"
#~ msgid ""
#~ "Here's a message that covers\\n"
#~ "multiple lines, and should still be handled\\n"
#~ "correctly.\\n"
#~ msgstr ""
#~ "Here's a message that covers\\n"
#~ "multiple lines, and should still be handled\\n"
#~ "correctly.\\n"''', buf.getvalue().strip())
示例6: handle
# 需要导入模块: from babel.messages.catalog import Catalog [as 别名]
# 或者: from babel.messages.catalog.Catalog import add [as 别名]
def handle(self, *args, **options):
if args:
# mimics puente.management.commands.extract for a list of files
outputdir = os.path.join(settings.ROOT, 'locale', 'templates',
'LC_MESSAGES')
if not os.path.isdir(outputdir):
os.makedirs(outputdir)
catalog = Catalog(
header_comment='',
project=get_setting('PROJECT'),
version=get_setting('VERSION'),
msgid_bugs_address=get_setting('MSGID_BUGS_ADDRESS'),
charset='utf-8',
)
for filename, lineno, msg, cmts, ctxt in extract_from_files(args):
catalog.add(msg, None, [(filename, lineno)], auto_comments=cmts,
context=ctxt)
with open(os.path.join(outputdir, '%s.pot' % DOMAIN), 'wb') as fp:
write_po(fp, catalog, width=80)
else:
# This is basically a wrapper around the puente extract
# command, we might want to do some things around this in the
# future
gettext_extract()
pot_to_langfiles(DOMAIN)
示例7: test_write_po_file_with_specified_charset
# 需要导入模块: from babel.messages.catalog import Catalog [as 别名]
# 或者: from babel.messages.catalog.Catalog import add [as 别名]
def test_write_po_file_with_specified_charset(self):
catalog = Catalog(charset="iso-8859-1")
catalog.add("foo", "\xe4\xf6\xfc", locations=[("main.py", 1)])
buf = BytesIO()
pofile.write_po(buf, catalog, omit_header=False)
po_file = buf.getvalue().strip()
assert br'"Content-Type: text/plain; charset=iso-8859-1\n"' in po_file
assert 'msgstr "\xe4\xf6\xfc"'.encode("iso-8859-1") in po_file
示例8: test_write_po_file_with_specified_charset
# 需要导入模块: from babel.messages.catalog import Catalog [as 别名]
# 或者: from babel.messages.catalog.Catalog import add [as 别名]
def test_write_po_file_with_specified_charset(self):
catalog = Catalog(charset='iso-8859-1')
catalog.add('foo', u'äöü', locations=[('main.py', 1)])
buf = BytesIO()
pofile.write_po(buf, catalog, omit_header=False)
po_file = buf.getvalue().strip()
assert b'"Content-Type: text/plain; charset=iso-8859-1\\n"' in po_file
assert u'msgstr "äöü"'.encode('iso-8859-1') in po_file
示例9: test_write_missing_translations
# 需要导入模块: from babel.messages.catalog import Catalog [as 别名]
# 或者: from babel.messages.catalog.Catalog import add [as 别名]
def test_write_missing_translations():
"""[Regression] Specifically test that arrays are not written to the
XML in an incomplete fashion if parts of the array are not translated.
"""
catalog = Catalog()
catalog.add('green', context='colors:0') # does not have a translation
catalog.add('red', 'rot', context='colors:1')
assert po2xml(catalog) == {'colors': ['green', 'rot']}
示例10: test_file_sorted_po
# 需要导入模块: from babel.messages.catalog import Catalog [as 别名]
# 或者: from babel.messages.catalog.Catalog import add [as 别名]
def test_file_sorted_po(self):
catalog = Catalog()
catalog.add(u'bar', locations=[('utils.py', 3)])
catalog.add((u'foo', u'foos'), (u'Voh', u'Voeh'), locations=[('main.py', 1)])
buf = BytesIO()
pofile.write_po(buf, catalog, sort_by_file=True)
value = buf.getvalue().strip()
assert value.find(b'main.py') < value.find(b'utils.py')
示例11: test_write
# 需要导入模块: from babel.messages.catalog import Catalog [as 别名]
# 或者: from babel.messages.catalog.Catalog import add [as 别名]
def test_write():
"""Test writing a basic catalog.
"""
catalog = Catalog()
catalog.add('green', context='colors:0')
catalog.add('red', context='colors:1')
assert etree.tostring(po2xml(catalog, with_untranslated=True)) == \
'<resources><string-array name="colors"><item>green</item><item>red</item></string-array></resources>'
示例12: test_join_locations
# 需要导入模块: from babel.messages.catalog import Catalog [as 别名]
# 或者: from babel.messages.catalog.Catalog import add [as 别名]
def test_join_locations(self):
catalog = Catalog()
catalog.add(u'foo', locations=[('main.py', 1)])
catalog.add(u'foo', locations=[('utils.py', 3)])
buf = BytesIO()
pofile.write_po(buf, catalog, omit_header=True)
self.assertEqual(b'''#: main.py:1 utils.py:3
msgid "foo"
msgstr ""''', buf.getvalue().strip())
示例13: test_duplicate_comments
# 需要导入模块: from babel.messages.catalog import Catalog [as 别名]
# 或者: from babel.messages.catalog.Catalog import add [as 别名]
def test_duplicate_comments(self):
catalog = Catalog()
catalog.add(u'foo', auto_comments=['A comment'])
catalog.add(u'foo', auto_comments=['A comment'])
buf = BytesIO()
pofile.write_po(buf, catalog, omit_header=True)
self.assertEqual(b'''#. A comment
msgid "foo"
msgstr ""''', buf.getvalue().strip())
示例14: test_no_include_lineno
# 需要导入模块: from babel.messages.catalog import Catalog [as 别名]
# 或者: from babel.messages.catalog.Catalog import add [as 别名]
def test_no_include_lineno(self):
catalog = Catalog()
catalog.add(u'foo', locations=[('main.py', 1)])
catalog.add(u'foo', locations=[('utils.py', 3)])
buf = BytesIO()
pofile.write_po(buf, catalog, omit_header=True, include_lineno=False)
self.assertEqual(b'''#: main.py utils.py
msgid "foo"
msgstr ""''', buf.getvalue().strip())
示例15: test_write
# 需要导入模块: from babel.messages.catalog import Catalog [as 别名]
# 或者: from babel.messages.catalog.Catalog import add [as 别名]
def test_write():
"""Test writing a basic catalog.
"""
catalog = Catalog()
catalog.add("green", context="colors:0")
catalog.add("red", context="colors:1")
assert (
etree.tostring(po2xml(catalog, with_untranslated=True))
== '<resources><string-array name="colors"><item>green</item><item>red</item></string-array></resources>'
)