内容简介:参考:这里包含了具体的例子 , 见example 文件夹1. 安装:
参考: https://github.com/react-native-community/react-native-languages
这里包含了具体的例子 , 见example 文件夹
1. 安装:
yarn add react-native-languages --verbose ( 用npm可能会报错, windows下)
2.
react-native link react-native-languages
对于安卓来说, 上面的命令已经把该干的事情干了。 IOS 日后补上 。
3. 使用。
3.1 写好各种i18n 的文件,
// src/translations/en.json
{
"title": "react-native-languages demo",
"current": "The current language is \"{{language}}\""
}
3.2 引入这些文件 (src/i18n.js) 这个文件算是一个汇总文件。 它也是要被别的文件引用的
import RNLanguages from 'react-native-languages';
import i18n from 'i18n-js';
import en from './translations/en.json';
import fr from './translations/fr.json';
import de from './translations/de.json';
i18n.locale = RNLanguages.language;
i18n.fallbacks = true;
i18n.translations = { en, fr, de };
export default i18n;
3.3 在实际的代码中: ( src/App.js)
<Text style={styles.title}>{i18n.t('title')}</Text>
import React, { Component } from 'react';
import RNLanguages from 'react-native-languages';
import i18n from './i18n';
import Root from './Root';
export default class App extends Component {
componentWillMount() {
RNLanguages.addEventListener('change', this._onLanguagesChange);
}
componentWillUnmount() {
RNLanguages.removeEventListener('change', this._onLanguagesChange);
}
_onLanguagesChange = ({ language }) => {
i18n.locale = language;
};
render() {
return (
{i18n.t('current', { language: i18n.currentLocale() })}
)
}
}
以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,也希望大家多多支持 码农网
本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们。
Visual Thinking
Colin Ware / Morgan Kaufmann / 2008-4-18 / USD 49.95
Increasingly, designers need to present information in ways that aid their audiences thinking process. Fortunately, results from the relatively new science of human visual perception provide valuable ......一起来看看 《Visual Thinking》 这本书的介绍吧!