microapps / gatsby-plugin-react-i18next

Easily translate your Gatsby website into multiple languages
MIT License
121 stars 72 forks source link

Question: are custom format functions possible? #88

Open beardwin opened 3 years ago

beardwin commented 3 years ago

Hello!

I'm currently doing up my first Gatsby site. So far so good. Where I'm stuck right now is trying to properly format currencies in my translation, which doesn't seem to be working.

For example:

// en.json
{
  "foo": "This is going to cost {{some.object.value, EUR}}. That's a lotta Foos!"
}

I tried updating the i18nextOptions in my gatsby-config in a feeble attempt to create a custom formatter, such as the following:

{
  resolve: 'gatsby-plugin-react-i18next',
  options: {
    // ...buncha stuff,
    i18nextOptions: {
      interpolation: {
        escapeValue: false,
        format: (value, format, lng) => {
         console.log('>>> in custom formatter')
          if(format === 'EUR') {
            return new Intl.NumberFormat(lng, {style: 'currency', currency: format}).format(value)
          }
          return value
        }
      }
    }
  }
}

When i try the above I don't get any errors, but i also don't see anything emitted in the console, nor are my values in some.object.value being properly formatted, so I'm lead to believe the i18nextOptions.interpolation.format isn't doing anything.

Is there a way to achieve what i want here? This is my first foray into building a static site in like, 7 years, so.... to say I'm rusty would be an understatement.

cheers!

MaximeCoopengo commented 3 years ago

It seems that we can't pass functions in gatsby-config's options...

https://www.gatsbyjs.com/docs/how-to/plugins-and-themes/configuring-usage-with-plugin-options/#what-can-be-passed-in-as-options

grgcnnr commented 3 years ago

So this is a limitation of Gatsby's config API then and not an issue with (react-)i8next. We'll need to build our own clie3nt-side formatted and pass in the formatted date ready to go, is that correct.