Forked from jonashaag/django_change_translation_fallback_language.py
Created
June 29, 2021 08:00
-
-
Save LowerDeez/25d867d21dfa1d3676fa0e3f0556df0c to your computer and use it in GitHub Desktop.
Django change translation fallback language
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
# This is a hack so that French translation falls back to English translation, | |
# not German translation (which is the default locale and the original | |
# strings). | |
from django.utils.translation import trans_real | |
class MyDjangoTranslation(trans_real.DjangoTranslation): | |
def _add_fallback(self, localedirs=None): | |
if self._DjangoTranslation__language[:2] in {'de', 'en'}: | |
return super()._add_fallback(localedirs) | |
else: | |
# Special case for French | |
if self.domain == 'django': | |
default_translation = trans_real.translation('en_GB') | |
else: | |
default_translation = self.__class__( | |
'en_GB', domain=self.domain, localedirs=localedirs) | |
self.add_fallback(default_translation) | |
trans_real.DjangoTranslation = MyDjangoTranslation |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment