React apps come with features such as state, props, lifecycle hooks, hooks functions, and so on. For a worldwide audience, your app should be configured so well that people in every country can access all of your app features.
The term “internationalization” refers to providing the app with additional language support. Your internationalized app will not miss any customer from any corner of the world. To achieve this, libraries such as React Intl are used. This guide describes how to implement and use React Intl, which contains various functions to format data, such as date format, and it uses formatjs
as a supporting library.
The library react-intl
uses formatjs
, which contains various functions that support formatting data for internationalization purposes.
Let's install the library using the NPM command below.
1npm install react-intl
After installing the package, you can initialize it and use various functions to format a message based on the required feature.
Here is an example using the react-intl
function formatMessage
.
1function () {
2 const message = defineMessages({
3 messageText: {
4 id: 'app.message',
5 defaultMessage: 'Hello, {message} !!!',
6 description: 'This is test description',
7 },
8 })
9
10 return intl.formatMessage(message.messageText, { message: 'FOO' })
11}
The intl
is an initialization object followed by the function name formatMessage()
, which accepts the two different arguments.
The message will get internationalized after providing both arguments based on the desired configuration for the internationalization.
While using internationalization libraries such as react-intl
, issues may arise, and one of the most common issues is message formatting.
Specifically, you might get the error no date format
related to the supported format for the date values. If you do not format the date from the available date formats from formatjs
, you may get various format errors.
There are two options available to format the date, which is explained below.
The standard approach uses the function FormattedDate
with the date value without using the date format specification.
1import React from "react";
2import {
3 IntlProvider,
4 FormattedDate,
5} from "react-intl";
6
7export default class extends React.Component {
8 render() {
9 const message = {
10 myMessage: "Ceci est une chaîne"
11 };
12 return (
13 <div>
14 <IntlProvider
15 messages={message}
16 locale="fr"
17 defaultLocale="en"
18 >
19 <p>
20 <FormattedDate value={new Date(1457832991843)} />
21 </p>
22 </IntlProvider>
23 </div>
24 );
25 }
26}
In this example, the function is imported from the package react-intl
.
1import {
2 IntlProvider,
3 FormattedDate,
4} from "react-intl";
The initProvider
initializes the react-intl
instance and uses its feature, and one of the functions used is FormattedDate
.
Internationalization is initialized with the provider <InitProvider>
along with various props such as messages
, locale
, and defaultLocale
, which specifies the local language. But to format the date, an additional component called <FormattedDate>
is used, as demonstrated below.
1<FormattedDate value={new Date(1457832991843)} />
The “value” props are used with the component to provide the date value, which gets converted to the default format dd/mm/yyyy
.
The date can also be formatted using various configurations, such as the day, month, and year format.
Available formats for the day:
Available formats for the month:
Available formats for the year:
The date can be formatted using all of the above formats with the respective categories based on the functional requirement. Below is a simple example of formatting a date with date formats.
1<FormattedDate
2 value={new Date(1457832991843)}
3 year="numeric"
4 month="long"
5 day="2-digit"
6/>
The formats for date values can be numeric
, long
, and 2-digit
, so the resulting data can be returned as 13 March 2016
.
You can try both approaches to formatting dates, i.e., with or without the date format properties.
Because of its ability to help apps reach a wider audience, the internationalization feature is one of the most critical features of any app. It can make sure that desired messages and values are transformed based on the local language where the app is being accessed.
We have created this guide to resolve the formatting issue commonly faced by developers implementing internationalization. I hope it helps you with your project needs.
If you have any questions, feel free to contact me at CodeAlphabet.