react-native国际化

基于 react-native-localize、 i18n-js两个包做国际化,先添加:

yarn add react-native-localize

yarn add i18n-js

ios要 cd ios & pod install 一下,

添加中文、英文的国际化文件:

en.js:

export default {
    hello: "Hello"
};

zh.js:

export default {
    hello: "你好"
};

I18n.js(国际化配置):

import I18n from "i18n-js";
import * as RNLocalize from "react-native-localize";

import en from "./locales/en";
import zh from "./locales/zh";

// 获取本机语言
const locales = RNLocalize.getLocales();

if (Array.isArray(locales)) {
    // 取第一个做默认语言
    I18n.locale = locales[0].languageTag;
}

I18n.fallbacks = true;
I18n.translations = {
    en,
    zh
};

I18n.changeLanguage = language => {
    I18n.locale = language;
}


export default I18n;

App.js:

import React from 'react';
import I18n from "./src/utils/I18n";
import {SafeAreaView, Text, View,} from 'react-native';
import {Colors} from "react-native/Libraries/NewAppScreen";


const AppData = () => {
    const backgroundStyle = {
        backgroundColor: Colors.light,
    };
    return (
        <SafeAreaView style={backgroundStyle}>
            <View style={{
                height: '100%',
                width: '100%',
                display: 'flex',
                justifyContent: 'center',
                alignItems: 'center',
            }}>
                <Text>
                    {I18n.t("hello")}
                </Text>
            </View>
        </SafeAreaView>
    )
}

const App2 = () => {
    return (
        <AppData/>
    )
}

export default App2;




 参考:https://medium.com/@nicolas.kovacs/react-native-localize-and-i18n-js-117f09428017

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值