[React Intl] Install and Configure the Entry Point of react-intl
We’ll install react-intl, then add it to the mounting point of our React app.
Then, we’ll use react-intl helpers to load locales in our app, including English, Spanish and French and choose which locale data to load based on the user's browser language.
We’ll also set up and include messages files for each language, which hold all of the translated strings for our app.
Install:
npm install --save react-intl
index.js:
import React from 'react';
import ReactDOM from 'react-dom'; import { addLocaleData, IntlProvider } from 'react-intl';
import en from 'react-intl/locale-data/en';
import fr from 'react-intl/locale-data/fr';
import es from 'react-intl/locale-data/es'; import messages from './messages'; import App from './App';
import './index.css'; addLocaleData([...en, ...fr, ...es]); let locale = (navigator.languages && navigator.languages[0])
|| navigator.language
|| navigator.userLanguage
|| 'en-US'; ReactDOM.render(
<IntlProvider locale={locale} messages={messages[locale]}>
<App />
</IntlProvider>,
document.getElementById('root')
);
For messages.js, it contains all the translations:
export default {
'en-US': {
detail: {
toggle: 'Toggle',
purchase: 'Purchase this book from:',
reviewsHeading: 'Reviews'
}
},
'es-ES': {
detail: {
toggle: 'Palanca',
purchase: 'Compre este libro de:',
reviewsHeading: 'Comentarios'
}
},
'fr-FR': {
detail: {
toggle:'Basculer',
purchase: 'Achetez ce livre à partir de:',
reviewsHeading: 'Avis'
}
}
}
It is recommended to use flatten structures. So we can use fatten utils:
export function flattenMessages(nestedMessages, prefix = '') {
return Object.keys(nestedMessages).reduce((messages, key) => {
let value = nestedMessages[key];
let prefixedKey = prefix ? `${prefix}.${key}` : key;
if (typeof value === 'string') {
messages[prefixedKey] = value;
} else {
Object.assign(messages, flattenMessages(value, prefixedKey));
}
return messages;
}, {});
}
Modify provider to use flattenMessages method:
ReactDOM.render(
<IntlProvider locale={locale} messages={flattenMessages(messages[locale])}>
<App />
</IntlProvider>,
document.getElementById('root')
);
The way to use it:
import { FormattedMessage } from 'react-intl';
<FormattedMessage id="detail.toggle"/>
[React Intl] Install and Configure the Entry Point of react-intl的更多相关文章
- Install and Configure OSSEC on Debian 7&8
Install and Configure OSSEC on Debian 7&8 Contributed by Sunday Ogwu-Chinuwa Updated Friday, Feb ...
- Install and Configure SharePoint 2013 Workflow
这篇文章主要briefly introduce the Install and configure SharePoint 2013 Workflow. Microsoft 推出了新的Workflow ...
- You must use the Role Management Tool to install or configure Microsoft .NET Framework 3.5 SP1
今天在Windows Server 2008 下安装SQL SERVER 2008时,碰到如下错误: You must use the Role Management Tool to install ...
- Use the PDFs below or the HTML contents to the left to install and configure P6 EPPM and its additional components.
Welcome to Your Documentation Use the PDFs below or the HTML contents to the left to install and c ...
- How to Install and Configure Nginx from Source on centos--转
1.CentOS - Installing Nginx from source http://articles.slicehost.com/2009/2/2/centos-installing-ngi ...
- Install and Configure Apache Kafka on Ubuntu 16.04
https://devops.profitbricks.com/tutorials/install-and-configure-apache-kafka-on-ubuntu-1604-1/ by hi ...
- Step 4: Install and Configure Databases
https://www.cloudera.com/documentation/enterprise/latest/topics/cm_ig_installing_configuring_dbs.htm ...
- ubuntu 16.04源码编译和配置caffe详细教程 | Install and Configure Caffe on ubuntu 16.04
本文首发于个人博客https://kezunlin.me/post/b90033a9/,欢迎阅读! Install and Configure Caffe on ubuntu 16.04 Series ...
- windows 10安装和配置caffe教程 | Install and Configure Caffe on windows 10
本文首发于个人博客https://kezunlin.me/post/1739694c/,欢迎阅读! Install and Configure Caffe on windows 10 Part 1: ...
随机推荐
- UNIX多线程编程
一个程序至少有一个进程.一个进程至少有一个线程.进程拥有自己独立的存储空间,而线程能够看作是轻量级的进程,共享进程内的全部资源.能够把进程看作一个工厂.线程看作工厂内的各个车间,每一个车间共享整个工厂 ...
- 将Firefox设置为使用远程DNS
将Firefox设置为使用远程DNS 原文 https://www.my-proxy.com/blog/firefox-remote-dns 测试当前在用DNS https: ...
- Python 面向对象 —— 多重继承
多重继承(一个子类同时继承多个父类),容易造成混乱,即如果两个父类又相同的方法名和变量名时,无法确定继承哪一个. 正因如此,Java 等语言中并不支持多重继承(Java 是单继承多接口).Python ...
- RTSP、HTTP、HTTPS、SDP四种协议详解
我们将主要讲解RTSP,HTTP,HTTPS, SDP四种协议. 一:RTSP协议简介 实时流协议RTSP是一个应用层协议,用于控制具有实时特性的数据(例如多媒体流)的传送. RTSP协议一般与RT ...
- 13.Axis创建webservice客户端和服务端
转自:https://blog.csdn.net/chenghui0317/article/details/9318317 一.Axis的介绍 Web Service是现在最适合实现SOA的技术,而A ...
- 【基础篇】EditText的一些属性设置
设置EditText的背景颜色 private test_editText=null; test_editText= (EditText) findViewById(R.id.EditTextInp ...
- kafka删除主题
hdp集群默认不能删除kafka主题,如果要删除,需要在ambari上进行配置,将enable delete设置为true.
- NewtonSoft对象转json时,把 NULL 转 "" , 过滤 NULL, DateTime 时间类型去除 T
IsoDateTimeConverter timeConverter = new IsoDateTimeConverter(); timeConverter.DateTimeFormat = &quo ...
- LuoguP3254 圆桌问题(最大流)
题目描述 假设有来自m 个不同单位的代表参加一次国际会议.每个单位的代表数分别为ri (i =1,2,……,m). 会议餐厅共有n 张餐桌,每张餐桌可容纳ci (i =1,2,……,n)个代表就餐. ...
- 洛谷——P1307 数字反转
https://www.luogu.org/problem/show?pid=1307#sub 题目描述 给定一个整数,请将该数各个位上数字反转得到一个新数.新数也应满足整数的常见形式,即除非给定的原 ...