react-native-i18n icon indicating copy to clipboard operation
react-native-i18n copied to clipboard

Totally not working on Android?

Open isaaclem opened this issue 6 years ago • 7 comments

First of all many thanks for this awesome package. It's running perfectly fine in iOS and I truly happy about it.

However when I run the exact same project in Android, it doesn't have any effect at all.

Below is my I18n.js file:

import I18n from 'react-native-i18n';
import { NativeModules, Platform } from 'react-native';
// Enable fallbacks if you want `en-US` and `en-GB` to fallback to `en`
I18n.fallbacks = true;

if (Platform.OS === 'android') {
  I18n.locale = NativeModules.I18nManager.localeIdentifier;
} else {
  I18n.locale = NativeModules.SettingsManager.settings.AppleLocale;
}
console.log('Current language:', I18n.locale);
I18n.defaultLocale = 'en';
// Add different defaultLocale here (default is 'en'). It will be used as a fallback when device locale isn't found in translations
// I18n.defaultLocale = 'nl'

// Add translations here
I18n.translations = {
  en: require('./en.json'),
  //zh-Hans: require('./cn.json'),
};

const languageCode = I18n.locale.substr(0, 2);

switch (languageCode) {
  case 'zh':
    I18n.translations.zh = require('./cn.json');
    break;
  default:
    break;
}

"react-native-i18n": "^2.0.15", "react": "^16.0.0-alpha.12", "react-native": "^0.56.0"

And I've gone thru both react-native link and manual steps as listed in README.md but to no avail. Any heads up is very much appreciated.

isaaclem avatar Aug 04 '18 03:08 isaaclem

what are you trying to do, its working perfectly fine on my IOS and Android, infact on latest react-native0.56.0

what is this?

if (Platform.OS === 'android') {
  I18n.locale = NativeModules.I18nManager.localeIdentifier;
} else {
  I18n.locale = NativeModules.SettingsManager.settings.AppleLocale;
}

library automatically determine and show the content according to the defined local. you can define your local as i18n.local = "en" and thats all.

yeomann avatar Aug 04 '18 15:08 yeomann

I'm not sure what went wrong because it's working totally fine on the iOS side but the translation didnt happen on Android. No error, no translation.

isaaclem avatar Aug 05 '18 07:08 isaaclem

you maybe have worng linking, also whatever you were trying to do this way NativeModules.I18nManager.localeIdentifier is not advised from the library. so you should probably create a test project and do manual linking. for me, I did just a normal linking and all works just fine. no error, no problem, nothing!

yeomann avatar Aug 05 '18 12:08 yeomann

I suspected that too because when I run npm link, it seems to me that somehow it's not working and so I've gone thru the manual way to link them for android, in which I've triple check all lines of codes comparing with documentation side by side, steps by steps.

Using NativeModules.I18nManager.localeIdentifier is a way for Android to get the system language of user's devices. Or you have a better suggestion on how to retrieve user preferred system language?

isaaclem avatar Aug 05 '18 12:08 isaaclem

I know that you are using that line for reading system language, but you don't need just check around library there's a way to read better, I think getLanguages() does it. but if you don't really configure anything, The library shows correct language string according to device language.

EDIT: just do this const deviceLocale = I18n.locale.substring(0, 2); it will give you device local. also please consider closing the issue. since its resolved!

yeomann avatar Aug 05 '18 12:08 yeomann

^ that solution doesn't work for chinese @yeomann . If you only support one type, specify it e.g traditional

const chineseLocales = [
  // simplified chinese android suffix
  '#Hans',
  // fallback mainland chinese android
  'zh-rCN',
  // simplified chinese ios
  'zh-Hans',
];

const locale = isAndroid
  ? NativeModules.I18nManager.localeIdentifier
  : NativeModules.SettingsManager.settings.AppleLocale;

const lngStr = () => {
  const lngId = locale.substring(0, 2);
  if (lngId === languages.Chinese) {
    return chineseLocales.some((lng: string) => locale.includes(lng))
      ? languages.Chinese
      : languages.Default;
  }

evanjmg avatar Jul 28 '21 16:07 evanjmg