Skip to content
This repository has been archived by the owner on Aug 26, 2022. It is now read-only.

Commit

Permalink
bug 1399639: Use locale name for statici18n path
Browse files Browse the repository at this point in the history
django-statici18n is now using the locale name, such as en_US, rather
than the standardized language code, like en-US. Replace that dash with
an underscore, and update some utility functions to be clear about
locale names versus language codes (which may be important as we retire
the custom locale middleware).
  • Loading branch information
jwhitlock committed Apr 5, 2018
1 parent 13c7f38 commit ff71078
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 13 deletions.
13 changes: 9 additions & 4 deletions kuma/core/templatetags/jinja_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,11 @@
from django_jinja import library
from pytz import timezone, utc
from soapbox.models import Message
from statici18n.templatetags.statici18n import statici18n
from statici18n.templatetags.statici18n import statici18n as lib_statici18n
from urlobject import URLObject

from ..urlresolvers import reverse, split_path
from ..utils import format_date_time, urlparams
from ..utils import format_date_time, language_to_locale, urlparams


htmlparser = HTMLParser.HTMLParser()
Expand All @@ -29,8 +29,6 @@
library.filter(defaultfilters.linebreaksbr)
library.filter(strip_tags)
library.filter(defaultfilters.truncatewords)
library.global_function(statici18n)

library.filter(urlparams)


Expand All @@ -40,6 +38,13 @@ def paginator(pager):
return Paginator(pager).render()


@library.global_function
def statici18n(language):
"""Get path to JS i18n file by Kuma langugage code."""
locale = language_to_locale(language)
return lib_statici18n(locale)


@library.global_function
def url(viewname, *args, **kwargs):
"""Helper for Django's ``reverse`` in templates."""
Expand Down
24 changes: 15 additions & 9 deletions kuma/core/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -393,17 +393,16 @@ def format_date_time(request, value, format='shortdatetime'):
except AttributeError:
pass

locale = _babel_locale(_get_request_locale(request))
locale = _get_request_locale(request)

try:
formatted = format_date_value(value, tzvalue, locale, format)
except KeyError:
# Babel sometimes stumbles over missing formatters in some locales
# e.g. bug #1247086
# we fall back formatting the value with the default language code
formatted = format_date_value(value, tzvalue,
_babel_locale(settings.LANGUAGE_CODE),
format)
formatted = format_date_value(
value, tzvalue, language_to_locale(settings.LANGUAGE_CODE), format)

return formatted, tzvalue

Expand All @@ -413,7 +412,7 @@ def _get_request_locale(request):
locale = request.LANGUAGE_CODE
if not localedata.exists(locale):
locale = settings.LANGUAGE_CODE
return locale
return language_to_locale(locale)


def format_date_value(value, tzvalue, locale, format):
Expand All @@ -439,7 +438,14 @@ def format_date_value(value, tzvalue, locale, format):
raise DateTimeFormatError


def _babel_locale(locale):
"""Return the Babel locale code, given a normal one."""
# Babel uses underscore as separator.
return locale.replace('-', '_')
def language_to_locale(language_code):
"""
Convert language codes to locale names used by Babel, Django
Kuma uses a dash for regions, like en-US, zh-CN.
Babel and Django use underscore, like en_US, zh_CN.
The codes are identical when there is no region, like fr, es.
https://docs.djangoproject.com/en/1.11/topics/i18n/#definitions
"""
return language_code.replace('-', '_')

0 comments on commit ff71078

Please sign in to comment.