Skip to content

Commit

Permalink
[#3073] smarter way to get available locales
Browse files Browse the repository at this point in the history
  • Loading branch information
k-nut committed Jun 1, 2016
1 parent 1885cba commit 1e4338e
Showing 1 changed file with 13 additions and 2 deletions.
15 changes: 13 additions & 2 deletions ckan/lib/i18n.py
@@ -1,7 +1,7 @@
import os

from babel import Locale, localedata
from babel.core import LOCALE_ALIASES, get_locale_identifier
from babel.core import LOCALE_ALIASES, get_locale_identifier, UnknownLocaleError
from babel.support import Translations
from paste.deploy.converters import aslist
from pylons import config
Expand Down Expand Up @@ -48,7 +48,18 @@ def _get_locales():
i18n_path = os.path.join(config.get('ckan.i18n_directory'), 'i18n')
else:
i18n_path = os.path.dirname(ckan.i18n.__file__)
locales += [l for l in os.listdir(i18n_path) if localedata.exists(l)]

# For every file in the ckan i18n directory see if babel can understan
# the locale. If yes, add it to the available locales
for locale in os.listdir(i18n_path):
try:
Locale.parse(locale)
locales.append(locale)
except (ValueError, UnknownLocaleError):
# Babel does not know how to make a locale out of this.
# This is fine since we are passing all files in the
# ckan.i18n_directory here which e.g. includes the __init__.py
pass

assert locale_default in locales, \
'default language "%s" not available' % locale_default
Expand Down

0 comments on commit 1e4338e

Please sign in to comment.