Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bug: Crash with missing pluralization key #39

Closed
kiskoza opened this issue Dec 13, 2022 · 2 comments · Fixed by #40
Closed

Bug: Crash with missing pluralization key #39

kiskoza opened this issue Dec 13, 2022 · 2 comments · Fixed by #40

Comments

@kiskoza
Copy link
Contributor

kiskoza commented Dec 13, 2022

Description

My app's main language is English, but it needs to support a few others, e.g.: Arabic. Arabic does not have the same pluralization rules as English, so I had to add a custom code to use "zero", "one", "two", etc. Unfortunately there are some missing translations in the app, where I need to fall back to English. However, when the pluralization key is not present, it crashes instead of giving the usual error message.

How to reproduce

import { I18n } from "i18n-js";

const i18n = new I18n({
  "ar": {
    "weeks": {
      "one": "1 week ago",
      "other": "%{count} weeks ago"
    }
  }
});

i18n.pluralization.register('ar', (_i18n, count) => {
  // Based on https://github.com/ruby-i18n/i18n/blob/master/test/test_data/locales/plurals.rb
  if (count === 0) {
    return ['zero'];
  }
  if (count === 1) {
    return ['one'];
  }
  if (count === 2) {
    return ['two'];
  }
  if ([3, 4, 5, 6, 7, 8, 9, 10].includes(count % 100)) {
    return ['few'];
  }
  if (Number.isInteger(count)) {
    return ['many'];
  }

  return ['other'];
});

i18n.locale = 'ar';

console.log(i18n.t("not_existing", { count: 1 })); // -> [missing "ar.not_existing" translation]
console.log(i18n.t("weeks", { count: 1 })); // -> 1 week ago
console.log(i18n.t("weeks", { count: 2 })); // -> Uncaught TypeError: message is undefined

What do you expect

It would be nice to get the same fallback as I have for missing keys (e.g.: [missing "ar.weeks.two" translation] )

What happened instead

It raises an error and the whole page crashes.

Software:

  • Package version: i18n-js@4.2.0
  • Browser: Firefox 104

Full backtrace

Uncaught TypeError: message is undefined
    interpolate ./node_modules/i18n-js/dist/import/helpers/interpolate.js:7
    pluralize ./node_modules/i18n-js/dist/import/helpers/pluralize.js:26
    pluralize ./node_modules/i18n-js/dist/import/I18n.js:118
    translate ./node_modules/i18n-js/dist/import/I18n.js:87
    <anonymous> ./src/index.js:43
@fnando
Copy link
Owner

fnando commented Dec 13, 2022

Thanks for reporting! I just released v4.2.1 with a fix for this.

@kiskoza
Copy link
Contributor Author

kiskoza commented Dec 14, 2022

Thanks, that was really quick. Upgraded the package version an it works for me 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants