[openstreetmap/openstreetmap-website] Update to i18n-js 4.x (PR #5811)
Anton Khorev
notifications at github.com
Mon Mar 17 14:06:15 UTC 2025
@AntonKhorev commented on this pull request.
>
// '-' are replaced with '_' in https://github.com/eemeli/make-plural/tree/main/packages/plurals
const pluralizer = plurals[locale.replace(/\W+/g, "_")] || plurals[locale.split("-")[0]];
if (pluralizer) {
- I18n.pluralization[locale] = (count) => [pluralizer(count), "other"];
+ OSM.i18n.pluralization.register(locale, I18n.useMakePlural({ pluralizer: locale }));
If you want to write this according to i18n-js docs, it should be:
```js
OSM.i18n.pluralization.register(locale, I18n.useMakePlural({ pluralizer }));
```
However that's not the best you can do.
English has `one` / `other` forms. Some languages have `one` / `few` / `many` forms. Let's suppose that there's a string with `count` that's not yet translated into such language. First, `count` goes to the pluralizer and the pluralizer says *it has to be `many` form*. The string with `many` key is looked up and it's missing. Then a fallback to English happens, the string is looked up and it's missing again because English has no `many` form. The user gets to see an error message instead of any fallback:

That's why I wrote the pluralizer wrapper like this for i18n-js v3:
```js
I18n.pluralization[locale] = (count) => [pluralizer(count), "other"];
```
It has a built-in fallback to `other` which is going to be present in English. It's not entirely correct because rules for `one` form can also be different and you sometimes get fallbacks to English `one` instead of `other`, but usually it's bearable.
The equivalent for i18n-js v4 looks like this:
```js
OSM.i18n.pluralization.register(locale, (_, count) => [pluralizer(count), "other"]);
```
--
Reply to this email directly or view it on GitHub:
https://github.com/openstreetmap/openstreetmap-website/pull/5811#pullrequestreview-2690650937
You are receiving this because you are subscribed to this thread.
Message ID: <openstreetmap/openstreetmap-website/pull/5811/review/2690650937 at github.com>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.openstreetmap.org/pipermail/rails-dev/attachments/20250317/e38eda6a/attachment-0001.htm>
More information about the rails-dev
mailing list