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

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

Make CACHED_TRANSLATIONS global, fix get_value typo
Make CACHED_TRANSLATIONS global, fix get_value typo

  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.cached_translations = {}
          self.default_translation = self.__get_translation(DEFAULT_LANGUAGE)
  
      def process(self, event):
-         lang = event.state.get_for("settings").get("language", DEFAULT_LANGUAGE)
+         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(self, language):
+     def __get_translation(language):
-         if language in self.cached_translations:
-             return self.cached_translations[language]
+         if language in CACHED_TRANSLATIONS:
+             translation = CACHED_TRANSLATIONS[language]
+         else:
-         translation = gettext.translation(TRANSLATION_DOMAIN, LOCALE_DIR, languages=[language], fallback=True)
+             translation = gettext.translation(TRANSLATION_DOMAIN, LOCALE_DIR, languages=[language], fallback=True)
-         self.cached_translations[language] = translation
+             CACHED_TRANSLATIONS[language] = translation
          return translation
  
Make CACHED_TRANSLATIONS global, fix get_value typo
## Code 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

## Instruction:
Make CACHED_TRANSLATIONS global, fix get_value typo
## Code 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
