#26328: jsi18n - get_javascript_catalog obscure and hardcoded english
-------------------------------------+-------------------------------------
Reporter: cristianocca | Owner: nobody
Type: Bug | Status: new
Component: | Version: 1.9
Internationalization |
Severity: Normal | Resolution:
Keywords: jsi18n | Triage Stage:
| Unreviewed
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by timgraham):
* needs_docs: => 0
* needs_tests: => 0
* needs_better_patch: => 0
Old description:
> 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
> }}}
New description:
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.
--
Comment:
Please submit a tested pull requested. I removed the code from the patch's
description since I don't think anyone will review it there (not to
mention, we cannot easily tell what changed).
I'm not sure what you're trying to say by describing the function as
"obscure" in the ticket summary. Could you try to rephrase it to describe
the desired changes?
Finally, if English fallbacks aren't loaded anymore, couldn't this be
backwards-incompatible for some user? (keep in mind, I don't have any
experience with this code so maybe there's no problem)
--
Ticket URL: <https://code.djangoproject.com/ticket/26328#comment:1>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.
--
You received this message because you are subscribed to the Google Groups
"Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
To view this discussion on the web visit
https://groups.google.com/d/msgid/django-updates/070.4af1bebfc99c9cc100085b14f0ab31fb%40djangoproject.com.
For more options, visit https://groups.google.com/d/optout.