Skip to content

Commit

Permalink
[#3073] Add short_name to locale for url construction
Browse files Browse the repository at this point in the history
  • Loading branch information
k-nut committed Jun 2, 2016
1 parent 1e4338e commit e54912e
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 7 deletions.
22 changes: 16 additions & 6 deletions ckan/lib/i18n.py
Expand Up @@ -49,7 +49,7 @@ def _get_locales():
else:
i18n_path = os.path.dirname(ckan.i18n.__file__)

# For every file in the ckan i18n directory see if babel can understan
# 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:
Expand Down Expand Up @@ -134,11 +134,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 e54912e

Please sign in to comment.