React-native-i18n: toCurrency() returning wrong currency even with correct currentLocale()

Created on 8 May 2017  路  5Comments  路  Source: AlexanderZaytsev/react-native-i18n

I'm using the latest master which fixed the currentLocale() bug and on my tests it returns the correct locale (pt-Br). Nevertheless, when trying to use the method toCurrency() it shows the currency on the default Locale 'en' instead of the correct one 'pt-Br'.

Thanks.

Most helpful comment

Yeah I made a workaround as well. Slightly different approach. Added a new function as a workaround:

//overwrite to Currency function
let newConvert = (number, precision) => {
    return I18n.toCurrency(number, {
        unit:I18n.t("currency"), 
        format:I18n.t("cuFormat"), 
        separator:I18n.t("cuSeparator"), 
        delimiter:I18n.t("cuDelimiter"), 
        precision: precision
    })
}

I18n.cu = newConvert

and added the values currency, cuFormat, cuSeparator, cuDelimiter to my language files.
Maybe its helpful for someone.

All 5 comments

Same here.
using I18n.toCurrency(10000) as well as I18n.l("currency", 10000) seems to ignore the locale and always returns US formatting.

Same problem. Currency and number formatting rely on the default locale, not the system one.

The workaround I currently use is to use toNumber / toCurrency with my own format settings:

render() {
    const currentLocale = I18n.currentLocale();
    const countryCode = currentLocale.split('-')[0];

    return (
      <View>
        <Text>{I18n.toNumber(1990.99, I18n.translations[countryCode].NUMBER_FORMAT)}</Text>
        <Text>{I18n.toNumber(1000, {...I18n.translations[countryCode].NUMBER_FORMAT, precision: 0})}</Text>
      </View>
    );
  }

Number formats are stored with other localization info:

I18n.translations = {
  ru: {
    greeting: '袩褉懈胁械褌!',
    NUMBER_FORMAT: {
      precision: 2,
      separator: ',',
      delimiter: ' ',
      strip_insignificant_zeros: false,
    },
  },
  en: {
    greeting: 'Hi!',
    NUMBER_FORMAT: {
      precision: 2,
      delimiter: '.',
      separator: ',',
      strip_insignificant_zeros: false,
    },
  }
};

Yeah I made a workaround as well. Slightly different approach. Added a new function as a workaround:

//overwrite to Currency function
let newConvert = (number, precision) => {
    return I18n.toCurrency(number, {
        unit:I18n.t("currency"), 
        format:I18n.t("cuFormat"), 
        separator:I18n.t("cuSeparator"), 
        delimiter:I18n.t("cuDelimiter"), 
        precision: precision
    })
}

I18n.cu = newConvert

and added the values currency, cuFormat, cuSeparator, cuDelimiter to my language files.
Maybe its helpful for someone.

@zoontek Why was this issue closed? It is still present as of version 2.0.9

@andrejcesen Because it's an issue with i18n-js, this lib is just a connector to get the language of the device, and i18n-js is just a dependency of it.

Was this page helpful?
0 / 5 - 0 ratings