ReactNativeLocalization icon indicating copy to clipboard operation
ReactNativeLocalization copied to clipboard

Always return the language en-US

Open Vinu-MysteriousTechVision opened this issue 8 years ago • 9 comments

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

EsmeraldiBejolli avatar Oct 19 '18 13:10 EsmeraldiBejolli

I have exactly the same problem

pavlo-kravchenko avatar Oct 19 '18 14:10 pavlo-kravchenko

Same here, anyone figure out how to fix it?

hubertwang avatar Oct 24 '18 01:10 hubertwang

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({});
  }

everton-e26 avatar Oct 24 '18 22:10 everton-e26

2.0.1 has the same problems both on android and iOS. RN 0.57.3

goodhyun avatar Oct 25 '18 09:10 goodhyun

Any updates here?

wkoutre avatar Oct 25 '18 20:10 wkoutre

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!

wkoutre avatar Oct 25 '18 21:10 wkoutre

Sorry I've fixed the problem in version 2.0.2

stefalda avatar Oct 26 '18 22:10 stefalda

Thanks @stefalda !! Much appreciated.

wkoutre avatar Oct 28 '18 17:10 wkoutre