Opened 12 months ago

Last modified 9 months ago

#35278 assigned Bug

`ngettext` result can be possibly undefined. — at Initial Version

Reported by: Piotr Kawula Owned by: Piotr Kawula
Component: Internationalization Version: 5.0
Severity: Normal Keywords: ngettext, catalog, i18n, internationalization,
Cc: Piotr Kawula, Claude Paroz Triage Stage: Accepted
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: yes
Easy pickings: no UI/UX: no
Pull Requests:17949 build:success, 18249 unmerged

Description

When the translation engine provide an invalid plural rule the pluralidx function can return invalid index which will cause the ngettext to return value that does not exist - undefined.
Same result can occur when the newcatalog contains invalid values, for example:

const newcatalog = {
  '%s something': [],
  ...
}

And in ngettext we have:

django.ngettext = function(singular, plural, count) {
 const value = django.catalog[singular];
 if (typeof value === 'undefined') {
    return (count == 1) ? singular : plural;
  } else {
    return value.constructor === Array ? value[django.pluralidx(count)] : value;
  }
};

Which in case of empty array go for value[django.pluralidx(count)] which is undefined.

I think we want the ngettext function to return string always, if it does not find proper value in catalog, should default to the provided values.

According to the ticket's flags, the next step(s) to move this issue forward are:

  • To improve the patch as described in the pull request review comments or on this ticket, then uncheck "Patch needs improvement".
  • If creating a new pull request, include a link to the pull request in the ticket comment when making that update. The usual format is: [https://github.com/django/django/pull/#### PR].

Change History (0)

Note: See TracTickets for help on using tickets.
Back to Top