i18next / i18next-icu

i18nFormat plugin to use ICU format with i18next
MIT License
81 stars 17 forks source link

ICU will ignore the first blank in plural #29

Closed acdzh closed 3 years ago

acdzh commented 3 years ago

🐛 Bug Report

Hello! I found the icu will ignore the first blank in plural, just like this demo. Please look at the { a} in key1. Is this a bug or just a feature? Is there any way to solve this problem? Because our i18n translations are auto-generated like key1, It is diffcullt to change the translation to like key2. Thanks!

image

To Reproduce

codepen

or

<div>
  <div>key1: {count} apple{num, plural, one { a} other{s two}} day, keep doctor away.</div><br />
  <div>1 apple: <div id="div11"></div></div><br />
  <div>2 apple: <div id="div12"></div></div><br />
</div>
<br />
<div>
  <div>key2: {count} {num, plural, one {apple a} other{apples two}} day, keep doctor away.</div><br />
  <div>1 apple: <div id="div21"></div></div><br />
  <div>2 apple: <div id="div22"></div></div><br />
</div>
const translation = {
  key1: '{count} apple{num, plural, one { a} other{s two}} day, keep doctor away.',
  key2: '{count} {num, plural, one {apple a} other{apples two}} day, keep doctor away.'
};

i18next.use(ICU).init({
  lng: "en",
  resources: {
    en: { translation }
  }
});

const div11 = document.getElementById('div11');
const div12 = document.getElementById('div12');
const div21 = document.getElementById('div21');
const div22 = document.getElementById('div22');

div11.innerText = i18next.t('key1', {count: 1, num: 1});
div12.innerText = i18next.t('key1', {count: 2, num: 2});
div21.innerText = i18next.t('key2', {count: 1, num: 1});
div22.innerText = i18next.t('key2', {count: 2, num: 2});

Expected behavior

We want the key1 and key2 have the same result.

Your Environment

jamuhl commented 3 years ago

the ICU variant of i18next depends on the implementation used in react-intl: https://github.com/i18next/i18next-icu/blob/master/package.json#L18

so, if an issue it would need to be solved there...

but honestly I recommend you to not build up your strings like you do as messageformat encourages having full sentences

{
   count, plural,
     one {One apple a day, keep the doctor away}
   other {# apples a day, keep the doctor away }
 }
acdzh commented 3 years ago

the ICU variant of i18next depends on the implementation used in react-intl: https://github.com/i18next/i18next-icu/blob/master/package.json#L18

so, if an issue it would need to be solved there...

but honestly I recommend you to not build up your strings like you do as messageformat encourages having full sentences

{
   count, plural,
     one {One apple a day, keep the doctor away}
   other {# apples a day, keep the doctor away }
 }

tks