<commit_before>import gettext

from bot.action.core.action import IntermediateAction

LOCALE_DIR = "locales"
TRANSLATION_DOMAIN = "telegram-bot"

DEFAULT_LANGUAGE = "en"


class InternationalizationAction(IntermediateAction):
    def __init__(self):
        super().__init__()
        self.cached_translations = {}
        self.default_translation = self.__get_translation(DEFAULT_LANGUAGE)

    def process(self, event):
        lang = event.state.get_for("settings").get("language", DEFAULT_LANGUAGE)
        translation = self.__get_translation(lang)
        translation.install()
        event._ = translation.gettext
        self._continue(event)
        self.default_translation.install()

    def __get_translation(self, language):
        if language in self.cached_translations:
            return self.cached_translations[language]
        translation = gettext.translation(TRANSLATION_DOMAIN, LOCALE_DIR, languages=[language], fallback=True)
        self.cached_translations[language] = translation
        return translation<commit_msg>Make CACHED_TRANSLATIONS global, fix get_value typo<commit_after>import gettext

from bot.action.core.action import IntermediateAction

LOCALE_DIR = "locales"
TRANSLATION_DOMAIN = "telegram-bot"

DEFAULT_LANGUAGE = "en"

CACHED_TRANSLATIONS = {}


class InternationalizationAction(IntermediateAction):
    def __init__(self):
        super().__init__()
        self.default_translation = self.__get_translation(DEFAULT_LANGUAGE)

    def process(self, event):
        lang = event.state.get_for("settings").get_value("language", DEFAULT_LANGUAGE)
        translation = self.__get_translation(lang)
        translation.install()
        event._ = translation.gettext
        self._continue(event)
        self.default_translation.install()

    @staticmethod
    def __get_translation(language):
        if language in CACHED_TRANSLATIONS:
            translation = CACHED_TRANSLATIONS[language]
        else:
            translation = gettext.translation(TRANSLATION_DOMAIN, LOCALE_DIR, languages=[language], fallback=True)
            CACHED_TRANSLATIONS[language] = translation
        return translation
{'fraction_non_alphanumeric': 0.08681522748375116, 'fraction_numerical': 0.0, 'mean_word_length': 3.7571743929359824, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3084441', 'n_tokens_mistral': 600, 'n_tokens_neox': 587, 'n_words': 107}