react - 10. 国际化 i18n

栏目: IOS · Android · 发布时间: 7年前

内容简介:参考:这里包含了具体的例子 , 见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() })}        
    )
  }
}

以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,也希望大家多多支持 码农网

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

Implementing Responsive Design

Implementing Responsive Design

Tim Kadlec / New Riders / 2012-7-31 / GBP 27.99

New devices and platforms emerge daily. Browsers iterate at a remarkable pace. Faced with this volatile landscape we can either struggle for control or we can embrace the inherent flexibility of the w......一起来看看 《Implementing Responsive Design》 这本书的介绍吧!

JS 压缩/解压工具
JS 压缩/解压工具

在线压缩/解压 JS 代码

CSS 压缩/解压工具
CSS 压缩/解压工具

在线压缩/解压 CSS 代码

XML、JSON 在线转换
XML、JSON 在线转换

在线XML、JSON转换工具