[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"/>
时间: 2024-08-28 14:35:26

[React Intl] Install and Configure the Entry Point of react-intl的相关文章

Install and Configure OpenStack Database Service (Trove)

Based on OpenStack Icehouse release we will install Database service on controller node 1. 这个还不完善,以后更新 Install and Configure OpenStack Database Service (Trove),布布扣,bubuko.com

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 or configure Microsoft .NET Framework 3.5 SP1. 既然碰到了顺便还是记录一下,虽然感觉没啥技术含量也没有难度,有时候有必要养成一个好习惯.记录你碰到的问题.解决问题的方案,你思考的点点滴滴! 打开 Server Manager,在"Features&quo

NotePad++ - 安装和配置C/C++开发插件 | NotePad++ - Install and Configure plugins for develop C/C++

一.NotePad++插件 / NotePad++ Plugins 1.Function List ( Unicode ) 当前最新版本:Version 2.1发布日期:2010-02-18安装程序下载地址:http://sourceforge.net/projects/npp-plugins/files/Function%20List/FunctionList%20Plugin%20v2.1/FunctionList_2_1_UNI_dll.zip/download 2.NppExec ( U

Install and Configure Apache Kafka on Ubuntu 16.04

https://devops.profitbricks.com/tutorials/install-and-configure-apache-kafka-on-ubuntu-1604-1/ by hitjethvaon Oct 03, 2016 Intermediate Table of Contents Introduction Features Requirements Getting Started Installing Java Install ZooKeeper Install and

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 Part 1: Install and Configure Caffe on windows 10 Part 2: Install and Configure Caffe on ubuntu 16.04 Guide requirements: NVIDIA driver 396.54 CUDA 8

react全家桶从0搭建一个完整的react项目(react-router4、redux、redux-saga)

react全家桶从0到1(最新) 本文从零开始,逐步讲解如何用react全家桶搭建一个完整的react项目.文中针对react.webpack.babel.react-route.redux.redux-saga的核心配置会加以讲解,通过这个项目,可以系统的了解react技术栈的主要知识,避免搭建一次后面就忘记的情况. 从webpack开始 思考一下webpack到底做了什么事情?其实简单来说,就是从入口文件开始,不断寻找依赖,同时为了解析各种不同的文件加载相应的loader,最后生成我们希望的

LDAP 在ubuntu14.04下的安装配置install and configure

https://help.ubuntu.com/lts/serverguide/openldap-server.html if error occurs in reinstall, try this: "1)sudo apt-get purge slapd 2)sudo apt-get install slapd ldap-utils "   you can configure ldap after install: When the installation is complete,

AX 2012 R3 Install And Configure Log

Base System Environment: (1).Windows Server 2012 R2; (2).SQL Server 2014; (3).SSDT 2008(Business Intelligence Development Studio in SQL Server 2008 for VS 2010); (4).Office 2013; (5).Visual Studio 2010; (6).Dynamics AX 2012 R3. 1.Install Windows Serv

How to install and configure Azure PowerShell

https://azure.microsoft.com/en-us/documentation/articles/powershell-install-configure/ In this article: How to: Install Azure PowerShell How to: Connect to your subscription How to use the cmdlets: An example Getting Help Additional Resources 11 Comm