@rockpack/localazer
Version:
This module can help you organize localization in your React application
233 lines (178 loc) • 6.57 kB
Markdown
<p align="center">
<img alt="Rockpack" src="https://www.rockpack.io/readme_assets/rockpack_logo_without_text.png">
</p>
# /localazer
Most application localization approaches use JSON files as the storage location. JSON is a convenient format for a developer but not for a localizer. The localizer works in specialized software that must maintain correct spelling, find typos, and combine GIT-style developments between versions of the application to form a dictionary.
The most common approach for localizing applications is **gettext**. This is a set of localization programs that organize spell checking, merge different versions of application localizations, and delete unnecessary text data. This application has been used by most of the desktop developers since the 90s.
In order to organize the communication of our **React** application with **gettext** and back, **/compiler** and **/localizer** can help us.
**/localazer** this module is part of the **Rockpack** project. See more details on [the official site](https://www.rockpack.io/).
## Articles
- [Localization. True way [Article]](https://www.rockpack.io/localization-true-way)
## How it works
Stage 1. We need to add localization and make it friends with our application.
Stage 2. Extract all the data for the dictionary from our application and pass it in the gettext format to the translator.
Stage 3. Having received the finished translation, we must overtake it into JSON and insert it into our application.
<p align="center">
<img src="https://www.rockpack.io/readme_assets/localazer-approach.jpg" alt="Localization approach" />
</p>
## Using
1. Installation:
```sh
# NPM
npm install /localaser --save
npm install /compiler --save-dev
# YARN
yarn add /localaser
yarn add /compiler --dev
```
2. In order for language switching to work correctly, you need to wrap the application in a *<LocalizationObserver>* component
```jsx
import { LocalizationObserver } from '@rockpack/localaser';
class Root extends Component {
render() {
return (
<LocalizationObserver currentLanguage={this.state.currentLanguage} languages={this.state.languages}>
<App/>
</LocalizationObserver>
)
}
}
```
3. In the components where you need to translate, you need to add components with the default language:
```jsx
import Localization, { LocalizationObserver, l, nl, sprintf } from '@rockpack/localaser';
...
<h2><Localization>{l('Hello')}</Localization></h2>
```
If you want to use variables in translation, you need to use:
```jsx
<Localization>
{
sprintf(
l('Your name is %s', 'USER'),
name
)
}
</Localization>
```
For plural forms:
```jsx
<Localization>
{
sprintf(
nl(
'%d click',
'%d clicks',
count
),
count
)
}
</Localization>
```
As a result, with count = 0, the text will be displayed 0 clicks, with count = 1 - 1 click.
5. After the text for localization has been added to the application, you need to extract the dictionary with these text fragments.
5.1 Make **makePOT.js** in the root of project
```js
const { localazer } = require('@rockpack/compiler');
localazer.makePot({
dist: './po',
src: './src'
});
```
Run the script
```sh
node makePOT.js
```
As a result, a dictionary with all text fragments for translation will be created.
To translate a dictionary, you must use <a href="https://poedit.net/download">POEdit tool</a>:
<p align="center">
<img src="http://www.natrube.net/localazer/assets/poedit.png" alt="POEdit" />
</p>
5.2 After translating the dictionary through POEdit, you need to save the **mo** file with the created translation. This file must be added to the project. Then it needs to be converted to JSON:
Make **po2json.js** in the root of project
```js
const { localazer } = require('@rockpack/compiler');
localazer.po2json({
dist: './json',
src: './po'
});
```
Run the script
```shell script
node po2json.js
```
6. When you convert the translated snippets to JSON, you can add them to the component with *<LocalizationObserver>* and create a way to switch the language.
```jsx
import ru from '../json/ru.json';
class Root extends Component {
constructor(props) {
super(props);
this.state = {
currentLanguage: 'en',
languages: { ru }
}
}
setCurrentLanguage = (currentLanguage) => {
this.setState({ currentLanguage })
}
render() {
return (
<LocalizationObserver currentLanguage={this.state.currentLanguage} languages={this.state.languages}>
<App setCurrentLanguage={setCurrentLanguage} />
</LocalizationObserver>
)
}
}
```
**/localazer** is not responsible for passing translations to the app. You can do this of your choice, for example through dynamic imports, backend API, Redux, Local Storage, etc.
An example of receiving converted transfers via the Backend API:
```jsx
class Root extends Component {
constructor(props) {
super(props);
this.state = {
currentLanguage: 'en',
languages: {}
}
}
componentDidMount() {
this.getLanguages();
}
getLanguages = () => {
fetch('http://localhost:8000/api/languages')
.then(response => {
return response.json();
})
.then(({ languages }) => {
this.setState({ languages });
});
}
setCurrentLanguage = (currentLanguage) => {
this.setState({ currentLanguage })
}
render() {
return (
<LocalizationObserver currentLanguage={this.state.currentLanguage} languages={this.state.languages}>
<App setCurrentLanguage={setCurrentLanguage} />
</LocalizationObserver>
)
}
}
```
## Properties
- \<LocalizationObserver /> properties:
| Property | Type | Description |
| --- | --- | --- |
| currentLanguage | String | Set active language |
| defaultLanguage | String['en'] | Default language |
| languages | Object | Object with JSON translations |
## Q&A
How do I use gettext on Windows?
- *It needs to install **gettext** version no earlier than 0.20 (for supporting JS)*
- You can download the latest gettext Windows version [here](https://mlocati.github.io/articles/gettext-iconv-windows.html)
- Before running localazer makePOT, you need to make sure that gettext, xgettext are available in the console
- To edit PO and POT files, you need to install [POEdit](https://poedit.net/download)
***
## The MIT License
<a href="https://github.com/AlexSergey/rockpack#the-mit-license" target="_blank">MIT</a>