You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
This repository was archived by the owner on Jan 10, 2024. It is now read-only.
Tiny React library for implementing gettext localization in your application. It provides HOC function to enhance your application by exposing gettext functions in the context scope.
4
6
5
7
## Instalation
6
8
7
-
React Gettext requires **React 15.0 or later**.
9
+
React Gettext requires **React 15.0 or later**. You can add this package using following commands:
8
10
9
11
```
10
12
npm install react-gettext --save
11
13
```
12
14
15
+
```
16
+
yarn add react-gettext
17
+
```
18
+
13
19
## Usage
14
20
15
21
Let's assume you have following React application:
@@ -55,7 +61,7 @@ To make it translatable you need to update your `app.js` file to use HOC functio
55
61
```diff
56
62
// app.js
57
63
import React, { Component } from 'react';
58
-
+ import Textdomain from 'react-gettext';
64
+
+ import withGettext from 'react-gettext';
59
65
import Header from './Header';
60
66
import Footer from './Footer';
61
67
@@ -64,15 +70,16 @@ To make it translatable you need to update your `app.js` file to use HOC functio
64
70
...
65
71
}
66
72
67
-
+ export default Textdomain({...}, 'n != 1')(App);
73
+
+ export default withGettext({...}, 'n != 1')(App);
68
74
```
69
75
70
-
After doing it you can start using `gettext`, `ngettext`and `xgettext` functions in your descending components:
76
+
After doing it you can start using `gettext`, `ngettext`, `xgettext`and `nxgettext` functions in your descending components:
71
77
72
78
```diff
73
79
// Header.js
74
80
- import React, { Component } from 'react';
75
-
+ import React, { Component, PropTypes } from 'react';
81
+
+ import React, { Component } from 'react';
82
+
+ import PropTypes from 'prop-types';
76
83
77
84
export default class Header extends Component {
78
85
@@ -88,13 +95,14 @@ After doing it you can start using `gettext`, `ngettext` and `xgettext` function
88
95
+ Header.contextTypes = {
89
96
+ gettext: PropTypes.func.isRequired,
90
97
+ ngettext: PropTypes.func.isRequired,
91
-
+ xgettext: PropTypes.func.isRequired
98
+
+ xgettext: PropTypes.func.isRequired,
99
+
+ nxgettext: PropTypes.func.isRequired,
92
100
+ };
93
101
```
94
102
95
103
## Documentation
96
104
97
-
### Textdomain(translations, pluralForms)
105
+
### withGettext(translations, pluralForms)
98
106
99
107
Higher-order function which is exported by default from `react-gettext` package. It accepts two arguments and returns function to create higher-order component.
100
108
@@ -111,7 +119,7 @@ const translations = {
111
119
112
120
constpluralForms='(n%10==1 && n%100!=11 ? 0 : n%10>=2 && n%10<=4 && (n%100<10 || n%100>=20) ? 1 : 2)'; // 3 plural forms for Russian, Belarusian, Bosnian, Croatian, Serbian, Ukrainian, etc.
The function to translate a string. Accepts original message and returns translation if it exists, otherwise original message.
@@ -171,14 +201,49 @@ Example:
171
201
this.context.xgettext('some text', 'context where this message is used');
172
202
```
173
203
204
+
### nxgettext(singular, plural, n, context)
205
+
206
+
The function to translate plural string based on a specific context. Accepts singular and plural messages along with a number to calculate plural form against and context string. Returns translated message based on plural form if it exists, otherwise original message based on **n** value.
207
+
208
+
-**singular**: a string to be translated when count is not plural
209
+
-**plural**: a string to be translated when count is plural
If you use Poedit app to translate your messages, then you can use `gettext;ngettext:1,2;xgettext:1,2c` as keywords list to properly parse and extract strings from your javascript files.
222
+
If you use Poedit app to translate your messages, then you can use `gettext;ngettext:1,2;xgettext:1,2c;nxgettext:1,2,4c` as keywords list to properly parse and extract strings from your javascript files.
223
+
224
+
Here is an example of a **POT** file which you can start with:
What to help or have a suggestion? Open a [new ticket](https://github.com/eugene-manuilov/react-gettext/issues/new) and we can discuss it or submit pull request. Please, make sure you run `npm run test` before submitting a pull request.
245
+
What to help or have a suggestion? Open a [new ticket](https://github.com/eugene-manuilov/react-gettext/issues/new) and we can discuss it or submit pull request. Please, make sure you run `npm test` before submitting a pull request.
0 commit comments