Created
May 23, 2018 15:06
-
-
Save pgrunewald/08e73e95a390ee46d4bfabbc3a0293d8 to your computer and use it in GitHub Desktop.
Overriding language binding programmically
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class YourBrowserView: | |
def some_method(self): | |
original_lang = self.context.REQUEST['LANGUAGE'] | |
original_lang_binding = self.context.REQUEST['LANGUAGE_TOOL'] | |
target_language = 'de' | |
try: | |
self._switch_language(target_language, self._create_language_binding(target_language)) | |
# do something here in the language you want | |
finally: | |
self._switch_language(original_lang, original_lang_binding) | |
def _create_language_binding(self, lang): | |
"""Creates a new LanguageBinding object and sets the given language shortcut | |
:param lang: language shortcut i.e. de, en | |
:type lang: str | |
""" | |
binding = LanguageBinding(self.context) | |
binding.LANGUAGE = lang | |
binding.DEFAULT_LANGUAGE = lang | |
binding.LANGUAGE_LIST = [] | |
return binding | |
def _switch_language(self, lang, lang_binding): | |
"""Creates a new LanguageBinding object and sets the given language shortcut | |
:param lang: language shortcut i.e. de or en | |
:type lang: str | |
:param lang_binding: a language binding object | |
:type lang_binding: LanguageBinding | |
""" | |
self.context.REQUEST['LANGUAGE'] = lang | |
self.context.REQUEST['LANGUAGE_TOOL'] = lang_binding |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment