Always return the language en-US
development version
"react": "16.6.0-alpha.8af6728",
"react-native": "0.57.3",
"react-native-localization": "^2.0.1"
the same for me I am using version 1.0.12 with react native 0.55 I also put emulator region United-Kindom it still return languageInterface = en_US
@stefalda
I have exactly the same problem
Same here, anyone figure out how to fix it?
react-native-localization dependencies basically uses navigator.language that is returning undefined and the package localized-strings return en-US in this case.
I fixed with a @ReactMethod on a AndroidNativeModule with the following code
@ReactMethod
public void getLanguage(Promise promise){
try{
String language = getReactApplicationContext().getResources().getConfiguration().locale.getLanguage();
promise.resolve(language);
}
catch (Exception e){
promise.reject(e);
}
}
and used on App.js
async componentDidMount() {
let language = await MyNativeModule.getLanguage();
Strings.setLanguage(language);
this.setState({});
}
2.0.1 has the same problems both on android and iOS. RN 0.57.3
Any updates here?
An update for anyone who find it useful:
For the same of time, I've successfully implemented a workaround for this. In my app, I have a new LocalizedStrings for each screen, separated into independent files in a texts folder, and am namespace exporting off texts/index.ts
Example:
// texts/accountScreenTexts.ts
interface IAccountScreenRowTexts {
about: string;
}
export const accountScreenRowTexts: LocalizedStringsType<
IAccountScreenRowTexts
> = new LocalizedStrings({
en: {
about: `ABOUT US`,
},
cn: {
about: `关于我们`,
},
});
I also have an AppLoader.tsx component responsible for various app loading functions.
In that file, I have something like...
import { NativeModules } from "react-native";
import * as texts from "../path/to/texts";
if (NativeModules.ReactLocalization.language.includes("zh")) {
Object.values(texts).forEach(localizedText => {
if (localizedText.setLanguage) {
localizedText.setLanguage("cn");
}
});
}
Currently, my app supports only English and Chinese. This example can easily be expanded to lookup various languages as offered by NativeModules.ReactLocalization.language, then map them to the codes you may already have in your library of LocalizedStrings.
Could also have this logic based on a piece of state in AppLoader to potentially implement dynamic language changing.
Works like a charm for all screens I currently have translated.
Cheers!
Sorry I've fixed the problem in version 2.0.2
Thanks @stefalda !! Much appreciated.