Created
March 21, 2013 14:08
-
-
Save senko/5213276 to your computer and use it in GitHub Desktop.
Compile translations for all languages in a single map, for Django.
This file contains hidden or 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
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