Skip to content

Instantly share code, notes, and snippets.

@senko
Created March 21, 2013 14:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save senko/5213276 to your computer and use it in GitHub Desktop.
Save senko/5213276 to your computer and use it in GitHub Desktop.
Compile translations for all languages in a single map, for Django.
from gettext import translation
import os.path
from django.conf import settings
def get_all_translations(domain):
"""Return a language code => translations mapping for all defined
languages. This is useful if in a single view you need to use
different translation catalogs at the same time.
"""
allcat = {}
paths = [os.path.join(settings.ROOT_DIR, app, 'locale') for
app in settings.INSTALLED_APPS if
os.path.exists(os.path.join(settings.ROOT_DIR, app, 'locale'))]
paths.extend(reversed(settings.LOCALE_PATHS))
for code, _ in settings.LANGUAGES:
allcat[code] = {}
for path in paths:
try:
t = translation(domain, path, [code])
allcat[code].update(t._catalog)
except IOError:
pass
if '' in allcat[code]:
del allcat[code]['']
return allcat
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment