react i18n 实现国际化
来源:互联网 发布:淘宝怎么可以买到片 编辑:程序博客网 时间:2024/06/11 22:24
来个前端工作吧:helloworld3q3q@gmail.com qq:2260410070
为了让react 实现本地语言,就需要i18n
当然首先就要npm install
npm install react-intl --save
安装好intl,这个组件依赖react 版本为 0.14.0 以上 或者 15.0.0以上
如果是0.13.0 的 就要对react 升级,主要
0.14以后react 对组件进行了分离,分为 react 和react-dom 还有react-addons
正文开始
建立语言文件:data.json 汉字进行Unicode编码转换
{ "en": { "BackManage": "Backstage Management", "POSTS": "POSTS", "Posts": "Posts", "Post Categories":"Post Categories", "GALLERIES": "GALLERIES", "Galleries": "Galleries", "ENQUIRIES": "ENQUIRIES", "Enquiries": "Enquiries", "YS": "YS", "Ys": "Ys", "OTHERS": "OTHERS", "TEST": "TEST", "Users": "Users", "Test": "Test", "Tests": "Tests" }, "zh": { "BackManage": "\u7ba1\u7406\u540e\u53f0", "POSTS": "\u6240\u6709\u535a\u6587", "Posts": "\u535a\u6587", "Post Categories":"\u535a\u6587\u5206\u7c7b", "GALLERIES": "\u6240\u6709\u753b\u5eca", "Galleries": "\u753b\u5eca", "ENQUIRIES": "\u67e5\u8be2\u6240\u6709", "Enquiries": "\u67e5\u8be2", "YS": "\u7ba1\u7406", "Ys": "\u7ba1\u7406", "OTHERS": "\u5176\u4ed6", "Users": "\u7528\u6237\u7ba1\u7406", "TEST": "\u6d4b\u8bd5", "Test": "\u6d4b\u8bd5", "Tests": "\u6d4b\u8bd5" }}
创建 Translate.js 组件
import { IntlProvider, addLocaleData } from 'react-intl';这个需要 intlprovider 用来传递 给子类 语言信息
import React, { Component } from 'react';import ReactDOM from 'react-dom';import { IntlProvider, addLocaleData } from 'react-intl';import localeData from '../../translations/data.json';import en from 'react-intl/locale-data/en';import zh from 'react-intl/locale-data/zh';//需要本地化的语言addLocaleData([...en, ...zh]);//获取本地语言const language = (navigator.languages && navigator.languages[0]) || navigator.language || navigator.userLanguage;const languageWithoutRegionCode = language.toLowerCase().split(/[_-]+/)[0];//messages data.json 里对应的 语言文本const messages = localeData[languageWithoutRegionCode] || localeData[language] || localeData.zh;class Translate extends Component {constructor(props) { super(props); } render() { //this.props.Template 父级传来的 this.props.Template return ( <IntlProvider locale={ language } messages={ messages }> {this.props.Template} </IntlProvider> ); }}module.exports = Translate;
父级组件
import React, { Component } from 'react';import ReactDOM from 'react-dom';import HomeDetail from './home-detail';import Translate from './translate';class HomeView extends Component { constructor(props) { super(props); } render() { return ( <Translate Template={<HomeDetail/>}/> ); }}ReactDOM.render(<HomeView />, document.getElementById('home-view'));
需要实现 本地化的 view 组件
引入
import { FormattedMessage } from 'react-intl';react-intl 还有其他很多 功能 时间
<FormattedMessage id={×××} /> id 值就是你要的显示的文字 当然还可以有其他属性
description='say hello todescription'
defaultMessage='Hello, defaultMessage'
import React, { Component } from 'react';import { FormattedMessage } from 'react-intl';export default class HomeDetail extends Component { constructor(props) { super(props); } renderFlatNav() {return index.lists.map((list) => {var href = list.external ? list.path : '/index/' + list.path;return (<h3 key={list.path}><a href={href}><FormattedMessage id={list.label} /> </a></h3>);});}renderGroupedNav() {return (<div>{index.nav.sections.map((navSection) => {return (<div className="nav-section" key={navSection.key}><h4><FormattedMessage id={navSection.label} /> </h4><ul>{navSection.lists.map((list) => {var href = list.external ? list.path : '/index/' + list.path;return (<li key={list.path}><a href={href}><FormattedMessage id={list.label}/> </a></li>);})}</ul></div>);})}{(() => {if (!index.orphanedLists.length) return;return (<div className="nav-section"><h4><FormattedMessage id={'OTHERS'} /> </h4><ul>{index.orphanedLists.map((list) => {return (<li key={list.path}><a href={'/index/' + list.path}><FormattedMessage id={list.label}/> </a></li>);})}</ul></div>);})()}</div>);} render() { return ( <div><div className="page-header"><h1><FormattedMessage id={'BackManage'} /> </h1></div><div className="index-lists">{index.nav.flat ? this.renderFlatNav() : this.renderGroupedNav()}</div></div> ); }}
直接传递字符串时 需要通过defineMessages 来对字符进行转换
import { FormattedMessage, injectIntl, defineMessages } from 'react-intl';const messages = defineMessages({ placeholder: {id: 'inputfilter'},});var test = React.createClass({getInitialState () {return {formatMessage: this.props.intl['formatMessage'],};},render() {return (<FormInput placeholder={this.state.formatMessage(messages.placeholder)} />)}});module.exports = injectIntl(test);
0 0
- react i18n 实现国际化
- JavaSE实现国际化(i18n)
- struts-i18n国际化实现
- Vue + i18n实现国际化
- 利用MessageSource实现国际化[I18N]
- Spring MVC +i18n实现国际化
- 使用i18n实现页面国际化
- 国际化--i18n
- 国际化-I18n
- 国际化i18n
- 国际化(I18N)
- I18N国际化
- 国际化i18n
- 国际化(i18n)
- 国际化(i18n)
- 国际化(i18n)
- 国际化i18n
- jQuery.i18n.properties实现js国际化
- 第9周 项目2-对称矩阵的压缩存储的实现与应用(1)
- 单击按钮放大或缩小显示文字
- 将字符串中连续出现的重复字母进行压缩
- 《您的设计模式》(CBF4LIFE)之“适配器模式”【整理】
- 互斥锁和信号量的区别
- react i18n 实现国际化
- php中变量与常量的区别
- 补码反码转换
- (OK) Linux各版本源代码-查看-Linux Cross Reference
- js判断手机是否弹出软键盘
- Struts总结
- 关于设置背景块透明,上面的文字不透明
- SecureCRT中文乱码解决方法
- 2016年大三上-网络安全-密码学-DES算法