Using i18next i18next-intervalplural-postprocessor with Jovo


#1

I want to be able to extend i18next that is used by Jovo to include interval plurals: i18next-intervalplural-postprocessor

The docs are here:
https://www.i18next.com/translation-function/plurals#interval-plurals

My current i18n configuration is:

  i18n: {
    returnNull: false,
    fallbackLng: 'en-US',
  },

@alexsam What do I need to do to use this?


#2

Can you try this snippet?

const intervalPlural = require('i18next-intervalplural-postprocessor');

app.middleware('setup').use((handleRequest) => {
    handleRequest.app.$cms.I18Next.i18n.use(intervalPlural);
});

#3

What file would this code go in? app.js?
Should I add it: 1) after const app = new App();, 2) after all app.use calls, 3) after any app.hook calls, 4) after calling app.setHandler, or 5) doesn’t matter where it goes really?


#4

Correct, in app.js

Mostly it doesn’t matter. In this case you can add it everywhere.

I would add it after the ‘base’-initialization, means after:

app.use(
    new Alexa(),
    new GoogleAssistant(),
    new JovoDebugger(),
    new FileDb()
);

Btw. There’s a hidden/for internal use command called performance-report. It shows all middlewares that are called in a request.

Try jovo run -- --performance-report


#5

I added the code to app.js:

\\ app.js
const intervalPlural = require('i18next-intervalplural-postprocessor');
const app = new App();

app.middleware('setup').use((handleRequest) => {
  handleRequest.app.$cms.I18Next.i18n.use(intervalPlural);
});

The above code is called, but the interval configuratoin doesn’t work.

\\ i18n/en-US.json

"Section": {
      "test": "Only one item",
      "test_interval": "(0){No items. Nope. None.};",
      "test_plural": "{{count}} items"
}

And also:

\\ handler.js
const text = this.t('Section.test',
{ postProcess: 'interval', count: myArray.length });

I also tried setting the postProcess value in config:

\\ config.js

  i18n: {
    returnNull: false,
    fallbackLng: 'en-US',
    postProcess: ['interval'],
  },

The processing when the count is 0 still uses _plural instead of _interval

I tried the middleware setup call at various places in app.js and it still doesn’t work.

Thoughts?


#6

Maybe it’s the missing _interval in the key?

console.log(this.t('Section.test_interval', { postProcess: 'interval', count: 1 }));
 // -> returns Only one item
console.log(this.t('Section.test_interval', { postProcess: 'interval', count: 2 }));
 // -> returns  2 items
console.log(this.t('Section.test_interval', { postProcess: 'interval', count: 0 }));
 // -> returns No items. Nope. None.

This is how they explain it in their docs (https://www.i18next.com/translation-function/plurals#interval-plurals)


#7

That was it! Not sure how I missed the “_interval” as part of the key name. That was not what I had expected. Thanks for the help!