[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: ...
随机推荐
- 深入理解Core Data
留给我这忘事精看 Core Data 是什么? 大概八年前,2005年的四月份,Apple 公布了 OS X 10.4,正是在这个版本号中 Core Data 框架公布了.那个时候 YouTube 也 ...
- Android实现本地图片选择及预览缩放效果仿春雨医生
在做项目时常常会遇到选择本地图片的需求.曾经都是懒得写直接调用系统方法来选择图片.可是这样并不能实现多选效果.近期又遇到了,所以还是写一个demo好了.以后也方便使用.还是首先来看看效果 显示的图片使 ...
- Linux系统下到哪儿寻找硬件错误
Linux系统下到哪儿寻找硬件错误 当linux系统出现故障的时候,作为管理员首先要定位错误,现在linux有许多工具都能帮助用户寻找错误,要学会利用他们确定问题.这些工具包括dmesg. ...
- janos.io
http://janos.io/ https://developer.mozilla.org/en-US/Firefox_OS/Developing_Firefox_OS/Porting
- springboot扫描通用的依赖模块
将实际使用类的启动类改为如下形式启动: public class OrderApplication { public static void main(String[] args) { Object[ ...
- Linux 设置文件默认打开方式
比如说我安装了一个绿色版的sublime(.tar解压出来的不是.deb) 但是现在我右键不能打开,不能添加为默认打开方式...这个时候就比较尴尬了... 我总不能每次都cd到安装目录下然后termi ...
- Ubuntu 下安装 Python 虚拟环境
写在前面: 安装指南是在 Ubuntu 下面操作的.不同的 Linux 版本,安装指令不同.所以,该指南的某些指令对于像 CentOS 等非 Ubuntu 系统不适用. 为什么需要使用虚拟环境? 虚拟 ...
- matplotlib散点图笔记
定义: 由一组不连续的点完成的图形 散点图: 包含正相关性,负相关性和不相关性. 散点图生成函数: plt.scatter(x,y) 演示代码如下: import numpy as np import ...
- 【2017 Multi-University Training Contest - Team 5】Rikka with Competition
[Link]: [Description] [Solution] 把所有人的能力从大到小排; 能力最大的肯定可能拿冠军; 然后一个一个地往后扫描; 一旦出现a[i-1]-a[i]>k; 则说明从 ...
- ArcSDE:C#创建SDE要素数据集
转自原文 ArcSDE:C#创建SDE要素数据集 /// <summary> /// 判断指定数据集是否存在 /// </summary> /// <param name ...