def _i18n_message_source_to_localizer( message: I18nMessage, ctx: JsonizeModuleContext) -> MessageLocalizer: """Return a localizer for the source of the given `I18nMessage`. Raise `NotInternationalizedError` if the source is a non-internationalized module. Raise `KeyError` if the source is a module that has no associated `ModuleZipFile`. """ if message.source == "module": module_zipfile = ctx.module_zipfiles[ ctx.module_id] # Raises `KeyError` return MESSAGE_LOCALIZER_REGISTRY.for_module_zipfile( module_zipfile) # Raises `NotInternationalizedError` elif message.source == "cjwmodule": return MESSAGE_LOCALIZER_REGISTRY.cjwmodule_localizer else: # if message.source is None return MESSAGE_LOCALIZER_REGISTRY.application_localizer
def _localize_module_spec_message(message_path: str, ctx: JsonizeModuleContext, spec_value: str) -> str: """Search the module catalogs for the spec message with the given path and localize it. The path of the message is its translation key minus "_spec." at the beginning. If the message is not found or is incorrectly formatted, `spec_value` is returned. In addition, if `spec_value` is empty, `spec_value` is returned. This is in order to make sure that module spec values not defined in the spec file cannot be overriden by message catalogs. Uses `locale_id`, `module_id`, and `module_zipfiles` from `ctx` """ if not spec_value: return spec_value message_id = f"_spec.{message_path}" if ctx.module_zipfile is None: logger.exception(f"Module {ctx.module_id} not in jsonize context") return spec_value try: localizer = MESSAGE_LOCALIZER_REGISTRY.for_module_zipfile( ctx.module_zipfile) except NotInternationalizedError: return spec_value try: return localizer.localize(ctx.locale_id, message_id) except ICUError: # `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( "I18nMessage badly formatted in default locale. id: %s, module: %s", message_id, str(ctx.module_zipfile.path), ) return spec_value except KeyError: logger.exception( "I18nMessage not found in module catalogs. id: %s, module: %s", message_id, str(ctx.module_zipfile.path), ) return spec_value
def _i18n_message_source_to_localizer( message: I18nMessage, ctx: JsonizeModuleContext) -> MessageLocalizer: """Return a localizer for the source of the given `I18nMessage`. Raise `NotInternationalizedError` if the source is a non-internationalized module. """ if message.source == "module": if ctx.module_zipfile is None: raise NotInternationalizedError return MESSAGE_LOCALIZER_REGISTRY.for_module_zipfile( ctx.module_zipfile) elif message.source == "cjwmodule": return MESSAGE_LOCALIZER_REGISTRY.cjwmodule_localizer elif message.source == "cjwparse": return MESSAGE_LOCALIZER_REGISTRY.cjwparse_localizer else: # if message.source is None return MESSAGE_LOCALIZER_REGISTRY.application_localizer