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.
Copy file name to clipboardexpand all lines: README.md
+72-9
Original file line number
Diff line number
Diff line change
@@ -6,12 +6,16 @@ Tiny React library for implementing gettext localization in your application. It
6
6
7
7
## Instalation
8
8
9
-
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:
10
10
11
11
```
12
12
npm install react-gettext --save
13
13
```
14
14
15
+
```
16
+
yarn add react-gettext
17
+
```
18
+
15
19
## Usage
16
20
17
21
Let's assume you have following React application:
@@ -57,7 +61,7 @@ To make it translatable you need to update your `app.js` file to use HOC functio
57
61
```diff
58
62
// app.js
59
63
import React, { Component } from 'react';
60
-
+ import Textdomain from 'react-gettext';
64
+
+ import withGettext from 'react-gettext';
61
65
import Header from './Header';
62
66
import Footer from './Footer';
63
67
@@ -66,15 +70,16 @@ To make it translatable you need to update your `app.js` file to use HOC functio
66
70
...
67
71
}
68
72
69
-
+ export default Textdomain({...}, 'n != 1')(App);
73
+
+ export default withGettext({...}, 'n != 1')(App);
70
74
```
71
75
72
-
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:
73
77
74
78
```diff
75
79
// Header.js
76
80
- import React, { Component } from 'react';
77
-
+ import React, { Component, PropTypes } from 'react';
81
+
+ import React, { Component } from 'react';
82
+
+ import PropTypes from 'prop-types';
78
83
79
84
export default class Header extends Component {
80
85
@@ -90,13 +95,14 @@ After doing it you can start using `gettext`, `ngettext` and `xgettext` function
90
95
+ Header.contextTypes = {
91
96
+ gettext: PropTypes.func.isRequired,
92
97
+ ngettext: PropTypes.func.isRequired,
93
-
+ xgettext: PropTypes.func.isRequired
98
+
+ xgettext: PropTypes.func.isRequired,
99
+
+ nxgettext: PropTypes.func.isRequired,
94
100
+ };
95
101
```
96
102
97
103
## Documentation
98
104
99
-
### Textdomain(translations, pluralForms)
105
+
### withGettext(translations, pluralForms)
100
106
101
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.
102
108
@@ -113,7 +119,7 @@ const translations = {
113
119
114
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.
@@ -173,10 +201,45 @@ Example:
173
201
this.context.xgettext('some text', 'context where this message is used');
174
202
```
175
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;nxgettext:1,2,4c` as keywords list to properly parse and extract strings from your javascript files.
179
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 test` before submitting a pull request.
0 commit comments