React-native-i18n: I18n.currentLocale() on iOS always returns "en"

Created on 7 Mar 2017  Â·  29Comments  Â·  Source: AlexanderZaytsev/react-native-i18n

Hey, i am just testing this module, but i always get false return values on ios.

this call: I18n.currentLocale();

always returns "en" even though the app is running on a german iphone, with german language and used in germany.

What rules are connected to currentLocale() ?

I already added german xliff in xcode , but no changes…

Can somebody help me on that?

Android seems to be working properly

Many Thanks!

All 29 comments

@patricktresp Can you give more informations? Which react-native version did you use? Which iOS version? Thanks!

Hi! I'm having the same issue, but on android. I'm using a >0.40 version on RN on a android 6.0 Marshmallow. My brazillian phone on brazillizan portuguese language on brazil returns currentLocalle() "en".

Thanks in advance!

@xilonxiao @patricktresp What ReactNativeI18n.locale returns on your device?

I'm currently working on a better language detection for iOS and Android (I'm a bit stuck with the Windows version).

Please also try a manual instal instead of react-native link

Hey,

i am using :
react-native-cli: 2.0.1
react-native: 0.40.0

i am using ios 8, 9, 10 all same "en" - locale by default

honestly, the only lib that is working correctly is

react-native-device-info

( DeviceInfo.getDeviceLocale() )

@patricktresp react-native-device-info use the exact same code as this library to get the device locale.

https://github.com/rebeccahughes/react-native-device-info/blob/master/RNDeviceInfo/RNDeviceInfo.m#L140

https://github.com/AlexanderZaytsev/react-native-i18n/blob/master/RNI18n/RNI18n.m#L19

So it might be an install problem. Did you do a react-native link or a manual install? Try removing libRN18n.a from Linked Frameworks and Libraries in Xcode, and add it again.

EDIT: See https://github.com/AlexanderZaytsev/react-native-i18n/issues/43

@zoontek Hi, sorry for the late reply. There it goes: the ReactNativeI18n.locale returns empty, not undefined, empty. Both defaultLocale and currentLocale returns "en". As I said before, I'm running it on a brazillian portuguese mobile.

Thanks!

@xilonxiao If it's return empty, it's because the Native module is undefined (and not linked well)
Check here: https://github.com/AlexanderZaytsev/react-native-i18n/blob/master/index.js#L7

If you import directly RNI18n from NativeModules you will get undefined. Can you try it?
Maybe I should add a warning for this case.

@zoontek Thanks for the reply! I guess i didn't understand what you meant. Doing "import I18n from 'react-native-i18n'" is not enough? Should i add all of this piece of code:

'use strict';

const { NativeModules } = require('react-native');
const { RNI18n } = NativeModules;
let I18n = require('./vendor/i18n');
I18n.locale = (RNI18n) ? RNI18n.locale.replace(/_/, '-') : '';
module.exports = I18n;

to make it work? I 've tried some things here, but notthing seems to work.

Thanks!

@xilonxiao You can try this:

const { NativeModules } = require('react-native');
const { RNI18n } = NativeModules;

console.log(RNI18n)

If it's undefined, the module is not correctly linked.
In this case, use manual install: https://github.com/AlexanderZaytsev/react-native-i18n#ios
After that, import I18n from 'react-native-i18n' and I18n.currentLocale() should work normally.

@zoontek Thanks, it worked! Now both locale and currentLocale returns the correct "pt-BR". But now i have another question: Even now with the correct locale, the method toCurrency() is returning the "en" format "$1,299.00" instead of the correct "pt-BR" format "R$1.299,00". Am I missing something?

Thanks for the quick reply!

@xilonxiao It's a problem with i18n-js, the underlaying library. I will update it this week :)

@zoontek Dumb question: closed means solved? If yes, how do i update with the solution?

Thanks

@xilonxiao I commited the latest i18n-js version on master. You can use it directly if you don't want to wait for the npm release :)
I close the issue because this one was resolved (bad linking). If your issue is still valid with the latest i18n-js, it would be better to create a new issue.

@zoontek Ok, thanks! I 'll wait the npm release.

Hello, is it supposed to be fixed without using directly i18n-js? I still have this issue (Android only)

@OzoTek You can use the master branch ("AlexanderZaytsev/react-native-i18n" as version number)

@zoontek Doesn't seem to do the trick unfortunately!

@OzoTek Did you do a manual link and not using react-native link like it is said? Weird.

No, just react-native link

@OzoTek It was explain here: https://github.com/AlexanderZaytsev/react-native-i18n/issues/69#issuecomment-285970489
Another issue is open to find the problem with react-native link: https://github.com/AlexanderZaytsev/react-native-i18n/issues/43

It works indeed, my bad! Thanks.

@zoontek What do i need to do to use the master branch? I didn't understand what you meant by " use the master branch ("AlexanderZaytsev/react-native-i18n" as version number)"

Thanks!

Guys, how did you solve it? It's still returning "en" always for me. How do i know if this works or not? Also, i'm in Argentina, and always returning en. I just don't know what else to try.

I got the I18n external from the index.ios.js and then i import it.

Everything seems to be accordingly as what you guys suggested in terms of library locations, everything is in place.

This is how i have it now:

const { NativeModules } = require('react-native');
const { RNI18n } = NativeModules; // Tried adding these two ^ because i read it here, but doesn't seem to do anything extra.

import I18n from 'react-native-i18n';

I18n.fallbacks = true;
console.log('////// ' + I18n.currentLocale());
console.log(I18n);
I18n.translations = {
    en: {
        text: 'englishhh'
    },
    es: {
        text: 'españoool'
    },
    pt: {
        text: 'brasiiiiiiil'
    },
    ru: {
        text: 'Mother rrrrrussia'
    }
}

module.exports = I18n;

Then i use it from my index.ios.js like this:

import i18n from './src/components/I18n';

Then:

{ i18n.t('text') }

Text is being translated to english correctly. But im always getting en as locale, tried using the Location Simulator in XCode and no matter what i choose, always keeps it in english. How can I test this?

If I remember well you have to import manually the package in the MainApplication.java.

@OzoTek even though i'm working now for iOS only? It wasn't added into the getPackages() method, so i added it manually but still, not working for me on iOS, always returning english.

    @Override
    protected List<ReactPackage> getPackages() {
      return Arrays.<ReactPackage>asList(
          new MainReactPackage(),
          new VectorIconsPackage(),
          new ReactNativeI18n()
      );
    }

No if you're just in iOS it doesn't affect this at all.
Try react-native link or manually link it in iOS using Xcode. I don't remember that I had any issue with iOS though, just the linking did the trick.

That's what it throws when i react-native link

rnpm-install info Android module react-native-i18n is already linked
rnpm-install info iOS module react-native-i18n is already linked
rnpm-install info Linking assets to ios project
rnpm-install info Linking assets to android project
rnpm-install info Assets have been successfully linked to your project

Nevermind! it's working! didn't know how to locale my simulation phone.

If anyone else doesn't know, read this:

https://developer.apple.com/library/content/documentation/MacOSX/Conceptual/BPInternational/TestingYourInternationalApp/TestingYourInternationalApp.html

Basically you gotta edit the scheme.

To update the locale just do it the same way as if it was a real phone, in the settings ;)

Was this page helpful?
0 / 5 - 0 ratings