Skip to content

Translator

Localization of Translations.

The localization of translations class contains the following parameters:

  • DEFAULT_LOCALE - Language code by default.
  • LANGUAGES - List of codes supported by languages.
  • RAMIFICE_TRANSLATIONS - Translations for Ramifice.
  • CUSTOM_TRANSLATIONS - Translations for custom project.
  • STUB_TRANSLATOR_FOR_ATTRIBUTES_OF_FIELD - Stub translator for attributes of fields.

The localization of translations class contains the following methods:

  • add_new_languages - Add new languages.
  • change_locale - Globally change the localization of translations.
  • ramifice_translator - Get translator for Ramifice.
  • custom_translator - Get translator for custom project.

Language Codes:

https://support.crowdin.com/developer/language-codes/

Hint - CKEditor supported languages:

af | ar | ast | az | bg | ca | cs | da | de | de_ch | el | en | en_au | en_gb | eo | es | et | eu | fa | fi | fr | gl | gu | he | hi | hr | hu | id | it | ja | km | kn | ko | ku | lt | lv | ms | nb | ne | nl | no | oc | pl | pt | pt_br | ro | ru | si | sk | sl | sq | sr | sr_latn | sv | th | tk | tr | tt | ug | uk | vi | zh | zh_cn

Translator

Manager by localization of translations.

Source code in src/ramifice/translator.py
@final
class Translator:
    """Manager by localization of translations."""

    # Language by default
    DEFAULT_LOCALE: ClassVar[str] = "en"
    # List of supported languages
    LANGUAGES: ClassVar[frozenset[str]] = frozenset(("en", "ru"))
    # Translations for Ramifice
    RAMIFICE_TRANSLATIONS: ClassVar[dict[str, _gettext.NullTranslations]] = {
        lang: _gettext.translation(
            domain="messages",
            localedir="config/translations/ramifice",
            languages=[lang],
            class_=None,
            fallback=True,
        )
        for lang in LANGUAGES
    }
    # Translations for custom project
    CUSTOM_TRANSLATIONS: ClassVar[dict[str, _gettext.NullTranslations]] = {
        lang: _gettext.translation(
            domain="messages",
            localedir="config/translations/custom",
            languages=[lang],
            class_=None,
            fallback=True,
        )
        for lang in LANGUAGES
    }
    # Stub translator for attributes of fields
    STUB_TRANSLATOR_FOR_ATTRIBUTES_OF_FIELD: ClassVar[Callable] = lambda message: message

    @classmethod
    def add_new_languages(cls, languages: frozenset[str]) -> None:
        """Add new languages."""
        cls.LANGUAGES.union(languages)

    @classmethod
    def ramifice_translator(cls, lang_code: str = "en", trust: bool = False) -> _gettext.NullTranslations:
        """Get translator for Ramifice."""
        # Return of the translator for Ramifice
        return cls.RAMIFICE_TRANSLATIONS.get(
            lang_code if trust or lang_code in cls.LANGUAGES else cls.DEFAULT_LOCALE,
            cls.RAMIFICE_TRANSLATIONS[cls.DEFAULT_LOCALE],
        )

    @classmethod
    def custom_translator(cls, lang_code: str = "en", trust: bool = False) -> _gettext.NullTranslations:
        """Get translator for custom project."""
        # Return custom translator
        return cls.CUSTOM_TRANSLATIONS.get(
            lang_code if trust or lang_code in cls.LANGUAGES else cls.DEFAULT_LOCALE,
            cls.CUSTOM_TRANSLATIONS[cls.DEFAULT_LOCALE],
        )

add_new_languages(languages) classmethod

Add new languages.

Source code in src/ramifice/translator.py
@classmethod
def add_new_languages(cls, languages: frozenset[str]) -> None:
    """Add new languages."""
    cls.LANGUAGES.union(languages)

custom_translator(lang_code='en', trust=False) classmethod

Get translator for custom project.

Source code in src/ramifice/translator.py
@classmethod
def custom_translator(cls, lang_code: str = "en", trust: bool = False) -> _gettext.NullTranslations:
    """Get translator for custom project."""
    # Return custom translator
    return cls.CUSTOM_TRANSLATIONS.get(
        lang_code if trust or lang_code in cls.LANGUAGES else cls.DEFAULT_LOCALE,
        cls.CUSTOM_TRANSLATIONS[cls.DEFAULT_LOCALE],
    )

ramifice_translator(lang_code='en', trust=False) classmethod

Get translator for Ramifice.

Source code in src/ramifice/translator.py
@classmethod
def ramifice_translator(cls, lang_code: str = "en", trust: bool = False) -> _gettext.NullTranslations:
    """Get translator for Ramifice."""
    # Return of the translator for Ramifice
    return cls.RAMIFICE_TRANSLATIONS.get(
        lang_code if trust or lang_code in cls.LANGUAGES else cls.DEFAULT_LOCALE,
        cls.RAMIFICE_TRANSLATIONS[cls.DEFAULT_LOCALE],
    )