Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to format currency using react i18next library

I have a create-react-app and I need to add i18n support to it. I'm planning to use https://github.com/i18next but in the documentation I couldnt find how to format currencies.

Does anybody how to format currencies using react i18next library?

Thank you so much for your help

like image 218
user2490936 Avatar asked Dec 15 '25 04:12

user2490936


1 Answers

You can use Intl.NumberFormat in order to format the currency.

In order to integrate it with i18next I've used the format option.

i18next.init({
  lng: 'en',
  debug: false,
  resources: {
    en: {
      translation: {
        "key": "{{value, price, EUR}}"
      }
    }
  },
  interpolation: {
    format: (value, rawFormat, lng) => {
      const [format, ...additionalValues] = rawFormat.split(',').map((v) => v.trim());
      switch (format) {
        case 'uppercase':
          return value.toUpperCase();
        case 'price':
          return Intl.NumberFormat(lng, {
            style: 'currency',
            currency: additionalValues[0]
          }).format(value);
      }
    }
  }
}).then(function(t) {
  // initialized and ready to go!
  document.getElementById('output').innerHTML = t('key', {
    value: 1000.01
  });
});
<script src="https://unpkg.com/i18next/dist/umd/i18next.min.js"></script>

<div id="output"></div>
like image 168
felixmosh Avatar answered Dec 16 '25 19:12

felixmosh



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!