def test_update_catalog_comments(): # Based on https://web.archive.org/web/20100710131029/http://babel.edgewall.org/attachment/ticket/163/cat-update-comments.py catalog = pofile.read_po( StringIO(''' # A user comment #. An auto comment #: main.py:1 #, fuzzy, python-format msgid "foo %(name)s" msgstr "foo %(name)s" ''')) assert all(message.user_comments and message.auto_comments for message in catalog if message.id) # NOTE: in the POT file, there are no comments template = pofile.read_po( StringIO(''' #: main.py:1 #, fuzzy, python-format msgid "bar %(name)s" msgstr "" ''')) catalog.update(template) # Auto comments will be obliterated here assert all(message.user_comments for message in catalog if message.id)
def test_frontend_can_log_to_predefined_handler(self): custom_stream = StringIO() log = logging.getLogger('babel') log.addHandler(logging.StreamHandler(custom_stream)) self._run_init_catalog() self.assertNotEqual(id(sys.stderr), id(custom_stream)) self.assertEqual('', sys.stderr.getvalue()) assert len(custom_stream.getvalue()) > 0
def test_abort_invalid_po_file(self): invalid_po = ''' msgctxt "" "{\"checksum\": 2148532640, \"cxt\": \"collector_thankyou\", \"id\": " "270005359}" msgid "" "Thank you very much for your time.\n" "If you have any questions regarding this survey, please contact Fulano " "at [email protected]" msgstr "Merci de prendre le temps de remplir le sondage. Pour toute question, veuillez communiquer avec Fulano à [email protected] " ''' invalid_po_2 = ''' msgctxt "" "{\"checksum\": 2148532640, \"cxt\": \"collector_thankyou\", \"id\": " "270005359}" msgid "" "Thank you very much for your time.\n" "If you have any questions regarding this survey, please contact Fulano " "at [email protected]." msgstr "Merci de prendre le temps de remplir le sondage. Pour toute question, veuillez communiquer avec Fulano a [email protected] " ''' # Catalog not created, throws Unicode Error buf = StringIO(invalid_po) output = None # This should only be thrown under py27 if sys.version_info.major == 2: with self.assertRaises(UnicodeEncodeError): output = pofile.read_po(buf, locale='fr', abort_invalid=False) assert not output else: output = pofile.read_po(buf, locale='fr', abort_invalid=False) assert isinstance(output, Catalog) # Catalog not created, throws PoFileError buf = StringIO(invalid_po_2) output = None with self.assertRaises(pofile.PoFileError) as e: output = pofile.read_po(buf, locale='fr', abort_invalid=True) assert not output # Catalog is created with warning, no abort buf = StringIO(invalid_po_2) output = pofile.read_po(buf, locale='fr', abort_invalid=False) assert isinstance(output, Catalog) # Catalog not created, aborted with PoFileError buf = StringIO(invalid_po_2) output = None with self.assertRaises(pofile.PoFileError) as e: output = pofile.read_po(buf, locale='fr', abort_invalid=True) assert not output
def test_unknown_language_roundtrip(): buf = StringIO(r''' msgid "" msgstr "" "Language: sr_SP\n"''') catalog = pofile.read_po(buf) assert catalog.locale_identifier == 'sr_SP' assert not catalog.locale buf = BytesIO() pofile.write_po(buf, catalog) assert 'sr_SP' in buf.getvalue().decode()
def test_locale_gets_overridden_by_file(self): buf = StringIO(r''' msgid "" msgstr "" "Language: en_US\n"''') catalog = pofile.read_po(buf, locale='de') self.assertEqual(Locale('en', 'US'), catalog.locale) buf = StringIO(r''' msgid "" msgstr "" "Language: ko-KR\n"''') catalog = pofile.read_po(buf, locale='de') self.assertEqual(Locale('ko', 'KR'), catalog.locale)
def setUp(self): self.datadir = os.path.join(this_dir, 'data') self.orig_working_dir = os.getcwd() self.orig_argv = sys.argv self.orig_stdout = sys.stdout self.orig_stderr = sys.stderr sys.argv = ['pybabel'] sys.stdout = StringIO() sys.stderr = StringIO() os.chdir(self.datadir) self._remove_log_handlers() self.cli = frontend.CommandLineInterface()
def test_collapse_whitespaces(): buf = StringIO('<html><div i18n> \n\t\t hello <strong>Beautiful</strong> \n\t\t world!\n\t\t </div> </html>') messages = list(extract_angularjs(buf, [], [], {})) assert messages == [ (1, 'gettext', 'hello <strong>Beautiful</strong> world!', []) ]
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 test_simple_string(): buf = StringIO('<html><div i18n>hello world!</div></html>') messages = list(extract_angularjs(buf, [], [], {})) assert messages == [ (1, 'gettext', 'hello world!', []) ]
def _get_mappings(self): mappings = {} if self.mapping_file: fileobj = open(self.mapping_file, 'U') try: method_map, options_map = parse_mapping(fileobj) for dirname in self.input_dirs: mappings[dirname] = method_map, options_map finally: fileobj.close() elif getattr(self.distribution, 'message_extractors', None): message_extractors = self.distribution.message_extractors for dirname, mapping in message_extractors.items(): if isinstance(mapping, string_types): method_map, options_map = parse_mapping(StringIO(mapping)) else: method_map, options_map = [], {} for pattern, method, options in mapping: method_map.append((pattern, method)) options_map[pattern] = options or {} mappings[dirname] = method_map, options_map else: for dirname in self.input_dirs: mappings[dirname] = DEFAULT_MAPPING, {} return mappings
def test_parse_mapping(): buf = StringIO('[extractors]\n' 'custom = mypackage.module:myfunc\n' '\n' '# Python source files\n' '[python: **.py]\n' '\n' '# Genshi templates\n' '[genshi: **/templates/**.html]\n' 'include_attrs =\n' '[genshi: **/templates/**.txt]\n' 'template_class = genshi.template:TextTemplate\n' 'encoding = latin-1\n' '\n' '# Some custom extractor\n' '[custom: **/custom/*.*]\n') method_map, options_map = frontend.parse_mapping(buf) assert len(method_map) == 4 assert method_map[0] == ('**.py', 'python') assert options_map['**.py'] == {} assert method_map[1] == ('**/templates/**.html', 'genshi') assert options_map['**/templates/**.html']['include_attrs'] == '' assert method_map[2] == ('**/templates/**.txt', 'genshi') assert (options_map['**/templates/**.txt']['template_class'] == 'genshi.template:TextTemplate') assert options_map['**/templates/**.txt']['encoding'] == 'latin-1' assert method_map[3] == ('**/custom/*.*', 'mypackage.module:myfunc') assert options_map['**/custom/*.*'] == {}
def test_do_not_extract_attribute(): buf = StringIO( '<html><div title="some title" no-i18n-title>hello world!</div></html>' ) messages = list( extract_angularjs(buf, [], [], {"include_attributes": "title"})) assert messages == []
def test_nested_tags(): buf = StringIO( '<html><div i18n>hello <strong>Beautiful</strong> world!</div></html>') messages = list(extract_angularjs(buf, [], [], {})) assert messages == [(1, 'gettext', 'hello <strong>Beautiful</strong> world!', [], ())]
def test_extract_attribute3(): buf = StringIO('<html><div i18n><strong something="some title" i18n-something>hello world!</strong></div></html>') messages = list(extract_angularjs(buf, [], [], {"include_attributes": "something"})) assert messages == [ (1, 'gettext', 'some title', ["something"]), (1, 'gettext', '<strong something="some title" i18n-something>hello world!</strong>', []) ]
def _get_mappings(self): mappings = [] if self.mapping_file: with open(self.mapping_file, 'U') as fileobj: method_map, options_map = parse_mapping(fileobj) for path in self.input_paths: mappings.append((path, method_map, options_map)) elif getattr(self.distribution, 'message_extractors', None): message_extractors = self.distribution.message_extractors for path, mapping in message_extractors.items(): if isinstance(mapping, string_types): method_map, options_map = parse_mapping(StringIO(mapping)) else: method_map, options_map = [], {} for pattern, method, options in mapping: method_map.append((pattern, method)) options_map[pattern] = options or {} mappings.append((path, method_map, options_map)) else: for path in self.input_paths: mappings.append((path, DEFAULT_MAPPING, {})) return mappings
def test_comments(): buf = StringIO('<html><div i18n="page title">hello world!</div></html>') messages = list(extract_angularjs(buf, [], [], {})) assert messages == [ (1, 'gettext', 'hello world!', ['page title']) ]
def test_check_tag_attributes_in_content_ok(): buf = StringIO('<html><div i18n>hello <br><strong><a href="www.helloworld.com" >world</a></strong>!</div></html>') messages = list(extract_angularjs(buf, [], [], {"include_attributes": "title alt", "allowed_tags": "a br strong", "allowed_attributes_a": "href"})) assert messages == [ (1, 'gettext', u'hello <br><strong><a href="www.helloworld.com">world</a></strong>!', []) ]
def test_do_not_extract_entire_div_block(): buf = StringIO('<html><div no-i18n>hello world!\n<h2 title="some title">Heading 2</h2>\n<div>another div <p>text1</p></div><p>text2</p></div>\n<h3 title="extracted title">Heading 3</h3></html>') messages = list(extract_angularjs(buf, [], [], {"include_attributes": "title alt", "include_tags": "p h2 h3"})) assert messages == [ (4, 'gettext', u'extracted title', ['title']), (4, 'gettext', u'Heading 3', []), ]
def test_do_not_extract_tag(): buf = StringIO('<html><h2 no-i18n>Heading 2</h2>\n<h3 no-i18n="fake comment">Heading 3</h3>\n<p>Hello world!</p><p i18n="useful comment for translator">Hello again.</p></html>') messages = list(extract_angularjs(buf, [], [], {"include_tags": "p h2 h3"})) assert messages == [ (3, 'gettext', u'Hello world!', []), (3, 'gettext', u'Hello again.', ["useful comment for translator"]) ]
def test_auto_extract(): buf = StringIO('<html><h2>Heading 2</h2>\n<h3>Heading 3</h3>\n<p>Hello world!</p><p i18n="useful comment for translator">Hello again.</p></html>') messages = list(extract_angularjs(buf, [], [], {"include_tags": "p h2 h3", "allowed_tags": "span strong br i a", "allowed_attributes_i": "class", "allowed_attributes_span": "class", "allowed_attributes_a": "target ng-href"})) assert messages == [ (1, 'gettext', u'Heading 2', []), (2, 'gettext', u'Heading 3', []), (3, 'gettext', u'Hello world!', []), (3, 'gettext', u'Hello again.', ["useful comment for translator"]) ]
def test_single_plural_form(self): buf = StringIO(r'''msgid "foo" msgid_plural "foos" msgstr[0] "Voh"''') catalog = pofile.read_po(buf, locale='ja_JP') self.assertEqual(1, len(catalog)) self.assertEqual(1, catalog.num_plurals) message = catalog['foo'] self.assertEqual(1, len(message.string))
def test_check_tags_in_content_tag_error(): buf = StringIO('<html><div title="hello world!" i18n> hello <br><strong><div>world</div></strong>!\n</div>\n<div alt="hello world!" i18n> hello world!</div></html>') passed = False try: messages = list(extract_angularjs(buf, [], [], {"include_attributes": "title alt"})) except TagNotAllowedException: passed = True assert passed
def test_check_tags_in_content_ok(): buf = StringIO('<html><div title="hello world!" i18n> hello <br><strong>world</strong>!\n</div>\n<div alt="hello world!" i18n> hello world!</div></html>') messages = list(extract_angularjs(buf, [], [], {"include_attributes": "title alt"})) assert messages == [ (1, 'gettext', u'hello world!', ['title']), (1, 'gettext', u'hello <br><strong>world</strong>!', []), (3, 'gettext', u'hello world!', ['alt']), (3, 'gettext', u'hello world!', []) ]
def test_plural_with_square_brackets(self): buf = StringIO(r'''msgid "foo" msgid_plural "foos" msgstr[0] "Voh [text]" msgstr[1] "Vohs [text]"''') catalog = pofile.read_po(buf, locale='nb_NO') self.assertEqual(1, len(catalog)) self.assertEqual(2, catalog.num_plurals) message = catalog['foo'] self.assertEqual(2, len(message.string))
def test_get_string_positions(): buf = StringIO('<html> \n\n\n<div title="hello <br> world!" i18n> \n hello world!\n</div>\n<div alt=" hello\n world \t!" i18n> hello world! ěščřžýá</div></html>') messages = list(extract_angularjs(buf, [], [], {"include_attributes": "title alt"})) assert messages == [ (4, 'gettext', u'hello <br> world!', ['title']), (4, 'gettext', u'hello world!', []), (7, 'gettext', u'hello world !', ['alt']), (7, 'gettext', u'hello world! ěščřžýá', []) ]
def test_do_not_extract_entire_div_block_inner_attributes_error(): for attr in ["i18n", "no-i18n", "i18n-title", "no-i18n-title"]: buf = StringIO('<html><div no-i18n>hello world!\n<h2 title="some title" %s>Heading 2</h2>\n<div>another div <p>text1</p></div><p>text2</p>\n<h3 title="extracted title">Heading 3</h3></div></html>' % attr) passed = False try: messages = list(extract_angularjs(buf, [], [], {"include_attributes": "title alt", "include_tags": "p h2 h3"})) except ExtractAttributeNotAllowedException: passed = True assert passed
def test_warn_if_empty_string_msgid_found_in_context_aware_extraction_method(self): buf = BytesIO(b"\nmsg = pgettext('ctxt', '')\n") stderr = sys.stderr sys.stderr = StringIO() try: messages = extract.extract('python', buf) self.assertEqual([], list(messages)) assert 'warning: Empty msgid.' in sys.stderr.getvalue() finally: sys.stderr = stderr
def test_more_than_two_plural_forms(self): buf = StringIO(r'''msgid "foo" msgid_plural "foos" msgstr[0] "Voh" msgstr[1] "Vohs" msgstr[2] "Vohss"''') catalog = pofile.read_po(buf, locale='lv_LV') self.assertEqual(1, len(catalog)) self.assertEqual(3, catalog.num_plurals) message = catalog['foo'] self.assertEqual(3, len(message.string)) self.assertEqual(u'Vohss', message.string[2])
def test_not_fuzzy_header(self): buf = StringIO(r''' # Translations template for AReallyReallyLongNameForAProject. # Copyright (C) 2007 ORGANIZATION # This file is distributed under the same license as the # AReallyReallyLongNameForAProject project. # FIRST AUTHOR <EMAIL@ADDRESS>, 2007. # ''') catalog = pofile.read_po(buf) self.assertEqual(1, len(list(catalog))) self.assertEqual(False, list(catalog)[0].fuzzy)
def test_read_multiline(self): buf = StringIO(r'''msgid "" "Here's some text that\n" "includesareallylongwordthatmightbutshouldnt" " throw us into an infinite " "loop\n" msgstr ""''') catalog = pofile.read_po(buf) self.assertEqual(1, len(catalog)) message = list(catalog)[1] self.assertEqual("Here's some text that\nincludesareallylongwordthat" "mightbutshouldnt throw us into an infinite loop\n", message.id)
continue # Get the translated test and insert it into the template translated_text = [] for prop in field: for text in prop['translatable_text']: translated_text.append(locale['locale'].ugettext(text)) translated_string = prop['template'] % tuple(translated_text) # Add the new translation to the catalog locale['catalog'].add(prop['raw_value'], translated_string) # Write every catalog with the new values for name, locale in locale_catalogs.iteritems(): # Again, if there is not catalog, simply go to the next one if 'catalog' not in locale: continue # Write the catalog to a string buf = StringIO() write_po(buf, locale['catalog']) # And the string to the file po_stream = open(locale['path'], 'w') po_stream.write(buf.getvalue()) po_stream.close() # If the file doesn't exist just do nothing except IOError as e: print(u'Template strings file does not exist, no action is necessary.')