@rockpack/localazer
v1.9.0
Published
This module can help you organize localization in your React application
Downloads
7
Maintainers
Readme
@rockpack/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, @rockpack/compiler and @rockpack/localizer can help us.
@rockpack/localazer this module is part of the Rockpack project. See more details on the official site.
Articles
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.
Using
- Installation:
# NPM
npm install @rockpack/localaser --save
npm install @rockpack/compiler --save-dev
# YARN
yarn add @rockpack/localaser
yarn add @rockpack/compiler --dev
- In order for language switching to work correctly, you need to wrap the application in a component
import { LocalizationObserver } from '@rockpack/localaser';
class Root extends Component {
render() {
return (
<LocalizationObserver currentLanguage={this.state.currentLanguage} languages={this.state.languages}>
<App/>
</LocalizationObserver>
)
}
}
- In the components where you need to translate, you need to add components with the default language:
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:
<Localization>
{
sprintf(
l('Your name is %s', 'USER'),
name
)
}
</Localization>
For plural forms:
<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.
- 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
const { localazer } = require('@rockpack/compiler');
localazer.makePot({
dist: './po',
src: './src'
});
Run the script
node makePOT.js
As a result, a dictionary with all text fragments for translation will be created.
To translate a dictionary, you must use POEdit tool:
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
const { localazer } = require('@rockpack/compiler');
localazer.po2json({
dist: './json',
src: './po'
});
Run the script
node po2json.js
- When you convert the translated snippets to JSON, you can add them to the component with and create a way to switch the language.
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>
)
}
}
@rockpack/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:
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
- 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
The MIT License
MIT