Skip to content

localization

Class info

Classes

Name Children Inherits
NoBabelExtension
jinjarope.localization

    🛈 DocStrings

    install_translations

    install_translations(
        env: Environment, locale: str | Locale, dirs: Sequence[str | PathLike[str]]
    ) -> None
    

    Install translations for the given locale in the Jinja environment.

    Parameters:

    Name Type Description Default
    env Environment

    The Jinja2 environment to install translations into.

    required
    locale str | Locale

    The target locale.

    required
    dirs Sequence[str | PathLike[str]]

    Sequence of directory paths to search for translations. Directories listed first

    required
    Source code in src/jinjarope/localization.py
     86
     87
     88
     89
     90
     91
     92
     93
     94
     95
     96
     97
     98
     99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    def install_translations(
        env: jinja2.Environment, locale: str | Locale, dirs: Sequence[str | os.PathLike[str]]
    ) -> None:
        """Install translations for the given locale in the Jinja environment.
    
        Args:
            env: The Jinja2 environment to install translations into.
            locale: The target locale.
            dirs: Sequence of directory paths to search for translations.
                  Directories listed first
        """
        if isinstance(locale, str):
            locale = parse_locale(locale)
        if HAS_BABEL:
            env.add_extension("jinja2.ext.i18n")
            translations = _get_merged_translations(dirs, "locales", locale)
            if translations is not None:
                env.install_gettext_translations(translations)  # type: ignore[attr-defined]
            else:
                env.install_null_translations()  # type: ignore[attr-defined]
                if locale.language != "en":
                    logger.warning(
                        "Translations couldnt be found for locale %r, defaulting to English",
                        locale,
                    )
        else:  # pragma: no cover
            # no babel installed, add dummy support for trans/endtrans blocks
            env.add_extension(NoBabelExtension)
            env.install_null_translations()  # type: ignore[attr-defined]
    

    parse_locale

    parse_locale(locale: str) -> Locale
    

    Parse a locale string into a Locale object.

    Parameters:

    Name Type Description Default
    locale str

    The locale string to parse (e.g., 'en_US').

    required

    Returns:

    Name Type Description
    Locale Locale

    The parsed Babel Locale object.

    Raises:

    Type Description
    RuntimeError

    If the locale string is invalid.

    Source code in src/jinjarope/localization.py
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    def parse_locale(locale: str) -> Locale:
        """Parse a locale string into a Locale object.
    
        Args:
            locale: The locale string to parse (e.g., 'en_US').
    
        Returns:
            Locale: The parsed Babel Locale object.
    
        Raises:
            RuntimeError: If the locale string is invalid.
        """
        try:
            return Locale.parse(locale, sep="_")
        except (ValueError, UnknownLocaleError, TypeError) as e:
            msg = f"Invalid value for locale: {e}"
            raise RuntimeError(msg) from e