反应导航如何更改底部选项卡的语言

ang*_*iwi 5 react-native expo

MainTabNavigator.js

import { createStackNavigator, createBottomTabNavigator } from 'react-navigation';
const ExploreStack = createStackNavigator({
    Explore: ExploreScreen,
});
ExploreStack.navigationOptions = {
    tabBarLabel: 'explore',
    tabBarIcon: ({ focused }) => (
        <MyTabBarIcon style={{width:22, height:21}} name={'explore'} focused ={focused} />
    ),
};
const CarStack = createStackNavigator({
    CarStack: CarScreen,
});

CarStack.navigationOptions = {
    tabBarLabel: 'car',
    tabBarIcon: ({ focused }) => (
        <MyTabBarIcon style={{width:39, height:19}} name={'car'} focused ={focused} />
    ),
};

export default createBottomTabNavigator({
    CarStack,
    ExploreStack,
});
Run Code Online (Sandbox Code Playgroud)

appNavigtor.js

import MainTabNavigator from './MainTabNavigator';
import SignIn from '../screens/SignIn'

export default createAppContainer(createSwitchNavigator({
  // You could add another route here for authentication.
  // Read more at https://reactnavigation.org/docs/en/auth-flow.html
  Main: MainTabNavigator,
  SignIn: SignIn
}));
Run Code Online (Sandbox Code Playgroud)

应用程序.js

import AppNavigator from './navigation/AppNavigator';
export default class App extends React.Component {
    render() {
            return (
                <View style={styles.container}>
                    <AppNavigator />
                </View>
            );
        }
}
Run Code Online (Sandbox Code Playgroud)

我正在使用“i18n-js”中的 expo i18n 和 expo 中的 Localization 来翻译组件,它们工作得很好。但是我得到了由世博会生成的底部标签。我不知道如何翻译我在上面的库中尝试过的那些标签,似乎不起作用。

标签看起来像这样

在此处输入图片说明

Mah*_*yar 0

有几种方法可以开发它:

我使用了 React-Native-i18n 包。第一的:

npm install react-native-i18n --save
Run Code Online (Sandbox Code Playgroud)

然后您需要创建本地化文件。如:en.json或fi.json

您应该将翻译添加到这些文件中:

en.json:
{
 "lang": "en",
   "button": {
     "ok": "OK",
     "cancel": "Cancel",
     "close": "Close"
   }
}
Run Code Online (Sandbox Code Playgroud)

{
"lang": "fi",
  "button": {
    "ok": "OK",
    "cancel": "Peruuta",
    "close": "Sulje"
   }
}
Run Code Online (Sandbox Code Playgroud)

您需要创建 i18n.js 文件。然后在 MainTabNavigator.js 中:

import I18n from '../i18n'

ExploreStack.navigationOptions = () => ({
   tabBarLabel: I18n.t('button.ok'),
   tabBarIcon: ({ focused }) => (
      <MyTabBarIcon style={{width:22, height:21}} name={'explore'} focused ={focused} 
      />
  ),
});
Run Code Online (Sandbox Code Playgroud)

我希望它对你有帮助。如果您需要更多解释,请告诉我。

  • 这不起作用,因为当您更改语言时 tabBarLabel 不会更新。您需要发送一些值,该值将在底部TabNavigation 中的语言更改时触发。https://medium.com/@leonlek/create-dynamic-language-changing-on-tabbarlabel-of-materialbottomtabnavigator-with-mobx-and-efc15bfbc1b3 (3认同)