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(r'''#: 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())
def test_sorted_po(self): catalog = Catalog() catalog.add( u('bar'), locations=[('utils.py', 3)], user_comments=['Comment About `bar` with', 'multiple lines.']) catalog.add((u('foo'), u('foos')), (u('Voh'), u('Voeh')), locations=[('main.py', 1)]) buf = BytesIO() pofile.write_po(buf, catalog, sort_output=True) value = buf.getvalue().strip() assert b('''\ # Comment About `bar` with # multiple lines. #: utils.py:3 msgid "bar" msgstr "" #: main.py:1 msgid "foo" msgid_plural "foos" msgstr[0] "Voh" msgstr[1] "Voeh"''') in value assert value.find(b('msgid ""')) < value.find( b('msgid "bar"')) < value.find(b('msgid "foo"'))
def test_misplaced_comments(self): buf = StringIO("""\ /* NOTE: this won't show up */ foo() /* NOTE: this will */ msg = _('Something') // NOTE: this will show up // too. msg = _('Something else') // NOTE: but this won't bar() _('no comment here') """) messages = list(extract.extract_javascript(buf, ('_', ), ['NOTE:'], {})) self.assertEqual(u('Something'), messages[0][2]) self.assertEqual([u('NOTE: this will')], messages[0][3]) self.assertEqual(u('Something else'), messages[1][2]) self.assertEqual([u('NOTE: this will show up'), 'too.'], messages[1][3]) self.assertEqual(u('no comment here'), messages[2][2]) self.assertEqual([], messages[2][3])
def test_header_entry(self): buf = StringIO(r'''\ # SOME DESCRIPTIVE TITLE. # Copyright (C) 2007 THE PACKAGE'S COPYRIGHT HOLDER # This file is distributed under the same license as the PACKAGE package. # FIRST AUTHOR <EMAIL@ADDRESS>, 2007. # #, fuzzy msgid "" msgstr "" "Project-Id-Version: 3.15\n" "Report-Msgid-Bugs-To: Fliegender Zirkus <*****@*****.**>\n" "POT-Creation-Date: 2007-09-27 11:19+0700\n" "PO-Revision-Date: 2007-09-27 21:42-0700\n" "Last-Translator: John <*****@*****.**>\n" "Language-Team: German Lang <*****@*****.**>\n" "Plural-Forms: nplurals=2; plural=(n != 1)\n" "MIME-Version: 1.0\n" "Content-Type: text/plain; charset=iso-8859-2\n" "Content-Transfer-Encoding: 8bit\n" "Generated-By: Babel 1.0dev-r313\n" ''') catalog = pofile.read_po(buf) self.assertEqual(1, len(list(catalog))) self.assertEqual(u('3.15'), catalog.version) self.assertEqual(u('Fliegender Zirkus <*****@*****.**>'), catalog.msgid_bugs_address) self.assertEqual(datetime(2007, 9, 27, 11, 19, tzinfo=FixedOffsetTimezone(7 * 60)), catalog.creation_date) self.assertEqual(u('John <*****@*****.**>'), catalog.last_translator) self.assertEqual(u('German Lang <*****@*****.**>'), catalog.language_team) self.assertEqual(u('iso-8859-2'), catalog.charset) self.assertEqual(True, list(catalog)[0].fuzzy)
def _merge(message, oldkey, newkey): message = message.clone() fuzzy = False if oldkey != newkey: fuzzy = True fuzzy_matches.add(oldkey) oldmsg = messages.get(oldkey) if isinstance(oldmsg.id, string_types): message.previous_id = [oldmsg.id] else: message.previous_id = list(oldmsg.id) else: oldmsg = remaining.pop(oldkey, None) message.string = oldmsg.string if isinstance(message.id, (list, tuple)): if not isinstance(message.string, (list, tuple)): fuzzy = True message.string = tuple( [message.string] + ([u('')] * (len(message.id) - 1)) ) elif len(message.string) != self.num_plurals: fuzzy = True message.string = tuple(message.string[:len(oldmsg.string)]) elif isinstance(message.string, (list, tuple)): fuzzy = True message.string = message.string[0] message.flags |= oldmsg.flags if fuzzy: message.flags |= set([u('fuzzy')]) self[message.id] = message
def _process_message_line(lineno, line): if line.startswith('msgid_plural'): in_msgid[0] = True msg = line[12:].lstrip() messages.append(msg) elif line.startswith('msgid'): in_msgid[0] = True offset[0] = lineno txt = line[5:].lstrip() if messages: _add_message() messages.append(txt) elif line.startswith('msgstr'): in_msgid[0] = False in_msgstr[0] = True msg = line[6:].lstrip() if msg.startswith('['): idx, msg = msg[1:].split(']', 1) translations.append([int(idx), msg.lstrip()]) else: translations.append([0, msg]) elif line.startswith('msgctxt'): if messages: _add_message() in_msgid[0] = in_msgstr[0] = False context.append(line[7:].lstrip()) elif line.startswith('"'): if in_msgid[0]: messages[-1] += u('\n') + line.rstrip() elif in_msgstr[0]: translations[-1][1] += u('\n') + line.rstrip() elif in_msgctxt[0]: context.append(line.rstrip())
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(r'''#: 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())
def _merge(message, oldkey, newkey): message = message.clone() fuzzy = False if oldkey != newkey: fuzzy = True fuzzy_matches.add(oldkey) oldmsg = messages.get(oldkey) if isinstance(oldmsg.id, string_types): message.previous_id = [oldmsg.id] else: message.previous_id = list(oldmsg.id) else: oldmsg = remaining.pop(oldkey, None) message.string = oldmsg.string if isinstance(message.id, (list, tuple)): if not isinstance(message.string, (list, tuple)): fuzzy = True message.string = tuple([message.string] + ([u('')] * (len(message.id) - 1))) elif len(message.string) != self.num_plurals: fuzzy = True message.string = tuple(message.string[:len(oldmsg.string)]) elif isinstance(message.string, (list, tuple)): fuzzy = True message.string = message.string[0] message.flags |= oldmsg.flags if fuzzy: message.flags |= set([u('fuzzy')]) self[message.id] = message
def apply(self, value, locale, currency=None): value *= self.scale is_negative = int(value < 0) if self.exp_prec: # Scientific notation value = abs(value) if value: exp = int(math.floor(math.log(value, 10))) else: exp = 0 # Minimum number of integer digits if self.int_prec[0] == self.int_prec[1]: exp -= self.int_prec[0] - 1 # Exponent grouping elif self.int_prec[1]: exp = int(exp) // self.int_prec[1] * self.int_prec[1] if not have_decimal or not isinstance(value, Decimal): value = float(value) if exp < 0: value = value * 10**(-exp) else: value = value / 10**exp exp_sign = '' if exp < 0: exp_sign = get_minus_sign_symbol(locale) elif self.exp_plus: exp_sign = get_plus_sign_symbol(locale) exp = abs(exp) number = u('%s%s%s%s') % \ (self._format_sigdig(value, self.frac_prec[0], self.frac_prec[1]), get_exponential_symbol(locale), exp_sign, self._format_int(str(exp), self.exp_prec[0], self.exp_prec[1], locale)) elif '@' in self.pattern: # Is it a siginificant digits pattern? text = self._format_sigdig(abs(value), self.int_prec[0], self.int_prec[1]) if '.' in text: a, b = text.split('.') a = self._format_int(a, 0, 1000, locale) if b: b = get_decimal_symbol(locale) + b number = a + b else: number = self._format_int(text, 0, 1000, locale) else: # A normal number pattern a, b = split_number(bankersround(abs(value), self.frac_prec[1])) b = b or '0' a = self._format_int(a, self.int_prec[0], self.int_prec[1], locale) b = self._format_frac(b, locale) number = a + b retval = u('%s%s%s') % (self.prefix[is_negative], number, self.suffix[is_negative]) if u('\xa4') in retval: retval = retval.replace(u('\xa4\xa4'), currency.upper()) retval = retval.replace(u('\xa4'), get_currency_symbol(currency, locale)) return retval
def test_message_with_line_comment(self): buf = StringIO("""\ // NOTE: hello msg = _('Bonjour à tous') """) messages = list(extract.extract_javascript(buf, ('_',), ['NOTE:'], {})) self.assertEqual(u('Bonjour \xe0 tous'), messages[0][2]) self.assertEqual([u('NOTE: hello')], messages[0][3])
def test_utf8_raw_strings_match_unicode_strings(self): buf = BytesIO(codecs.BOM_UTF8 + u(""" msg = _('Bonjour \xe0 tous') msgu = _(u'Bonjour \xe0 tous') """).encode('utf-8')) messages = list(extract.extract_python(buf, ('_',), ['NOTE:'], {})) self.assertEqual(u('Bonjour \xe0 tous'), messages[0][2]) self.assertEqual(messages[0][2], messages[1][2])
def test_utf8_message_with_utf8_bom(self): buf = BytesIO(codecs.BOM_UTF8 + u(""" # NOTE: hello msg = _('Bonjour \xe0 tous') """).encode('utf-8')) messages = list(extract.extract_python(buf, ('_',), ['NOTE:'], {})) self.assertEqual(u('Bonjour \xe0 tous'), messages[0][2]) self.assertEqual([u('NOTE: hello')], messages[0][3])
def test_utf8_message_with_magic_comment(self): buf = StringIO("""# -*- coding: utf-8 -*- # NOTE: hello msg = _('Bonjour à tous') """) messages = list(extract.extract_python(buf, ('_',), ['NOTE:'], {})) self.assertEqual(u('Bonjour \xe0 tous'), messages[0][2]) self.assertEqual([u('NOTE: hello')], messages[0][3])
def test_comment_tag(self): buf = StringIO(""" # NOTE: A translation comment msg = _(u'Foo Bar') """) messages = list(extract.extract_python(buf, ('_',), ['NOTE:'], {})) self.assertEqual(u('Foo Bar'), messages[0][2]) self.assertEqual([u('NOTE: A translation comment')], messages[0][3])
def test_update_message_changed_to_simple(self): cat = catalog.Catalog() cat.add((u('foofoos')), (u('Voh'), u('V\xf6hs'))) tmpl = catalog.Catalog() tmpl.add(u('foo')) cat.update(tmpl) self.assertEqual(u('Voh'), cat['foo'].string) assert cat['foo'].fuzzy
def test_utf8_message_with_utf8_bom(self): buf = BytesIO(codecs.BOM_UTF8 + u(""" # NOTE: hello msg = _('Bonjour \xe0 tous') """).encode('utf-8')) messages = list(extract.extract_python(buf, ('_', ), ['NOTE:'], {})) self.assertEqual(u('Bonjour \xe0 tous'), messages[0][2]) self.assertEqual([u('NOTE: hello')], messages[0][3])
def test_write_po_file_with_specified_charset(self): catalog = Catalog(charset='iso-8859-1') catalog.add('foo', u('\xe4\xf6\xfc'), locations=[('main.py', 1)]) buf = BytesIO() pofile.write_po(buf, catalog, omit_header=False) po_file = buf.getvalue().strip() assert b(r'"Content-Type: text/plain; charset=iso-8859-1\n"') in po_file assert u('msgstr "\xe4\xf6\xfc"').encode('iso-8859-1') in po_file
def test_utf8_raw_strings_match_unicode_strings(self): buf = BytesIO(codecs.BOM_UTF8 + u(""" msg = _('Bonjour \xe0 tous') msgu = _(u'Bonjour \xe0 tous') """).encode('utf-8')) messages = list(extract.extract_python(buf, ('_', ), ['NOTE:'], {})) self.assertEqual(u('Bonjour \xe0 tous'), messages[0][2]) self.assertEqual(messages[0][2], messages[1][2])
def test_comment_tag(self): buf = StringIO(""" # NOTE: A translation comment msg = _(u'Foo Bar') """) messages = list(extract.extract_python(buf, ('_', ), ['NOTE:'], {})) self.assertEqual(u('Foo Bar'), messages[0][2]) self.assertEqual([u('NOTE: A translation comment')], messages[0][3])
def test_update_message_changed_to_plural(self): cat = catalog.Catalog() cat.add(u('foo'), u('Voh')) tmpl = catalog.Catalog() tmpl.add((u('foo'), u('foos'))) cat.update(tmpl) self.assertEqual((u('Voh'), ''), cat['foo'].string) assert cat['foo'].fuzzy
def test_utf8_message_with_magic_comment(self): buf = StringIO("""# -*- coding: utf-8 -*- # NOTE: hello msg = _('Bonjour à tous') """) messages = list(extract.extract_python(buf, ('_', ), ['NOTE:'], {})) self.assertEqual(u('Bonjour \xe0 tous'), messages[0][2]) self.assertEqual([u('NOTE: hello')], messages[0][3])
def test_message_with_line_comment(self): buf = StringIO("""\ // NOTE: hello msg = _('Bonjour à tous') """) messages = list(extract.extract_javascript(buf, ('_', ), ['NOTE:'], {})) self.assertEqual(u('Bonjour \xe0 tous'), messages[0][2]) self.assertEqual([u('NOTE: hello')], messages[0][3])
def test_two_succeeding_comments(self): buf = StringIO(""" # NOTE: one # NOTE: two msg = _(u'Foo Bar') """) messages = list(extract.extract_python(buf, ('_',), ['NOTE:'], {})) self.assertEqual(u('Foo Bar'), messages[0][2]) self.assertEqual([u('NOTE: one'), u('NOTE: two')], messages[0][3])
def test_write_po_file_with_specified_charset(self): catalog = Catalog(charset='iso-8859-1') catalog.add('foo', u('\xe4\xf6\xfc'), locations=[('main.py', 1)]) buf = BytesIO() pofile.write_po(buf, catalog, omit_header=False) po_file = buf.getvalue().strip() assert b( r'"Content-Type: text/plain; charset=iso-8859-1\n"') in po_file assert u('msgstr "\xe4\xf6\xfc"').encode('iso-8859-1') in po_file
def test_utf8_message(self): buf = StringIO(""" # NOTE: hello msg = _('Bonjour à tous') """) messages = list(extract.extract_python(buf, ('_',), ['NOTE:'], {'encoding': 'utf-8'})) self.assertEqual(u('Bonjour \xe0 tous'), messages[0][2]) self.assertEqual([u('NOTE: hello')], messages[0][3])
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())
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())
def test_two_succeeding_comments(self): buf = StringIO(""" # NOTE: one # NOTE: two msg = _(u'Foo Bar') """) messages = list(extract.extract_python(buf, ('_', ), ['NOTE:'], {})) self.assertEqual(u('Foo Bar'), messages[0][2]) self.assertEqual([u('NOTE: one'), u('NOTE: two')], messages[0][3])
def test_nested_comments(self): buf = StringIO("""\ msg = ngettext('pylon', # TRANSLATORS: shouldn't be 'pylons', # TRANSLATORS: seeing this count) """) messages = list( extract.extract_python(buf, ('ngettext', ), ['TRANSLATORS:'], {})) self.assertEqual([(1, 'ngettext', (u('pylon'), u('pylons'), None), [])], messages)
def test_nested_comments(self): buf = StringIO("""\ msg = ngettext('pylon', # TRANSLATORS: shouldn't be 'pylons', # TRANSLATORS: seeing this count) """) messages = list(extract.extract_python(buf, ('ngettext',), ['TRANSLATORS:'], {})) self.assertEqual([(1, 'ngettext', (u('pylon'), u('pylons'), None), [])], messages)
def test_comment_tag_with_leading_space(self): buf = StringIO(""" #: A translation comment #: with leading spaces msg = _(u'Foo Bar') """) messages = list(extract.extract_python(buf, ('_',), [':'], {})) self.assertEqual(u('Foo Bar'), messages[0][2]) self.assertEqual([u(': A translation comment'), u(': with leading spaces')], messages[0][3])
def test_po_with_previous_msgid(self): catalog = Catalog() catalog.add(u('foo'), u('Voh'), locations=[('main.py', 1)], previous_id=u('fo')) buf = BytesIO() pofile.write_po(buf, catalog, omit_header=True, include_previous=True) self.assertEqual(b('''#: main.py:1 #| msgid "fo" msgid "foo" msgstr "Voh"'''), buf.getvalue().strip())
def test_utf8_message(self): buf = StringIO(""" # NOTE: hello msg = _('Bonjour à tous') """) messages = list( extract.extract_python(buf, ('_', ), ['NOTE:'], {'encoding': 'utf-8'})) self.assertEqual(u('Bonjour \xe0 tous'), messages[0][2]) self.assertEqual([u('NOTE: hello')], messages[0][3])
def test_nested_calls(self): buf = StringIO("""\ msg1 = _(i18n_arg.replace(r'\"', '"')) msg2 = ungettext(i18n_arg.replace(r'\"', '"'), multi_arg.replace(r'\"', '"'), 2) msg3 = ungettext("Babel", multi_arg.replace(r'\"', '"'), 2) msg4 = ungettext(i18n_arg.replace(r'\"', '"'), "Babels", 2) msg5 = ungettext('bunny', 'bunnies', random.randint(1, 2)) msg6 = ungettext(arg0, 'bunnies', random.randint(1, 2)) msg7 = _(hello.there) msg8 = gettext('Rabbit') msg9 = dgettext('wiki', model.addPage()) msg10 = dngettext(getDomain(), 'Page', 'Pages', 3) """) messages = list( extract.extract_python(buf, extract.DEFAULT_KEYWORDS.keys(), [], {})) self.assertEqual([(1, '_', None, []), (2, 'ungettext', (None, None, None), []), (3, 'ungettext', (u('Babel'), None, None), []), (4, 'ungettext', (None, u('Babels'), None), []), (5, 'ungettext', (u('bunny'), u('bunnies'), None), []), (6, 'ungettext', (None, u('bunnies'), None), []), (7, '_', None, []), (8, 'gettext', u('Rabbit'), []), (9, 'dgettext', (u('wiki'), None), []), (10, 'dngettext', (None, u('Page'), u('Pages'), None), [])], messages)
def test_nested_calls(self): buf = StringIO("""\ msg1 = _(i18n_arg.replace(r'\"', '"')) msg2 = ungettext(i18n_arg.replace(r'\"', '"'), multi_arg.replace(r'\"', '"'), 2) msg3 = ungettext("Babel", multi_arg.replace(r'\"', '"'), 2) msg4 = ungettext(i18n_arg.replace(r'\"', '"'), "Babels", 2) msg5 = ungettext('bunny', 'bunnies', random.randint(1, 2)) msg6 = ungettext(arg0, 'bunnies', random.randint(1, 2)) msg7 = _(hello.there) msg8 = gettext('Rabbit') msg9 = dgettext('wiki', model.addPage()) msg10 = dngettext(getDomain(), 'Page', 'Pages', 3) """) messages = list(extract.extract_python(buf, extract.DEFAULT_KEYWORDS.keys(), [], {})) self.assertEqual([ (1, '_', None, []), (2, 'ungettext', (None, None, None), []), (3, 'ungettext', (u('Babel'), None, None), []), (4, 'ungettext', (None, u('Babels'), None), []), (5, 'ungettext', (u('bunny'), u('bunnies'), None), []), (6, 'ungettext', (None, u('bunnies'), None), []), (7, '_', None, []), (8, 'gettext', u('Rabbit'), []), (9, 'dgettext', (u('wiki'), None), []), (10, 'dngettext', (None, u('Page'), u('Pages'), None), [])], messages)
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())
def test_comment_tags_not_on_start_of_comment(self): buf = StringIO(""" # This shouldn't be in the output # because it didn't start with a comment tag # do NOTE: this will not be a translation comment # NOTE: This one will be msg = _(u'Foo Bar') """) messages = list(extract.extract_python(buf, ('_',), ['NOTE:'], {})) self.assertEqual(u('Foo Bar'), messages[0][2]) self.assertEqual([u('NOTE: This one will be')], messages[0][3])
def test_unpgettext(self): self.assertEqualTypeToo( u('Voh1'), self.translations.ungettext('foo1', 'foos1', 1)) self.assertEqualTypeToo( u('Vohs1'), self.translations.ungettext('foo1', 'foos1', 2)) self.assertEqualTypeToo( u('VohCTX1'), self.translations.unpgettext('foo', 'foo1', 'foos1', 1)) self.assertEqualTypeToo( u('VohsCTX1'), self.translations.unpgettext('foo', 'foo1', 'foos1', 2))
def test_dunpgettext(self): self.assertEqualTypeToo( u('VohD1'), self.translations.dungettext('messages1', 'foo1', 'foos1', 1)) self.assertEqualTypeToo( u('VohsD1'), self.translations.dungettext('messages1', 'foo1', 'foos1', 2)) self.assertEqualTypeToo( u('VohCTXD1'), self.translations.dunpgettext('messages1', 'foo', 'foo1', 'foos1', 1)) self.assertEqualTypeToo( u('VohsCTXD1'), self.translations.dunpgettext('messages1', 'foo', 'foo1', 'foos1', 2))
def test_comment_tag_with_leading_space(self): buf = StringIO(""" #: A translation comment #: with leading spaces msg = _(u'Foo Bar') """) messages = list(extract.extract_python(buf, ('_', ), [':'], {})) self.assertEqual(u('Foo Bar'), messages[0][2]) self.assertEqual( [u(': A translation comment'), u(': with leading spaces')], messages[0][3])
def test_unpgettext(self): self.assertEqualTypeToo(u('Voh1'), self.translations.ungettext('foo1', 'foos1', 1)) self.assertEqualTypeToo(u('Vohs1'), self.translations.ungettext('foo1', 'foos1', 2)) self.assertEqualTypeToo(u('VohCTX1'), self.translations.unpgettext('foo', 'foo1', 'foos1', 1)) self.assertEqualTypeToo(u('VohsCTX1'), self.translations.unpgettext('foo', 'foo1', 'foos1', 2))
def test_comment_tags_not_on_start_of_comment(self): buf = StringIO(""" # This shouldn't be in the output # because it didn't start with a comment tag # do NOTE: this will not be a translation comment # NOTE: This one will be msg = _(u'Foo Bar') """) messages = list(extract.extract_python(buf, ('_', ), ['NOTE:'], {})) self.assertEqual(u('Foo Bar'), messages[0][2]) self.assertEqual([u('NOTE: This one will be')], messages[0][3])
def test_po_with_obsolete_message_ignored(self): catalog = Catalog() catalog.add(u('foo'), u('Voh'), locations=[('main.py', 1)]) catalog.obsolete['bar'] = Message(u('bar'), u('Bahr'), locations=[('utils.py', 3)], user_comments=['User comment']) buf = BytesIO() pofile.write_po(buf, catalog, omit_header=True, ignore_obsolete=True) self.assertEqual(b('''#: main.py:1 msgid "foo" msgstr "Voh"'''), buf.getvalue().strip())
def test_translator_comments_with_previous_non_translator_comments(self): buf = StringIO(""" # This shouldn't be in the output # because it didn't start with a comment tag # NOTE: A translation comment # with a second line msg = _(u'Foo Bar') """) messages = list(extract.extract_python(buf, ('_',), ['NOTE:'], {})) self.assertEqual(u('Foo Bar'), messages[0][2]) self.assertEqual([u('NOTE: A translation comment'), u('with a second line')], messages[0][3])
def test_multiline(self): buf = StringIO("""\ msg1 = ngettext('pylon', 'pylons', count) msg2 = ngettext('elvis', 'elvises', count) """) messages = list(extract.extract_python(buf, ('ngettext',), [], {})) self.assertEqual([(1, 'ngettext', (u('pylon'), u('pylons'), None), []), (3, 'ngettext', (u('elvis'), u('elvises'), None), [])], messages)
def test_multiline(self): buf = StringIO("""\ msg1 = ngettext('pylon', 'pylons', count) msg2 = ngettext('elvis', 'elvises', count) """) messages = list(extract.extract_python(buf, ('ngettext', ), [], {})) self.assertEqual([(1, 'ngettext', (u('pylon'), u('pylons'), None), []), (3, 'ngettext', (u('elvis'), u('elvises'), None), [])], messages)
def test_translator_comments_with_previous_non_translator_comments(self): buf = StringIO(""" # This shouldn't be in the output # because it didn't start with a comment tag # NOTE: A translation comment # with a second line msg = _(u'Foo Bar') """) messages = list(extract.extract_python(buf, ('_', ), ['NOTE:'], {})) self.assertEqual(u('Foo Bar'), messages[0][2]) self.assertEqual( [u('NOTE: A translation comment'), u('with a second line')], messages[0][3])
def test_triple_quoted_strings(self): buf = StringIO("""\ msg1 = _('''pylons''') msg2 = ngettext(r'''elvis''', \"\"\"elvises\"\"\", count) msg2 = ngettext(\"\"\"elvis\"\"\", 'elvises', count) """) messages = list(extract.extract_python(buf, extract.DEFAULT_KEYWORDS.keys(), [], {})) self.assertEqual([(1, '_', (u('pylons')), []), (2, 'ngettext', (u('elvis'), u('elvises'), None), []), (3, 'ngettext', (u('elvis'), u('elvises'), None), [])], messages)
def test_wrap_locations_with_hyphens(self): catalog = Catalog() catalog.add(u('foo'), locations=[ ('doupy/templates/base/navmenu.inc.html.py', 60) ]) catalog.add(u('foo'), locations=[ ('doupy/templates/job-offers/helpers.html', 22) ]) buf = BytesIO() pofile.write_po(buf, catalog, omit_header=True) self.assertEqual(b('''#: doupy/templates/base/navmenu.inc.html.py:60 #: doupy/templates/job-offers/helpers.html:22 msgid "foo" msgstr ""'''), buf.getvalue().strip())
def test_triple_quoted_strings(self): buf = StringIO("""\ msg1 = _('''pylons''') msg2 = ngettext(r'''elvis''', \"\"\"elvises\"\"\", count) msg2 = ngettext(\"\"\"elvis\"\"\", 'elvises', count) """) messages = list( extract.extract_python(buf, extract.DEFAULT_KEYWORDS.keys(), [], {})) self.assertEqual([(1, '_', (u('pylons')), []), (2, 'ngettext', (u('elvis'), u('elvises'), None), []), (3, 'ngettext', (u('elvis'), u('elvises'), None), [])], messages)
def test_po_with_previous_msgid(self): catalog = Catalog() catalog.add(u('foo'), u('Voh'), locations=[('main.py', 1)], previous_id=u('fo')) buf = BytesIO() pofile.write_po(buf, catalog, omit_header=True, include_previous=True) self.assertEqual( b('''#: main.py:1 #| msgid "fo" msgid "foo" msgstr "Voh"'''), buf.getvalue().strip())
def test_different_signatures(self): buf = StringIO(""" foo = _('foo', 'bar') n = ngettext('hello', 'there', n=3) n = ngettext(n=3, 'hello', 'there') n = ngettext(n=3, *messages) n = ngettext() n = ngettext('foo') """) messages = \ list(extract.extract('python', buf, extract.DEFAULT_KEYWORDS, [], {})) self.assertEqual(len(messages), 2) self.assertEqual(u('foo'), messages[0][1]) self.assertEqual((u('hello'), u('there')), messages[1][1])
def get_display_name(self, locale=None): """Return the display name of the locale using the given locale. The display name will include the language, territory, script, and variant, if those are specified. >>> Locale('zh', 'CN', script='Hans').get_display_name('en') == u('Chinese (Simplified Han, China)') True :param locale: the locale to use :return: the display name """ if locale is None: locale = self locale = Locale.parse(locale) retval = locale.languages.get(self.language) if self.territory or self.script or self.variant: details = [] if self.script: details.append(locale.scripts.get(self.script)) if self.territory: details.append(locale.territories.get(self.territory)) if self.variant: details.append(locale.variants.get(self.variant)) details = [_f for _f in details if _f] if details: retval += ' (%s)' % u(', ').join(details) return retval