def _get_mime_headers(self): headers = [] headers.append(('Project-Id-Version', '%s %s' % (self.project, self.version))) headers.append(('Report-Msgid-Bugs-To', self.msgid_bugs_address)) headers.append(('POT-Creation-Date', format_datetime(self.creation_date, 'yyyy-MM-dd HH:mmZ', locale='en'))) if isinstance(self.revision_date, (datetime, time_) + number_types): headers.append(('PO-Revision-Date', format_datetime(self.revision_date, 'yyyy-MM-dd HH:mmZ', locale='en'))) else: headers.append(('PO-Revision-Date', self.revision_date)) headers.append(('Last-Translator', self.last_translator)) if (self.locale is not None) and ('LANGUAGE' in self.language_team): headers.append(('Language-Team', self.language_team.replace('LANGUAGE', str(self.locale)))) else: headers.append(('Language-Team', self.language_team)) if self.locale is not None: headers.append(('Plural-Forms', self.plural_forms)) headers.append(('MIME-Version', '1.0')) headers.append(('Content-Type', 'text/plain; charset=%s' % self.charset)) headers.append(('Content-Transfer-Encoding', '8bit')) headers.append(('Generated-By', 'Babel %s\n' % VERSION)) return headers
def test_format_datetime(): dt = datetime(2007, 4, 1, 15, 30) assert dates.format_datetime(dt, locale="en_US") == u"Apr 1, 2007, 3:30:00 PM" full = dates.format_datetime(dt, "full", tzinfo=timezone("Europe/Paris"), locale="fr_FR") assert full == (u"dimanche 1 avril 2007 17:30:00 heure " u"avanc\xe9e d\u2019Europe centrale") custom = dates.format_datetime(dt, "yyyy.MM.dd G 'at' HH:mm:ss zzz", tzinfo=timezone("US/Eastern"), locale="en") assert custom == u"2007.04.01 AD at 11:30:00 EDT"
def test_supports_no_wrap(self): self.cmd.input_file = 'project/i18n/long_messages.pot' self.cmd.locale = 'en_US' self.cmd.output_dir = 'project/i18n' long_message = '"'+ 'xxxxx '*15 + '"' with open('project/i18n/messages.pot', 'rb') as f: pot_contents = f.read().decode('latin-1') pot_with_very_long_line = pot_contents.replace('"bar"', long_message) with open(self.cmd.input_file, 'wb') as f: f.write(pot_with_very_long_line.encode('latin-1')) self.cmd.no_wrap = True self.cmd.finalize_options() self.cmd.run() po_file = self._po_file('en_US') assert os.path.isfile(po_file) expected_content = r"""# English (United States) 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: en_US <*****@*****.**>\n" "Plural-Forms: nplurals=2; plural=(n != 1)\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 coment, #. that will include several lines #: project/file1.py:8 msgid %(long_message)s msgstr "" #: project/file2.py:9 msgid "foobar" msgid_plural "foobars" msgstr[0] "" msgstr[1] "" """ % {'version': VERSION, 'date': format_datetime(datetime.now(LOCALTZ), 'yyyy-MM-dd HH:mmZ', tzinfo=LOCALTZ, locale='en_US'), 'long_message': long_message} with open(po_file, 'U') as f: actual_content = f.read() self.assertEqual(expected_content, actual_content)
def test_extraction_with_mapping_dict(self): self.dist.message_extractors = { 'project': [ ('**/ignored/**.*', 'ignore', None), ('**.py', 'python', None), ] } self.cmd.copyright_holder = 'FooBar, Inc.' self.cmd.msgid_bugs_address = '*****@*****.**' self.cmd.output_file = 'project/i18n/temp.pot' self.cmd.add_comments = 'TRANSLATOR:,TRANSLATORS:' self.cmd.finalize_options() self.cmd.run() self.assert_pot_file_exists() expected_content = r"""# Translations template for TestProject. # Copyright (C) %(year)s FooBar, Inc. # This file is distributed under the same license as the TestProject # project. # FIRST AUTHOR <EMAIL@ADDRESS>, %(year)s. # #, fuzzy msgid "" msgstr "" "Project-Id-Version: TestProject 0.1\n" "Report-Msgid-Bugs-To: [email protected]\n" "POT-Creation-Date: %(date)s\n" "PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n" "Last-Translator: FULL NAME <EMAIL@ADDRESS>\n" "Language-Team: LANGUAGE <*****@*****.**>\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" #. TRANSLATOR: This will be a translator coment, #. 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] "" """ % {'version': VERSION, 'year': time.strftime('%Y'), 'date': format_datetime(datetime.now(LOCALTZ), 'yyyy-MM-dd HH:mmZ', tzinfo=LOCALTZ, locale='en')} with open(self._pot_file(), 'U') as f: actual_content = f.read() self.assertEqual(expected_content, actual_content)
def test_extract_with_default_mapping(self): pot_file = self._pot_file() self.cli.run(sys.argv + ['extract', '--copyright-holder', 'FooBar, Inc.', '--project', 'TestProject', '--version', '0.1', '--msgid-bugs-address', '*****@*****.**', '-c', 'TRANSLATOR', '-c', 'TRANSLATORS:', '-o', pot_file, 'project']) self.assert_pot_file_exists() expected_content = r"""# Translations template for TestProject. # Copyright (C) %(year)s FooBar, Inc. # This file is distributed under the same license as the TestProject # project. # FIRST AUTHOR <EMAIL@ADDRESS>, %(year)s. # #, fuzzy msgid "" msgstr "" "Project-Id-Version: TestProject 0.1\n" "Report-Msgid-Bugs-To: [email protected]\n" "POT-Creation-Date: %(date)s\n" "PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n" "Last-Translator: FULL NAME <EMAIL@ADDRESS>\n" "Language-Team: LANGUAGE <*****@*****.**>\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" #. TRANSLATOR: This will be a translator coment, #. 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] "" #: project/ignored/this_wont_normally_be_here.py:11 msgid "FooBar" msgid_plural "FooBars" msgstr[0] "" msgstr[1] "" """ % {'version': VERSION, 'year': time.strftime('%Y'), 'date': format_datetime(datetime.now(LOCALTZ), 'yyyy-MM-dd HH:mmZ', tzinfo=LOCALTZ, locale='en')} with open(pot_file, 'U') as f: actual_content = f.read() self.assertEqual(expected_content, actual_content)
def test_1_num_plurals_checkers(self): for _locale in [p for p in PLURALS if PLURALS[p][0] == 1]: try: locale = Locale.parse(_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] "" """ % dict(locale = _locale, english_name = locale.english_name, version = VERSION, year = time.strftime('%Y'), date = format_datetime(datetime.now(LOCALTZ), 'yyyy-MM-dd HH:mmZ', tzinfo=LOCALTZ, locale=_locale), num_plurals = PLURALS[_locale][0], plural_expr = PLURALS[_locale][0])).encode('utf-8') # 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 datetime(self, datetime=None, format='medium'): """Return a date and time formatted according to the given pattern. >>> from datetime import datetime >>> from pytz import timezone >>> fmt = Format('en_US', tzinfo=timezone('US/Eastern')) >>> fmt.datetime(datetime(2007, 4, 1, 15, 30)) u'Apr 1, 2007, 11:30:00 AM' """ return format_datetime(datetime, format, tzinfo=self.tzinfo, locale=self.locale)
def test_correct_init_more_than_2_plurals(self): self.cmd.input_file = 'project/i18n/messages.pot' self.cmd.locale = 'lv_LV' self.cmd.output_dir = 'project/i18n' self.cmd.finalize_options() self.cmd.run() po_file = self._po_file('lv_LV') assert os.path.isfile(po_file) expected_content = r"""# Latvian (Latvia) 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: lv_LV <*****@*****.**>\n" "Plural-Forms: nplurals=3; plural=(n%%10==1 && n%%100!=11 ? 0 : n != 0 ? 1 :" " 2)\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 coment, #. 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] "" """ % {'version': VERSION, 'date': format_datetime(datetime.now(LOCALTZ), 'yyyy-MM-dd HH:mmZ', tzinfo=LOCALTZ, locale='en')} with open(po_file, 'U') as f: actual_content = f.read() self.assertEqual(expected_content, actual_content)
def test_init_with_output_dir(self): po_file = self._po_file('en_US') self.cli.run(sys.argv + ['init', '--locale', 'en_US', '-d', os.path.join(self._i18n_dir()), '-i', os.path.join(self._i18n_dir(), 'messages.pot')]) assert os.path.isfile(po_file) expected_content = r"""# English (United States) 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. # #, fuzzy 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: en_US <*****@*****.**>\n" "Plural-Forms: nplurals=2; plural=(n != 1)\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 coment, #. 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] "" """ % {'version': VERSION, 'date': format_datetime(datetime.now(LOCALTZ), 'yyyy-MM-dd HH:mmZ', tzinfo=LOCALTZ, locale='en')} with open(po_file, 'U') as f: actual_content = f.read() self.assertEqual(expected_content, actual_content)
def test_mime_headers_contain_same_information_as_attributes(self): cat = catalog.Catalog() cat[''] = catalog.Message('', "Last-Translator: Foo Bar <*****@*****.**>\n" + "Language-Team: de <*****@*****.**>\n" + "POT-Creation-Date: 2009-03-01 11:20+0200\n" + "PO-Revision-Date: 2009-03-09 15:47-0700\n") self.assertEqual(None, cat.locale) mime_headers = dict(cat.mime_headers) self.assertEqual('Foo Bar <*****@*****.**>', cat.last_translator) self.assertEqual('Foo Bar <*****@*****.**>', mime_headers['Last-Translator']) self.assertEqual('de <*****@*****.**>', cat.language_team) self.assertEqual('de <*****@*****.**>', mime_headers['Language-Team']) dt = datetime.datetime(2009, 3, 9, 15, 47, tzinfo=FixedOffsetTimezone(-7 * 60)) self.assertEqual(dt, cat.revision_date) formatted_dt = format_datetime(dt, 'yyyy-MM-dd HH:mmZ', locale='en') self.assertEqual(formatted_dt, mime_headers['PO-Revision-Date'])
def test_with_float(self): d = datetime(2012, 4, 1, 15, 30, 29, tzinfo=timezone("UTC")) epoch = float(calendar.timegm(d.timetuple())) formatted_string = dates.format_datetime(epoch, format="long", locale="en_US") self.assertEqual(u"April 1, 2012 at 3:30:29 PM +0000", formatted_string)