Ejemplo n.º 1
0
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())
Ejemplo n.º 2
0
def _secret_error(user_secret: Dict[str, Any],
                  message: I18nMessage) -> Dict[str, Any]:
    retval = {**user_secret, "error": message.to_dict()}
    del retval["secret"]
    return retval