﻿id	summary	reporter	owner	description	type	status	component	version	severity	resolution	keywords	cc	stage	has_patch	needs_docs	needs_tests	needs_better_patch	easy	ui_ux
26328	jsi18n - get_javascript_catalog obscure and hardcoded english	Cristiano Coelho	nobody	"This is a ticket to get some support in removing the actual hardcoded english fallback (even if default language is not english) in get_javascript_catalog.

Discussion here: https://groups.google.com/forum/#!topic/django-developers/gDU8HlZg4Ug

But basically, the current jsi18n will always load english as a fallback language, which will be then overriten by what ever your default language is, but may cause some issues if you don't want to add message strings to your default language (because ids == values) and you end up with english translations.

The proposed change is to remove this english loading completely, and only load the default language as fallback, which will then match the server side behaviour for translations where the above is not an issue.

Below is the complete code of get_javascript_catalog modified, if there are no issues with this change I would like to do a pull request for it.


{{{
def get_javascript_catalog(locale, domain, packages):
    default_locale = to_locale(settings.LANGUAGE_CODE)
    app_configs = apps.get_app_configs()
    allowable_packages = set(app_config.name for app_config in app_configs)
    allowable_packages.update(DEFAULT_PACKAGES)
    packages = [p for p in packages if p in allowable_packages]
    t = {}
    paths = []

    # paths of requested packages
    for package in packages:
        p = importlib.import_module(package)
        path = os.path.join(os.path.dirname(upath(p.__file__)), 'locale')
        paths.append(path)
    # add the filesystem paths listed in the LOCALE_PATHS setting
    paths.extend(reversed(settings.LOCALE_PATHS))

    # First load the settings.LANGUAGE_CODE translations as fallback
    for path in paths:
        try:
            catalog = gettext_module.translation(domain, path, [default_locale])
        except IOError:
            catalog = None
        if catalog is not None:
            t.update(catalog._catalog)

    # finally load the currently selected language, if it isn't identical to the default.
    if locale != default_locale:        
        for path in paths:
            try:
                catalog = gettext_module.translation(domain, path, [locale])
            except IOError:
                catalog = None
            if catalog is not None:
                t.update(catalog._catalog)

    plural = None
    if '' in t:
        for l in t[''].split('\n'):
            if l.startswith('Plural-Forms:'):
                plural = l.split(':', 1)[1].strip()

        #This is to prevent the unnecessary if condition on the below for loop.
        del t['']

    if plural is not None:
        # this should actually be a compiled function of a typical plural-form:
        # Plural-Forms: nplurals=3; plural=n%10==1 && n%100!=11 ? 0 :
        #               n%10>=2 && n%10<=4 && (n%100<10 || n%100>=20) ? 1 : 2;
        plural = [el.strip() for el in plural.split(';') if el.strip().startswith('plural=')][0].split('=', 1)[1]

    pdict = {}
    maxcnts = {}
    catalog = {}
    for k, v in t.items():        
        if isinstance(k, six.string_types):
            catalog[k] = v
        elif isinstance(k, tuple):
            msgid = k[0]
            cnt = k[1]
            maxcnts[msgid] = max(cnt, maxcnts.get(msgid, 0))
            pdict.setdefault(msgid, {})[cnt] = v
        else:
            raise TypeError(k)
    for k, v in pdict.items():
        catalog[k] = [v.get(i, '') for i in range(maxcnts[msgid] + 1)]

    return catalog, plural
}}}
"	Bug	new	Internationalization	1.9	Normal		jsi18n	cristianocca@…	Unreviewed	1	0	0	0	0	0
