def test_extract_default_encoding_utf8(self): buf = BytesIO(u'_("☃")'.encode('UTF-8')) messages = list(extract.extract_python( buf, list(extract.DEFAULT_KEYWORDS), [], {}, )) self.assertEqual([(1, '_', u'☃', [])], messages)
def test_parses_django(self): buf = BytesIO(b'{% trans "Bunny" %}') messages = list(extract(buf, self.keywords, [], {})) assert messages == ([(1, None, 'Bunny', [])])
def test_parses_underscore(self): buf = BytesIO(b'hello: <%= name %>') messages = list(extract(buf, self.keywords, [], {})) assert messages == []
def test_more_plural_forms(self): catalog2 = Catalog(locale='ru_RU') catalog2.add(('Fuzz', 'Fuzzes'), ('', '', '')) buf = BytesIO() mofile.write_mo(buf, catalog2)
except KeyError: assert True class FixedOffsetTimezoneTestCase(unittest.TestCase): def test_zone_negative_offset(self): self.assertEqual('Etc/GMT-60', util.FixedOffsetTimezone(-60).zone) def test_zone_zero_offset(self): self.assertEqual('Etc/GMT+0', util.FixedOffsetTimezone(0).zone) def test_zone_positive_offset(self): self.assertEqual('Etc/GMT+330', util.FixedOffsetTimezone(330).zone) parse_encoding = lambda s: util.parse_encoding(BytesIO(s.encode('utf-8'))) def test_parse_encoding_defined(): assert parse_encoding(u'# coding: utf-8') == 'utf-8' def test_parse_encoding_undefined(): assert parse_encoding(u'') is None def test_parse_encoding_non_ascii(): assert parse_encoding(u'K\xf6ln') is None @pytest.mark.parametrize('source, result', [
def test_extract_var(self): buf = BytesIO(b'{{ book }}') messages = list(extract_django(buf, default_keys, [], {})) self.assertEqual([], messages)
def test_template_string_standard_usage(): buf = BytesIO(b"msg1 = gettext(`Very template, wow`)") messages = list( extract.extract('javascript', buf, {"gettext": None}, [], {})) assert messages == [(1, 'Very template, wow', [], None)]
def test_extract_constant_single_quotes(self): buf = BytesIO(b"{{ _('constant') }}") messages = list(extract_django(buf, default_keys, [], {})) self.assertEqual([(1, None, u"'constant'", [])], messages)
def test_extract_constant_single_quotes(self): buf = BytesIO(b'{{ _("constant") }}') messages = list(extract_django(buf, default_keys, [], {})) self.assertEqual([(1, None, u'"constant"', [])], messages)
def setUp(self): fp = BytesIO() write_mo(fp, Catalog(locale='de')) fp.seek(0) self.translations = support.Translations(fp=fp) self.null_translations = support.NullTranslations(fp=fp)
def test_extract_filters_default_translatable_single_quotes(self): buf = BytesIO(b"{{ book.author|default:_('Unknown') }}") messages = list(extract_django(buf, default_keys, [], {})) self.assertEqual([(1, None, u'Unknown', [])], messages)
def test_extract_unicode(self): buf = BytesIO( u'<%= gettext("@ſðæ314“ſſ¶ÐĐÞ→SÆ^ĸŁ") %>'.encode('utf-8')) messages = list(extract(buf, self.keywords, [], {})) assert messages == [(1, 'gettext', u'@ſðæ314“ſſ¶ÐĐÞ→SÆ^ĸŁ', [])]
def test_invalid_extract_method(self): buf = BytesIO(b'') self.assertRaises(ValueError, list, extract.extract('spam', buf))
def test_unicode_string_arg(self): buf = BytesIO(b"msg = _(u'Foo Bar')") messages = list(extract.extract_python(buf, ('_', ), [], {})) self.assertEqual(u'Foo Bar', messages[0][2])
def test_extract_ignored_comment(self): buf = BytesIO( b'{# ignored comment #1 #}{% trans "Translatable literal #9a" %}') messages = list(extract_django(buf, default_keys, [], {})) self.assertEqual([(1, None, u'Translatable literal #9a', [])], messages)
def test_extract_constant_block(self): buf = BytesIO(b'{% _("constant") %}') messages = list(extract_django(buf, default_keys, [], {})) self.assertEqual([(1, None, u'"constant"', [])], messages)
def test_trans_blocks_must_not_include_other_block_tags(self): buf = BytesIO(b'{% blocktrans %}{% other_tag %}{% endblocktrans %}') gen = extract_django(buf, default_keys, [], {}) pytest.raises(SyntaxError, gen.next)
def test_extract_constant_in_block(self): buf = BytesIO( b'{% blocktrans foo=_("constant") %}{{ foo }}{% endblocktrans %}') messages = list(extract_django(buf, default_keys, [], {})) self.assertEqual([(1, None, u'"constant"', []), (1, None, u'%(foo)s', [])], messages)
def test_2_num_plurals_checkers(self): # in this testcase we add an extra msgstr[idx], we should be # disregarding it for _locale in [p for p in PLURALS if PLURALS[p][0] == 2]: if _locale in ['nn', 'no']: _locale = 'nn_NO' num_plurals = PLURALS[_locale.split('_')[0]][0] plural_expr = PLURALS[_locale.split('_')[0]][1] else: num_plurals = PLURALS[_locale][0] plural_expr = PLURALS[_locale][1] try: locale = Locale(_locale) date = format_datetime(datetime.now(LOCALTZ), 'yyyy-MM-dd HH:mmZ', tzinfo=LOCALTZ, locale=_locale) except UnknownLocaleError: # Just an alias? Not what we're testing here, let's continue continue po_file = (u"""\ # %(english_name)s translations for TestProject. # Copyright (C) 2007 FooBar, Inc. # This file is distributed under the same license as the TestProject # project. # FIRST AUTHOR <EMAIL@ADDRESS>, 2007. # msgid "" msgstr "" "Project-Id-Version: TestProject 0.1\\n" "Report-Msgid-Bugs-To: [email protected]\\n" "POT-Creation-Date: 2007-04-01 15:30+0200\\n" "PO-Revision-Date: %(date)s\\n" "Last-Translator: FULL NAME <EMAIL@ADDRESS>\\n" "Language-Team: %(locale)s <*****@*****.**>\\n" "Plural-Forms: nplurals=%(num_plurals)s; plural=%(plural_expr)s\\n" "MIME-Version: 1.0\\n" "Content-Type: text/plain; charset=utf-8\\n" "Content-Transfer-Encoding: 8bit\\n" "Generated-By: Babel %(version)s\\n" #. This will be a translator comment, #. that will include several lines #: project/file1.py:8 msgid "bar" msgstr "" #: project/file2.py:9 msgid "foobar" msgid_plural "foobars" msgstr[0] "" msgstr[1] "" msgstr[2] "" """ % dict(locale=_locale, english_name=locale.english_name, version=VERSION, year=time.strftime('%Y'), date=date, num_plurals=num_plurals, plural_expr=plural_expr)).encode('utf-8') # we should be adding the missing msgstr[0] # This test will fail for revisions <= 406 because so far # catalog.num_plurals was neglected catalog = read_po(BytesIO(po_file), _locale) message = catalog['foobar'] checkers.num_plurals(catalog, message)
def test_extract_no_tags(self): buf = BytesIO(b'nothing') messages = list(extract_django(buf, default_keys, [], {})) self.assertEqual([], messages)
def test_template_string_tag_usage(): buf = BytesIO(b"function() { if(foo) msg1 = i18n`Tag template, wow`; }") messages = list(extract.extract('javascript', buf, {"i18n": None}, [], {})) assert messages == [(1, 'Tag template, wow', [], None)]
def test_extract_simple_single_quotes(self): buf = BytesIO(b"{% trans 'Bunny' %}") messages = list(extract_django(buf, default_keys, [], {})) self.assertEqual([(1, None, u'Bunny', [])], messages)
#(filename, lineno, message, comments, context) ('main.py', 9, 'MSG000', [], None) ('sub.py', 1, 'MSG000', [], None) ('mypackage/mymodule.py', 1, 'Good Luck !', [], None) """ #翻訳データ(Catalog)を作成する import babel.messages.catalog catalog = babel.messages.catalog.Catalog(locale='ja', domain='hello') c_list = list(babel.messages.extract.extract_from_dir(dirname='../../src/')) for c_tpl in c_list: catalog.add(c_tpl[2], string='翻訳後テキスト', locations=[(c_t[0], c_t[1]) for c_t in c_list if c_tpl[2]==c_t[2]]) #poファイル書き出し from babel._compat import BytesIO buf = BytesIO() import babel.messages.pofile babel.messages.pofile.write_po(buf, catalog) #babel.messages.pofile.write_po(buf, catalog, omit_header=True) print(buf.getvalue().decode("utf8")) """ #: main.py:9 sub.py:1 msgid "MSG000" msgstr "翻訳後テキスト" #: mypackage/mymodule.py:1 msgid "Good Luck !" msgstr "翻訳後テキスト" """
def test_extract_var(self): buf = BytesIO(b'{% blocktrans %}{{ anton }}{% endblocktrans %}') messages = list(extract_django(buf, default_keys, [], {})) self.assertEqual([(1, None, u'%(anton)s', [])], messages)
def test_parse_future(source, result): fp = BytesIO(source.encode('latin-1')) flags = parse_future_flags(fp) assert flags == result
def test_extract_filter_with_filter(self): buf = BytesIO( b'{% blocktrans with berta=anton|lower %}{{ berta }}{% endblocktrans %}' ) messages = list(extract_django(buf, default_keys, [], {})) self.assertEqual([(1, None, u'%(berta)s', [])], messages)
def test_parses_blocktrans(self): buf = BytesIO(b'Ignored {% blocktrans %}{{ anton }}{% endblocktrans %}') messages = list(extract(buf, self.keywords, [], {})) assert messages == [(1, None, '%(anton)s', [])]
def test_extract_with_interpolation(self): buf = BytesIO(b'{% blocktrans %}xxx{{ anton }}xxx{% endblocktrans %}') messages = list(extract_django(buf, default_keys, [], {})) self.assertEqual([(1, None, u'xxx%(anton)sxxx', [])], messages)
def test_parses_underscore_gettext(self): buf = BytesIO(b'hello: <%= gettext("name") %>') messages = list(extract(buf, self.keywords, [], {})) assert messages == [(1, 'gettext', 'name', [])]
def test_extract_allows_callable(self): def arbitrary_extractor(fileobj, keywords, comment_tags, options): return [(1, None, (), ())] for x in extract.extract(arbitrary_extractor, BytesIO(b"")): assert x[0] == 1