fnando / i18n-js

It's a small library to provide the I18n translations on the Javascript. It comes with Rails support.
MIT License
3.77k stars 520 forks source link

Translations don't work for some texts (React Native - Expo) #600

Closed Panicum closed 3 years ago

Panicum commented 3 years ago

I am making React Native app using Expo and I need to make this app in few languages (for this moment in Polish and English). So I choose i18n-js to handle texts translations and I followed instructions from this tutorial, and it's works... But not for all strings.

Most of the texts are translated correctly by i18n-js, but I have two cases that are not working:

1) Translations in Navigation Drawer Menu

1

Fragment of Drawer.js code:

const RootDrawerNavigator = createDrawerNavigator({
Home: {
  screen: HomeStack,
  navigationOptions: {
    title: translate('menu.home'),
    drawerIcon: ({ tintColor }) => <AntDesign name="home" size={24} color={colors.black}/>,
  }
},
Map: {
  screen: MainStack,
  navigationOptions: {
    title: translate('menu.map'),
    drawerIcon: ({ tintColor }) => <FontAwesome name="map-o" size={20} color={colors.black}/>,
  }
},
FindSpot: {
  screen: FindSpotStack,
  navigationOptions: {
    title: translate('menu.find_spot'),
    drawerIcon: ({ tintColor }) => <MaterialCommunityIcons name="magnify" size={24} color={colors.black}/>,
  },
},

2) Translations in DropDown:

2

Sample dropdown code:

    const waterTypesArrayDD = [
  { value: translate('common.typ_flat')},
  { value: translate('common.typ_wave')},]

  <Dropdown
       containerStyle={formStyles.dropdown}
       baseColor={colors.primaryColor}
       ref={(ref) =>
          this.state.WATER_TYPE_REF = ref
       }
       label={translate('addSpotAct.water_type')}
       data={waterTypesArrayDD}
       pickerStyle={{ backgroundColor: colors.secondaryColor, width: 0.75 * width }}
       selectedItemColor={colors.primaryColor}/>

Translations handling file code:

import { memoize } from 'lodash'
import i18n from 'i18n-js'
import * as Localization from 'expo-localization'

export const availableTranslations = ['en','pl']

const translationGetters = {
  pl: () => require("./pl.json"),
  en: () => require("./en.json"),
};

export const setI18nConfig = () => {
  // fallback if no available language fits
  const fallback = { languageTag: "en"};

  let languageTag = Localization.locale.substring(0,2)

  if(!availableTranslations.includes(languageTag)){
    languageTag = fallback.languageTag
  }
  translate.cache.clear()
  console.log('User language', languageTag)
  i18n.translations = { [languageTag]: translationGetters[languageTag]() };
  i18n.fallback = true
  i18n.locale = languageTag;
  return languageTag
};

export const translate = memoize(
  (key, config) => i18n.t(key, config),
  (key, config) => (config ? key + JSON.stringify(config) : key)
);

I have all the necessary translations in both: en.json and pl.json

I would be very grateful for any ideas on how to solve this problem!

PikachuEXE commented 3 years ago

I don't have experience in React / React Native I suggest in one of the places with translation missing (e.g. en.menu.home) Print out the value fetched manually (i.e. i18n.translations["en"]["menu"]["home"]) If translation entry is absent, not library issue. If translation entry is present, we can investigate further.

alicanso commented 1 year ago

dont use "en". "common.menu" is enough