def test_translation_with_nonstring_default(self): code = BytesIO(b""" from cjwmodule.i18n import trans def render(table): default_message = 'Default message' return (table, trans('message.id', default_message) ) """) with self.assertRaises(SyntaxError): find_messages_in_module_code(code, "module.py")
def test_multiple_translations(self): code = BytesIO(b""" from cjwmodule import i18n from mygreatmodule import this def render(table): if this(): return (table, # i18n: Some helpful comment i18n.trans('message.id', 'Default message') ) else: return (table, i18n.trans( 'other_message.id', 'Other default message', {'param1': 'a', 'param2': 3} ) ) """) result = find_messages_in_module_code(code, "module.py") expected = { "message.id": { "string": "Default message", "locations": [("module.py", 9)], "comments": ["i18n: Some helpful comment"], }, "other_message.id": { "string": "Other default message", "locations": [("module.py", 13)], "comments": [], }, } self.assertEqual(result, expected)
def test_no_translations(self): code = BytesIO(b""" def render(table): return table """) result = find_messages_in_module_code(code, "module.py") expected = {} self.assertEqual(result, expected)
def test_translation_with_nonstring_id(self): code = BytesIO(b""" from cjwmodule.i18n import trans def render(table): id = 'message.id' return (table, trans(id, 'Default message') ) """) result = find_messages_in_module_code(code, "module.py") expected = {} self.assertEqual(result, expected)
def test_translation_simple(self): code = BytesIO(b""" from cjwmodule import i18n def render(table): return (table, i18n.trans('message.id', 'Default message')) """) result = find_messages_in_module_code(code, "module.py") expected = { "message.id": { "string": "Default message", "locations": [("module.py", 5)], "comments": [], } } self.assertEqual(result, expected)
def _build_source_catalog( spec: ModuleSpec, module_code_path: pathlib.Path, module_root_directory: pathlib.Path, ) -> Catalog: source_catalog = Catalog(default_locale) for message_id, source_string in find_spec_messages(spec).items(): source_catalog.add(message_id, string=source_string) for message_id, message_properties in find_messages_in_module_code( module_code_path, module_root_directory).items(): source_catalog.add( message_id, string=message_properties["string"], auto_comments=message_properties["comments"], locations=message_properties["locations"], ) return source_catalog
def _build_source_catalog(module_zipfile: ModuleZipfile) -> Catalog: source_catalog = Catalog(default_locale) spec = module_zipfile.get_spec() for message_id, source_string in find_spec_messages(spec).items(): source_catalog.add(message_id, string=source_string) with zipfile.ZipFile(module_zipfile.path, mode="r") as zf: for info in zf.infolist(): if info.filename.endswith(".py"): with zf.open(info) as code_io: for message_id, message_properties in find_messages_in_module_code( code_io, info.filename).items(): source_catalog.add( message_id, string=message_properties["string"], auto_comments=message_properties["comments"], locations=message_properties["locations"], ) return source_catalog
def test_translation_with_arguments(self): code = BytesIO(b""" from cjwmodule import i18n from mygreatmodule import help def render(table): return (table, i18n.trans('message.id', 'Default message with {parameter}', {'parameter': help()}) ) """) result = find_messages_in_module_code(code, "module.py") expected = { "message.id": { "string": "Default message with {parameter}", "locations": [("module.py", 7)], "comments": [], } } self.assertEqual(result, expected)
def test_simple_translation_without_i18n_literal(self): code = BytesIO(b""" from cjwmodule.i18n import trans def render(table): return (table, # i18n: Some helpful comment trans('message.id', 'Default message') ) """) result = find_messages_in_module_code(code, "module.py") expected = { "message.id": { "string": "Default message", "locations": [("module.py", 7)], "comments": ["i18n: Some helpful comment"], } } self.assertEqual(result, expected)
def test_translation_multiple_appearances(self): code = BytesIO(b""" from cjwmodule import i18n from mygreatmodule import this def render(table): if this(): return (table, i18n.trans('message.id', 'Default message with {parameter}', {'parameter': this()}) ) else: return (table, i18n.trans('message.id', 'Default message with {parameter}', {'parameter': 'other'}) ) """) result = find_messages_in_module_code(code, "module.py") expected = { "message.id": { "string": "Default message with {parameter}", "locations": [("module.py", 8), ("module.py", 12)], "comments": [], } } self.assertEqual(result, expected)