Project Name | Stars | Downloads | Repos Using This | Packages Using This | Most Recent Commit | Total Releases | Latest Release | Open Issues | License | Language |
---|---|---|---|---|---|---|---|---|---|---|
Formatjs | 13,868 | 7,980 | 3,590 | 2 days ago | 312 | June 12, 2023 | 33 | TypeScript | ||
The monorepo home to all of the FormatJS related libraries, most notably react-intl. | ||||||||||
React I18next | 8,443 | 1,307 | 3,113 | a month ago | 285 | July 31, 2023 | 12 | mit | JavaScript | |
Internationalization for react done right. Using the i18next i18n ecosystem. | ||||||||||
Js Lingui | 3,902 | 47 | 129 | 8 hours ago | 96 | August 08, 2023 | 25 | mit | TypeScript | |
🌍 📖 A readable, automated, and optimized (3 kb) internationalization for JavaScript | ||||||||||
React Most Wanted | 2,415 | 7 | a month ago | 98 | October 18, 2022 | 18 | mit | JavaScript | ||
React starter kit with "Most Wanted" application features | ||||||||||
Typesafe I18n | 1,855 | 13 | 2 days ago | 265 | August 25, 2023 | 16 | mit | TypeScript | ||
A fully type-safe and lightweight internationalization library for all your TypeScript and JavaScript projects. | ||||||||||
Reactjs Interview Questions | 1,552 | 3 years ago | JavaScript | |||||||
List of top 304 ReactJS Interview Questions & Answers | ||||||||||
React Intl Universal | 1,294 | 56 | 89 | 7 days ago | 114 | July 19, 2023 | 51 | bsd-3-clause | JavaScript | |
Internationalize React apps. Not only for Component but also for Vanilla JS. | ||||||||||
Next Intl | 1,071 | 11 | a day ago | 170 | September 22, 2023 | 33 | mit | TypeScript | ||
Internationalization (i18n) for Next.js that gets out of your way. 🌐 | ||||||||||
Tolgee Platform | 899 | 5 hours ago | 106 | other | Kotlin | |||||
Developer & translator friendly web-based localization platform | ||||||||||
React Phone Number Input | 882 | 95 | 235 | a month ago | 345 | September 02, 2023 | 16 | mit | JavaScript | |
React component for international phone number input |
React-intl-hooks is a small and fast library that you can use to replace Format.js components. This allows for a consistent codebase if you're using the latest React.js features!.
Format.js with react-intl is an awesome library, but for the people like us who love React new features like hooks, it can still be bit daunting to use libraries that have components. So we decided to combine the great things about react-intl along with React hooks.
Yarn
yarn add react-intl-hooks
NPM
npm install react-intl-hooks
First we need to enable internationalization in our app. We need to import the IntlProvider
component, then use it to wrap our React app.
Your src/index.js
should look like this:
import React from 'react';
import ReactDOM from 'react-dom';
import './index.css';
import App from './App';
import { IntlProvider } from 'react-intl-hooks';
import locale_en from './translations/en.json';
import locale_es from './translations/es.json';
const data = {
es: locale_es,
en: locale_en,
};
const language = navigator.language.split(/[-_]/)[0];
ReactDOM.render(
<React.StrictMode>
<IntlProvider
locale={language}
messages={data[language]}
defaultLocale="en"
>
<App />
</IntlProvider>
</React.StrictMode>,
document.getElementById('root'),
);
We import our translation files and we get the locale from the user's browser. If the locale is not available we can default to en as our locale.
Create src/translations
folder in your project and create files for the locales you want to support in you React app.
In this example we're going to create translation files for English and Spanish.
Here's some content for our files:
src/translations/en.json
:{
"app.learn": "Learn React"
}
src/translations/es.json
:{
"app.learn": "Aprende React"
}
Translating your components is just as easy as importing the hook you need.
src/app.js
:
import React from 'react';
import logo from './logo.svg';
import './App.css';
import { useFormatMessage } from 'react-intl-hooks';
function App() {
const t = useFormatMessage();
return (
<div className="App">
<header className="App-header">
<img src={logo} className="App-logo" alt="logo" />
<a
className="App-link"
href="https://reactjs.org"
target="_blank"
rel="noopener noreferrer"
>
{t({ id: 'app.learn', defaultMessage: 'Learn React' })}
</a>
</header>
</div>
);
}
export default App;
In the last example we simply translated the default app you get when you use create-react-app
to boostrap your project.
Your have a wide variety of hooks to choose depending on the things you want to translate.
import {
useFormatMessage,
useFormatDate,
useFormatList,
useFormatNumber,
useFormatPlural,
useFormatRelativeTime,
useFormatTime,
} from 'react-intl-hooks';
We'll have a detailed look of each one in the next section.
Here you'll get a detailed explanation of every hook react-intl-hooks
has to offer.
useFormatMessage
Hook used to translate text in your application.
Import the hook:
import { useFormatMessage } from 'react-intl-hooks';
Then use it like this:
const t = useFormatMessage();
The hook returns a translation function that can be used inside the JSX code of your components.
t(message: MessageDescriptor, values?: MessageFormatValues);
{
id: string,
description?: string,
defaultMessage?: string
}
Object used to pass variables or React elements to our translated text. In the following example you can see a use case for this.
import React from 'react';
import { useFormatMessage } from 'react-intl-hooks';
const MyComponent = () => {
const t = useFormatMessage();
return (
<div>
<h1>
{t(
{
id: 'mycomponent.title',
description: 'Title for my component!',
defaultMessage: 'Hello {user}!',
},
{ user: 'Mateo' },
)}
</h1>
</div>
);
};
export default MyComponent;
<div>
<h1>Hello Mateo!</h1>
</div>
useFormatDate
This hook is used to translate a date to the users locale.
Import the hook:
import { useFormatDate } from 'react-intl-hooks';
Then use it like this:
const t = useFormatDate();
The hook returns a translation function that can be used inside the JSX code of your components.
t(value: DateFormatPrimitiveValue, options?: FormatDateOptions);
The date to be translated, it can have the following types: Date
, number
or string
.
Object containing formating options of the time to be translated. Each property is optional and can recieve the following values: short
or numeric
.
{
year?: string,
month?: string,
day?: string,
hour?: string,
minute?: string,
second?: string,
weekday?: string,
era?: string
}
In the following example you see how to use the formatting options.
import React from 'react';
import { useFormatDate } from 'react-intl-hooks';
const MyComponent = () => {
const t = useFormatDate();
const todayDate = new Date('3/4/2016');
return (
<div>
<h1>
{t(todayDate, {
weekday: 'short',
year: 'numeric',
month: 'short',
day: 'numeric',
})}
</h1>
</div>
);
};
export default MyComponent;
<div>
<h1>Fri, Mar 4, 2016</h1>
</div>
useFormatTime
This hook is used to format a time to the user locale.
Import the hook:
import { useFormatTime } from 'react-intl-hooks';
Then use it like this:
const t = useFormatTime();
The hook returns a translation function that can be used inside the JSX code of your components.
t(value: TimeFormatPrimitiveValue, options?: FormatDateOptions);
The time to be translated, it can have the following types: Date
or number
.
Object containing formatting options of the time to be translated. Each property is optional and can recieve the following values: short
or numeric
.
{
year?: string,
month?: string,
day?: string,
hour?: string,
minute?: string,
second?: string,
weekday?: string,
era?: string
}
In the following example you see how to use the formatting options.
import React from 'react';
import { useFormatTime } from 'react-intl-hooks';
const MyComponent = () => {
const t = useFormatTime();
const todayDate = new Date('3/4/2016');
return (
<div>
<h1>{t(todayDate)}</h1>
</div>
);
};
export default MyComponent;
<div>
<h1>12:00 AM</h1>
</div>
useFormatNumber
This hook is used to format numbers to the user locale.
Import the hook:
import { useFormatNumber } from 'react-intl-hooks';
Then use it like this:
const t = useFormatNumber();
The hook returns a translation function that can be used inside the JSX code of your components.
t(value: number, options?: FormatNumberOptions);
The number to be formatted to the user locale.
Object containing formatting options of the number to be translated. Each property is optional.
{
style?: string,
unit?: string,
unitDisplay?: string,
currency?: string
}
currency
(e.g., $1,000).unit
and unitDisplay
(e.g., 1,000kB).style
property is set to "unit". Possible values are almost 27 units, we'll show some of the possible values:
style
is set to "currency". Possible values are the ISO 4217 abbreviations of the currencies:
In the following example you see how to use the formatting options.
import React from 'react';
import { useFormatNumber } from 'react-intl-hooks';
const MyComponent = () => {
const t = useFormatNumber();
return (
<div>
<h1>{t(1000, { style: 'currency', currency: 'USD' })}</h1>
</div>
);
};
export default MyComponent;
<div>
<h1>$1,000.00</h1>
</div>
useFormatRelativeTime
This hook recieves a number and formats it to a relative time.
Import the hook:
import { useFormatRelativeTime } from 'react-intl-hooks';
Then use it like this:
const t = useFormatRelativeTime();
The hook returns a translation function that can be used inside the JSX code of your components.
t(value: number, unit?: Unit, options?: FormatNumberOptions);
The number to be formatted into a relative time matching the use locale.
The unit of the number to be formatted. It can have the following values: second
, minute
, hour
, day
, week
, month
, quarter
or year.
Object containing formatting options of the time to be transformed into a relative time. Each property is optional.
{
numeric?: string,
style?: string
}
import React from 'react';
import { useFormatRelativeTime } from 'react-intl-hooks';
const MyComponent = () => {
const t = useFormatRelative();
return (
<div>
<h1>{t(1, 'hour')}</h1>
</div>
);
};
export default MyComponent;
<div>
<h1>in 1 hour</h1>
</div>
useFormatList
This hook allows you to join list of things together in an i18n-safe way.
Import the hook:
import { useFormatList } from 'react-intl-hooks';
Then use it like this:
const t = useFormatList();
The hook returns a translation function that can be used inside the JSX code of your components.
t(value: string[], options?: FormatListOptions);
Array of strings to be joined together.
Object containing formatting options for the list to be formatted. Each property is optional.
{
type?: string,
style?: string
}
import React from 'react';
import { useFormatList } from 'react-intl-hooks';
const MyComponent = () => {
const t = useFormatList();
return (
<div>
<h1>{t(['5 hours', '3 minutes'], { type: 'unit' })}</h1>
</div>
);
};
export default MyComponent;
<div>
<h1>5 hours, 3 minutes</h1>
</div>
useFormatPlural
This hook will return a plural category string: "zero", "one", "two", "few", "many", or "other".
Note: This hook should only be used in apps that need to support one language. If your app supports multiple languages use useFormatMessage
instead.
Import the hook:
import { useFormatPlural } from 'react-intl-hooks';
Then use it like this:
const t = useFormatPlural();
The hook returns a translation function that can be used inside the JSX code of your components.
t(value: number, options?: FormatPluralOptions);
Number to be pluralized.
Object containing formatting options for the number to be formatted. The property is optional.
{
type?: string,
}
type: the type of the output message. Possible values are:
import React from 'react';
import { useFormatPlural } from 'react-intl-hooks';
const MyComponent = () => {
const t = useFormatPlural();
return (
<div>
<h1>{t(2, { style: 'ordinal' })}</h1>
</div>
);
};
export default MyComponent;
<div>
<h1>two</h1>
</div>
We'd like to thank these awesome people who made this whole thing happen. [Become a contributor]
This project is licensed under the MIT license, Copyright (c) 2020 CreateThrive. [License]