Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to apply a formatter in nested translations with i18next

I would like to format nested translations with i18next

Given the resources:

{
  translation: {
    en: {
      food: 'bread',
      food_is_good: "$t(food), that's not bad",
    },
  },
}

and a formatting function:

function format(value, format, lng) {
  if (value == undefined) return value;
  switch (format) {
    case 'capitalize':
      return _.capitalize(value);
    default:
      return value;
  }
}

that is used in the initialization of i18next:

...
  interpolation: { format: format },
...

I would like the output to be "Bread, that is not bad". So I hoped that something like:

{
  ...
  "food_is_good_1" : "$t(food,capitalize), that's not bad",
  "food_is_good_2" : "{{$t(food),capitalize}}, that's not bad",
  "food_is_good_3" : "{{food,capitalize}}, that's not bad",
  ...
}

would do the trick. The first option displays errors : "failed parsing options string in nesting" The last two options warn: missed to pass in variable food,capitalize for interpolating {{food,capitalize}}

like image 373
Arjan Avatar asked Aug 10 '16 08:08

Arjan


1 Answers

Your first option works in the latest i18next (v19).

https://codesandbox.io/s/react-i18next-forked-g1d47?file=/src/i18n.js

like image 68
felixmosh Avatar answered Oct 10 '22 18:10

felixmosh