def translation_dummy(language): """Return dummy translation object to please Django's l10n while Live Translation is enabled. """ t = trans_real._translations.get(language, None) if t is not None: return t dummytrans = trans_real.DjangoTranslation() dummytrans.set_language(language) #FIXME: the need for the _catalog attribute means we are not hijacking # gettext early enough. dummytrans._catalog = {} dummytrans.plural = lambda x: x trans_real._translations[language] = dummytrans return dummytrans
def merge_translation(self, context): """ Context wrapper which modifies the given language's translation catalog using the i18n service, if found. """ language = get_language() i18n_service = context.get('_i18n_service', None) if i18n_service: # Cache the original translation object to reduce overhead if language not in self._translations: self._translations[language] = trans_real.DjangoTranslation( language) translation = trans_real.translation(language) translation.merge(i18n_service) yield # Revert to original translation object if language in self._translations: trans_real._translations[language] = self._translations[language] # Re-activate the current language to reset translation caches trans_real.activate(language)