Changes between Initial Version and Version 1 of Ticket #26328


Ignore:
Timestamp:
Mar 4, 2016, 4:31:38 PM (9 years ago)
Author:
Tim Graham
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)

Legend:

Unmodified
Added
Removed
Modified
  • Ticket #26328 – Description

    initial v1  
    66
    77The 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.
    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 }}}
Back to Top