def test_message_invalid_parameter_syntax_in_both(self): catalog = Catalog() catalog.add("id", string="Hello {a b}") with mock_app_catalogs({"el": catalog, "en": catalog}): with self.assertLogs(level=logging.ERROR): with self.assertRaises(ICUError): localize("el", "id", arguments={"a": "you", "b": "!"})
def test_message_exists_in_no_locales(self): def mock_get_translations(locale): return MessageTranslator(locale, Catalog()) with patch("cjworkbench.i18n.trans._get_translations", mock_get_translations): with self.assertRaises(KeyError): localize("el", "id")
def test_message_invalid_parameter_syntax_in_default(self): def mock_get_translations(locale): catalog = Catalog() if locale == "en": catalog.add("id", string="Hello {a b}") return MessageTranslator(locale, catalog) with patch("cjworkbench.i18n.trans._get_translations", mock_get_translations): with self.assertRaises(ICUError): localize("el", "id", arguments={"a": "you", "b": "!"})
def jsonize_i18n_message(message: I18nMessage, ctx: JsonizeContext) -> str: """Localize (or unwrap, if it's a TODO_i18n) an `I18nMessage` Uses `locale_id` from `ctx` Raises `KeyError` if the message text cannot be found in the catalogs. """ assert message.source is None if message.id == "TODO_i18n": return message.args["text"] else: # Attempt to localize in the locale given by `ctx`. try: return localize(ctx.locale_id, message.id, arguments=message.args) except ICUError as err: # `localize` handles `ICUError` for the given locale. # Hence, if we get here, it means that the message is badly formatted in the default locale. logger.exception( f"I18nMessage badly formatted in default locale. id: {message.id}, source: {message.source}" ) except KeyError as err: logger.exception( f"I18nMessage not found. id: {message.id}, source: {message.source}" ) return json.dumps(message.to_dict())
def test_message_exists_in_given_locale(self): en_catalog = Catalog() en_catalog.add("id", string="Hello") el_catalog = Catalog() el_catalog.add("id", string="Hey") with mock_app_catalogs({"el": el_catalog, "en": en_catalog}): self.assertEqual(localize("el", "id"), "Hey")
def test_message_invalid_parameter_syntax(self): en_catalog = Catalog() en_catalog.add("id", string="Hello {a} {b}") el_catalog = Catalog() el_catalog.add("id", string="Hey {a b}!") with mock_app_catalogs({"el": el_catalog, "en": en_catalog}): with self.assertLogs(level=logging.ERROR): result = localize("el", "id", arguments={"a": "you", "b": "!"}) self.assertEqual(result, "Hello you !")
def test_message_exists_only_in_default_locale(self): def mock_get_translations(locale): catalog = Catalog() if locale == "en": catalog.add("id", string="Hello") return MessageTranslator(locale, catalog) with patch("cjworkbench.i18n.trans._get_translations", mock_get_translations): self.assertEqual(localize("el", "id"), "Hello")
def test_message_invalid_parameter_syntax(self): def mock_get_translations(locale): catalog = Catalog() if locale == "en": catalog.add("id", string="Hello {a} {b}") else: catalog.add("id", string="Hey {a b}!") return MessageTranslator(locale, catalog) with patch("cjworkbench.i18n.trans._get_translations", mock_get_translations): with self.assertLogs(level=logging.ERROR): result = localize("el", "id", arguments={"a": "you", "b": "!"}) self.assertEqual(result, "Hello you !")
def test_message_invalid_parameter_syntax_in_default(self): en_catalog = Catalog() en_catalog.add("id", string="Hello {a b}") with mock_app_catalogs({"el": Catalog(), "en": en_catalog}): with self.assertRaises(ICUError): localize("el", "id", arguments={"a": "you", "b": "!"})
def test_message_exists_in_no_locales(self): with mock_app_catalogs({"el": Catalog(), "en": Catalog()}): with self.assertRaises(KeyError): localize("el", "id")