| 8 | | |
| 9 | | 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. |
| 10 | | |
| 11 | | |
| 12 | | {{{ |
| 13 | | def get_javascript_catalog(locale, domain, packages): |
| 14 | | default_locale = to_locale(settings.LANGUAGE_CODE) |
| 15 | | app_configs = apps.get_app_configs() |
| 16 | | allowable_packages = set(app_config.name for app_config in app_configs) |
| 17 | | allowable_packages.update(DEFAULT_PACKAGES) |
| 18 | | packages = [p for p in packages if p in allowable_packages] |
| 19 | | t = {} |
| 20 | | paths = [] |
| 21 | | |
| 22 | | # paths of requested packages |
| 23 | | for package in packages: |
| 24 | | p = importlib.import_module(package) |
| 25 | | path = os.path.join(os.path.dirname(upath(p.__file__)), 'locale') |
| 26 | | paths.append(path) |
| 27 | | # add the filesystem paths listed in the LOCALE_PATHS setting |
| 28 | | paths.extend(reversed(settings.LOCALE_PATHS)) |
| 29 | | |
| 30 | | # First load the settings.LANGUAGE_CODE translations as fallback |
| 31 | | for path in paths: |
| 32 | | try: |
| 33 | | catalog = gettext_module.translation(domain, path, [default_locale]) |
| 34 | | except IOError: |
| 35 | | catalog = None |
| 36 | | if catalog is not None: |
| 37 | | t.update(catalog._catalog) |
| 38 | | |
| 39 | | # finally load the currently selected language, if it isn't identical to the default. |
| 40 | | if locale != default_locale: |
| 41 | | for path in paths: |
| 42 | | try: |
| 43 | | catalog = gettext_module.translation(domain, path, [locale]) |
| 44 | | except IOError: |
| 45 | | catalog = None |
| 46 | | if catalog is not None: |
| 47 | | t.update(catalog._catalog) |
| 48 | | |
| 49 | | plural = None |
| 50 | | if '' in t: |
| 51 | | for l in t[''].split('\n'): |
| 52 | | if l.startswith('Plural-Forms:'): |
| 53 | | plural = l.split(':', 1)[1].strip() |
| 54 | | |
| 55 | | #This is to prevent the unnecessary if condition on the below for loop. |
| 56 | | del t[''] |
| 57 | | |
| 58 | | if plural is not None: |
| 59 | | # this should actually be a compiled function of a typical plural-form: |
| 60 | | # Plural-Forms: nplurals=3; plural=n%10==1 && n%100!=11 ? 0 : |
| 61 | | # n%10>=2 && n%10<=4 && (n%100<10 || n%100>=20) ? 1 : 2; |
| 62 | | plural = [el.strip() for el in plural.split(';') if el.strip().startswith('plural=')][0].split('=', 1)[1] |
| 63 | | |
| 64 | | pdict = {} |
| 65 | | maxcnts = {} |
| 66 | | catalog = {} |
| 67 | | for k, v in t.items(): |
| 68 | | if isinstance(k, six.string_types): |
| 69 | | catalog[k] = v |
| 70 | | elif isinstance(k, tuple): |
| 71 | | msgid = k[0] |
| 72 | | cnt = k[1] |
| 73 | | maxcnts[msgid] = max(cnt, maxcnts.get(msgid, 0)) |
| 74 | | pdict.setdefault(msgid, {})[cnt] = v |
| 75 | | else: |
| 76 | | raise TypeError(k) |
| 77 | | for k, v in pdict.items(): |
| 78 | | catalog[k] = [v.get(i, '') for i in range(maxcnts[msgid] + 1)] |
| 79 | | |
| 80 | | return catalog, plural |
| 81 | | }}} |