Home  >  Article  >  Web Front-end  >  Detailed explanation of multi-language examples of React-intl implementation

Detailed explanation of multi-language examples of React-intl implementation

小云云
小云云Original
2017-12-28 10:39:022584browse

The language internationalization function has recently been added to the project. This article mainly introduces the sample code for multi-language implementation in React-intl. The editor thinks it is quite good, so I will share it with you now and give it as a reference. Let’s follow the editor to take a look, I hope it can help everyone.

Language internationalization, some people call it language localization, is actually adding multiple languages ​​to the Web App. Our project currently includes the Chinese version and the English version. Logically speaking, "word-for-word replacement" is not It's a big deal, but is there any money to be made with such a low approach?

At the beginning, I considered the traditional method of adding config files to the entire project. According to different languages ​​and regions, loading different config files can achieve the purpose of interface language switching. Of course, it is precisely because this idea is too naive that it is called the "initial" idea. The internationalization of language is not just about translating the UI text on the interface into another language, but also includes date & time display, numerical display (a comma every 3 digits in the English environment: 1,000), and quantifier display (an apple). is apple, two apples should be apples), and there is even a case where a variable is inserted in the middle of a string ("I ate {count} chicken drumsticks for lunch today")...

So this is not It is not just a simple character replacement problem, but also, we need to easily export a directory and put it in word or page, so that other colleagues can compare and translate it. This is very important! ! Do you want the product manager to make changes directly in the code? Or do you want to search and replace one by one? If you just do it without thinking it through, trust me, you'll pay for this.

As an aspiring coder, you definitely don’t want to add a line of 2934a685527f5cd6bcb20a3dc28499e1 reference to index.html, right? In addition, all the text in the UI belongs to the student who used the picture. Please stand up and get out. If you want to elegantly import something from somewhere in a React project, then replace the text in the interface with c82bfd454240bbe8068ab5889f7b4ae6 components, and finally achieve language internationalization through simple configuration, then we will use React -intl.

For React internationalization, I recommend using React-intl. This library provides React components and APIs to format dates, numbers, strings, etc. Now that we know this library, let’s start using it

Component usage

In order to be more integrated with React, we can use the component method

1 .Install


npm install react-intl --save

2.Add reference


import {IntlProvider, addLocaleData} from 'react-intl';

3.Add locale configuration file

zh-CN.js


const zh_CN = { 'intl.hello': "你好", 'intl.name': '我的名字是 {name}' } export default zh_CN;

en-US.js


const en_US = { 'intl.hello': "hello", 'intl.name': 'my name is {name}' } export default en_US;

4. Use 828bf50b1037c4b9793813cd10cf72a4

Basic usage



If the current language environment is Chinese, it will display Hello. If it is an English environment, it will display Hello.

Dynamic value transfer



{name}}}
/>

When we defined intl.name, we used {name} in the template. This means that we can dynamically pass values. We can pass a JSON object through the values ​​attribute in FormattedMessage, which will dynamically display our content. .

6. Usage of other components

Ract-intl provides us with a wealth of components that can help us process strings, times, and dates. You can check the API yourself, if any If you don’t understand something, I can leave a message.

API Usage

Sometimes we may need to dynamically do internationalization in the code, which requires a dynamic API. Let me briefly introduce how to use

1. Import injectIntl


import { injectIntl, FormattedMessage } from 'react-intl';

2. Inject

## in the component #

export default connect(mapStateToProps,mapActionCreators)(injectIntl(App))

I used Redux in the project. The injection should be as above. If you don’t use Redux, you only need to export defuault injectIntl(App)

3. Use the intl object

After the second step of injection, we will now get an intl object in the props of the component. The methods it provides basically correspond to the components we introduced above. At this time, if we want to display a string, we can use formatMessage Method:


const {intl} = this.props;
  
let tmp = intl.formatMessage({id: 'intl.name'},{name: 'joe'});

The first parameter of formatMessage can be passed in Id, and the second parameter can be passed in values. For more detailed information, please view API

Related recommendations:


PHP - Implementing multi-language automatic switching

Multi-language supported graphics and text in ASP.NET Core Detailed explanation

Detailed introduction to multi-language support in ASP.NET Core

The above is the detailed content of Detailed explanation of multi-language examples of React-intl implementation. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn