Skip to content

Commit

Permalink
Merge branch 'k-nut-3073-enable-chinese-locale'
Browse files Browse the repository at this point in the history
  • Loading branch information
amercader committed Jun 3, 2016
2 parents ae9e163 + 1e747f6 commit 4bffd8c
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 9 deletions.
39 changes: 31 additions & 8 deletions ckan/lib/i18n.py
Expand Up @@ -2,8 +2,10 @@

import os

from babel import Locale, localedata
from babel.core import LOCALE_ALIASES, get_locale_identifier
from babel import Locale
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 @@ -50,7 +52,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 understand
# 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 Expand Up @@ -125,11 +138,21 @@ def get_available_locales():
e.g. [ Locale('en'), Locale('de'), ... ] '''
global available_locales
if not available_locales:
available_locales = map(Locale.parse, get_locales())
# Add the full identifier (eg `pt_BR`) to the locale classes, as it does
# not offer a way of accessing it directly
for locale in available_locales:
setattr(locale, 'identifier', get_identifier_from_locale_class(locale))
available_locales = []
for locale in get_locales():
# Add the short names for the locales. This equals the filename
# of the ckan translation files as opposed to the long name
# that includes the script which is generated by babel
# so e.g. `zn_CH` instead of `zn_Hans_CH` this is needed
# to properly construct urls with url_for
parsed_locale = Locale.parse(locale)
parsed_locale.short_name = locale

# Add the full identifier (eg `pt_BR`) to the locale classes,
# as it does not offer a way of accessing it directly
parsed_locale.identifier = \
get_identifier_from_locale_class(parsed_locale)
available_locales.append(parsed_locale)
return available_locales


Expand Down
2 changes: 1 addition & 1 deletion ckan/templates/snippets/language_selector.html
Expand Up @@ -4,7 +4,7 @@
<label for="field-lang-select">{{ _('Language') }}</label>
<select id="field-lang-select" name="url" data-module="autocomplete" data-module-dropdown-class="lang-dropdown" data-module-container-class="lang-container">
{% for locale in h.get_available_locales() %}
<option value="{% url_for current_url, locale=locale %}" {% if locale.identifier == current_lang %}selected="selected"{% endif %}>
<option value="{% url_for current_url, locale=locale.short_name %}" {% if locale.identifier == current_lang %}selected="selected"{% endif %}>
{{ locale.display_name or locale.english_name }}
</option>
{% endfor %}
Expand Down

0 comments on commit 4bffd8c

Please sign in to comment.