1

Recently I came across a strange behavior, I am not sure whether is my fault or a bug ... (probably my fault though) I have the following Trans Component:

<Trans i18nKey="projectDetailList.pledgesInfo" count={promotions.length}>
    <p>
        Explore <span>{{promotionLength: promotions.length}} pledge</span>
    </p>
</Trans>

in my locale translation.json files its keys (singular and plural) are declared as follows both for english and italian language:

locales/en/translation.json
...
"projectDetailList": {
    "noPromotionsFound": "No promotions found for this project! Try again later!",
    "pledgesInfo": "<0>Explore <1>{{count}} pledge</1></0>",
    "pledgesInfo_plural": "<0>Explore all <1>{{count}} pledges</1></0>"
}
...
locales/it/translation.json
...
"projectDetailList": {
    "noPromotionsFound": "Nessuna promozione trovata per questo progetto! Prova più tardi!",
    "pledgesInfo": "<0>Esplora <1>{{count}} pledge</1></0>",
    "pledgesInfo_plural": "<0>Esplora tutti i <1>{{count}} pledges</1></0>"
}
...

It works like a charm, and correctly translate, select the appropriate key for plural and interpolate variables, however anytime I launch i18next-scanner both the keys pledgesInfo_plural are removed from both the locale files... what am I doing wrong? As anyone experienced this issue?

That's my i18next-scanner.config.js file:

module.exports = {
  options: {
    debug: false,
    sort: true,
    removeUnusedKeys: true,
    func: {
      list: ["t"],
      extensions: [".js"]
    },
    trans: {
      component: "Trans",
      i18nKey: "i18nKey",
      defaultsKey: "defaults",
      extensions: [".js"],
      fallbackKey: function(ns, value) {
        // Returns a hash value as the fallback key
        return sha1(value);
      }
    },
    lngs: ["en", "it"],
    ns: ["translation"],
    defaultLng: "en",
    defaultNs: "translation",
    defaultValue: "",
    resource: {
      loadPath: "translations/locales/{{lng}}/{{ns}}.json",
      savePath: "translations/locales/{{lng}}/{{ns}}.json",
      jsonIndent: 2,
      lineEnding: "\n"
    },
    nsSeparator: ":", // namespace separator
    keySeparator: ".", // key separator
    interpolation: {
      prefix: "{{",
      suffix: "}}"
    }
  }
};

Thanks for your patience!

remix23
  • 2,632
  • 2
  • 11
  • 21
  • As I can see you are assigning same {{count}} for both singular and plural values. For plural values, count should have to be > 1. – Thilina Koggalage Nov 14 '19 at 08:10
  • I believe that, based on the value of `count`, i18next would select the appropriate key (`pledgesInfo_plural` or `pledgesInfo`) also according to the third code snippet on [docs](https://react.i18next.com/latest/trans-component) – Raffaele Tosti Nov 14 '19 at 08:29
  • a bad hack to avoid keys removal could be sets `removeUnusedKeys: false` since it seems like i18next-scanner treat plurar keys as unused – Raffaele Tosti Nov 19 '19 at 15:05
  • Hey how can one remove unused keys then? I am facing a similar challenge too but I need to remove some unused keys – sikaili99 Aug 30 '22 at 22:30

0 Answers0