Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save jonashaag/c69b5213d18b2fd8da1ef7b0ee61b9f8 to your computer and use it in GitHub Desktop.
Save jonashaag/c69b5213d18b2fd8da1ef7b0ee61b9f8 to your computer and use it in GitHub Desktop.
Django change translation fallback language
# 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