Search code examples
typescripti18next

i18next - interpolation parameters can only be of type number


I am using i18next to get proper translation for my application. This is the json file I am using for the i18n keys:

{
  "text": "New count is {{count}}"
}

In my index.ts file I am now executing these two commands:

const translatedText = i18n.t("text", { count: "test" });
const translatedText2 = i18n.t("text", { count: 1 });

But I get a typescript error for the first line telling me that the value for parameter count can only be of type number. Why is that so?

You can test the code here: https://stackblitz.com/edit/vitejs-vite-wgqvtk?file=package.json


Solution

  • Use cnt or other words instead of count.

    {
      "text": "New count is {{cnt}}"
    }
    
    const translatedText = i18n.t("text", { cnt: "test" });
    const translatedText2 = i18n.t("text", { cnt: 1 });
    

    The t function has an option object parameter which contains the count key. It's a number type. https://www.i18next.com/translation-function/essentials#overview-options.