Skip to content
This repository was archived by the owner on Jan 10, 2024. It is now read-only.

Commit 1778757

Browse files
Merge pull request #3 from eugene-manuilov/release/0.3.0
Release/0.3.0
2 parents ac3ace8 + 702a3dd commit 1778757

14 files changed

+726
-361
lines changed

.eslintrc

+4-2
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@
33
"rules": {
44
"func-names": 0,
55
"no-tabs": 0,
6-
"react/jsx-filename-extension": 0,
7-
"indent": ["error", "tab"]
6+
"max-len": 1,
7+
"class-methods-use-this": 0,
8+
"indent": [2, "tab"],
9+
"react/jsx-filename-extension": 0
810
}
911
}

.travis.yml

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
language: node_js
2+
node_js:
3+
- "6"
4+
script:
5+
- "npm test"

CHANGELOG.md

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# Change Log
2+
3+
## v0.3.0 (2017-06-26)
4+
5+
**Implemented enhancements:**
6+
7+
- Added nxgettext function which allows to translate plural strings with context.
8+
- Extracted HOC into a separate component and updated it to inherit from that standalone component.

README.md

+78-13
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,21 @@
1-
# react-gettext
1+
# react-gettext 0.3.0
2+
3+
[![Build Status](https://travis-ci.org/eugene-manuilov/react-gettext.svg?branch=master)](https://travis-ci.org/eugene-manuilov/react-gettext)
24

35
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.
46

57
## Instalation
68

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:
810

911
```
1012
npm install react-gettext --save
1113
```
1214

15+
```
16+
yarn add react-gettext
17+
```
18+
1319
## Usage
1420

1521
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
5561
```diff
5662
// app.js
5763
import React, { Component } from 'react';
58-
+ import Textdomain from 'react-gettext';
64+
+ import withGettext from 'react-gettext';
5965
import Header from './Header';
6066
import Footer from './Footer';
6167

@@ -64,15 +70,16 @@ To make it translatable you need to update your `app.js` file to use HOC functio
6470
...
6571
}
6672

67-
+ export default Textdomain({...}, 'n != 1')(App);
73+
+ export default withGettext({...}, 'n != 1')(App);
6874
```
6975

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:
7177

7278
```diff
7379
// Header.js
7480
- import React, { Component } from 'react';
75-
+ import React, { Component, PropTypes } from 'react';
81+
+ import React, { Component } from 'react';
82+
+ import PropTypes from 'prop-types';
7683

7784
export default class Header extends Component {
7885

@@ -88,13 +95,14 @@ After doing it you can start using `gettext`, `ngettext` and `xgettext` function
8895
+ Header.contextTypes = {
8996
+ gettext: PropTypes.func.isRequired,
9097
+ ngettext: PropTypes.func.isRequired,
91-
+ xgettext: PropTypes.func.isRequired
98+
+ xgettext: PropTypes.func.isRequired,
99+
+ nxgettext: PropTypes.func.isRequired,
92100
+ };
93101
```
94102

95103
## Documentation
96104

97-
### Textdomain(translations, pluralForms)
105+
### withGettext(translations, pluralForms)
98106

99107
Higher-order function which is exported by default from `react-gettext` package. It accepts two arguments and returns function to create higher-order component.
100108

@@ -111,7 +119,7 @@ const translations = {
111119

112120
const pluralForms = '(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.
113121

114-
const HOC = Textdomain(translations, pluralForms)(App);
122+
const HOC = withGettext(translations, pluralForms)(App);
115123
```
116124

117125
```javascript
@@ -126,9 +134,31 @@ function getPluralForms(n) {
126134
return n > 1 ? 1 : 0;
127135
}
128136

129-
const HOC = Textdomain(getTranslations, getPluralForms)(App);
137+
const HOC = withGettext(getTranslations, getPluralForms)(App);
130138
```
131139

140+
As an alternative you can pass translations and plural form as properties to higher-order-component, like this:
141+
142+
```javascript
143+
function getTranslations() {
144+
return {
145+
'Some text': 'Some translated text',
146+
...
147+
};
148+
}
149+
150+
function getPluralForms(n) {
151+
return n > 1 ? 1 : 0;
152+
}
153+
154+
const HOC = withGettext()(App);
155+
156+
...
157+
158+
ReactDOM.render(<HOC translations={getTranslations} plural={getPluralForms}>...</HOC>, ...);
159+
```
160+
161+
132162
### gettext(message)
133163

134164
The function to translate a string. Accepts original message and returns translation if it exists, otherwise original message.
@@ -171,14 +201,49 @@ Example:
171201
this.context.xgettext('some text', 'context where this message is used');
172202
```
173203

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
210+
- **n**: a number to count plural form
211+
- **context**: A context to search translation in.
212+
213+
Example:
214+
215+
```javascript
216+
// somewhere in your jsx component
217+
this.context.nxgettext('day ago', 'days ago', numberOfDays, 'Article publish date');
218+
```
219+
174220
## Poedit
175221

176-
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:
225+
226+
```
227+
msgid ""
228+
msgstr ""
229+
"Plural-Forms: nplurals=2; plural=(n != 1);\n"
230+
"Project-Id-Version: \n"
231+
"POT-Creation-Date: \n"
232+
"PO-Revision-Date: \n"
233+
"Last-Translator: \n"
234+
"Language-Team: \n"
235+
"MIME-Version: 1.0\n"
236+
"Content-Type: text/plain; charset=iso-8859-1\n"
237+
"Content-Transfer-Encoding: 8bit\n"
238+
"X-Poedit-Basepath: ./src\n"
239+
"X-Poedit-KeywordsList: gettext;ngettext:1,2;xgettext:1,2c;nxgettext:1,2,4c\n"
240+
"X-Poedit-SourceCharset: UTF-8\n"
241+
```
177242

178243
## Contribute
179244

180-
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.
181246

182247
## License
183248

184-
MIT
249+
MIT

__tests__/__snapshots__/context.js.snap

-30
This file was deleted.

0 commit comments

Comments
 (0)