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

Commit 702a3dd

Browse files
Merge pull request #2 from eugene-manuilov/feature/documentation-update-for-0.3.0
Feature/documentation update for 0.3.0
2 parents 716e862 + 72452a4 commit 702a3dd

File tree

4 files changed

+86
-15
lines changed

4 files changed

+86
-15
lines changed

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

+72-9
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,16 @@ Tiny React library for implementing gettext localization in your application. It
66

77
## Instalation
88

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

1111
```
1212
npm install react-gettext --save
1313
```
1414

15+
```
16+
yarn add react-gettext
17+
```
18+
1519
## Usage
1620

1721
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
5761
```diff
5862
// app.js
5963
import React, { Component } from 'react';
60-
+ import Textdomain from 'react-gettext';
64+
+ import withGettext from 'react-gettext';
6165
import Header from './Header';
6266
import Footer from './Footer';
6367

@@ -66,15 +70,16 @@ To make it translatable you need to update your `app.js` file to use HOC functio
6670
...
6771
}
6872

69-
+ export default Textdomain({...}, 'n != 1')(App);
73+
+ export default withGettext({...}, 'n != 1')(App);
7074
```
7175

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

7478
```diff
7579
// Header.js
7680
- import React, { Component } from 'react';
77-
+ import React, { Component, PropTypes } from 'react';
81+
+ import React, { Component } from 'react';
82+
+ import PropTypes from 'prop-types';
7883

7984
export default class Header extends Component {
8085

@@ -90,13 +95,14 @@ After doing it you can start using `gettext`, `ngettext` and `xgettext` function
9095
+ Header.contextTypes = {
9196
+ gettext: PropTypes.func.isRequired,
9297
+ ngettext: PropTypes.func.isRequired,
93-
+ xgettext: PropTypes.func.isRequired
98+
+ xgettext: PropTypes.func.isRequired,
99+
+ nxgettext: PropTypes.func.isRequired,
94100
+ };
95101
```
96102

97103
## Documentation
98104

99-
### Textdomain(translations, pluralForms)
105+
### withGettext(translations, pluralForms)
100106

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

@@ -113,7 +119,7 @@ const translations = {
113119

114120
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.
115121

116-
const HOC = Textdomain(translations, pluralForms)(App);
122+
const HOC = withGettext(translations, pluralForms)(App);
117123
```
118124

119125
```javascript
@@ -128,9 +134,31 @@ function getPluralForms(n) {
128134
return n > 1 ? 1 : 0;
129135
}
130136

131-
const HOC = Textdomain(getTranslations, getPluralForms)(App);
137+
const HOC = withGettext(getTranslations, getPluralForms)(App);
138+
```
139+
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>, ...);
132159
```
133160

161+
134162
### gettext(message)
135163

136164
The function to translate a string. Accepts original message and returns translation if it exists, otherwise original message.
@@ -173,10 +201,45 @@ Example:
173201
this.context.xgettext('some text', 'context where this message is used');
174202
```
175203

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+
176220
## Poedit
177221

178222
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.
179223

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+
```
242+
180243
## Contribute
181244

182245
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.

src/Textdomain.js

+4-4
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@ class Textdomain extends Component {
1515
}
1616

1717
getTranslations() {
18-
const { catalog } = this.props;
19-
return typeof catalog === 'function' ? catalog() : catalog;
18+
const { translations } = this.props;
19+
return typeof translations === 'function' ? translations() : translations;
2020
}
2121

2222
getPluralForm(n) {
@@ -103,7 +103,7 @@ class Textdomain extends Component {
103103
}
104104

105105
Textdomain.propTypes = {
106-
catalog: PropTypes.oneOfType([
106+
translations: PropTypes.oneOfType([
107107
PropTypes.func,
108108
PropTypes.objectOf(PropTypes.oneOfType([
109109
PropTypes.string,
@@ -118,7 +118,7 @@ Textdomain.propTypes = {
118118
};
119119

120120
Textdomain.defaultProps = {
121-
catalog: {},
121+
translations: {},
122122
plural: 'n != 1',
123123
children: [],
124124
};

src/index.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import React from 'react';
22
import hoistNonReactStatic from 'hoist-non-react-statics';
33
import Textdomain from './Textdomain';
44

5-
export default function withGettext(translations, pluralForm) {
5+
export default function withGettext(translations = {}, pluralForm = 'n != 1') {
66
return (WrappedComponent) => {
77
class WithGettext extends Textdomain {
88

@@ -13,7 +13,7 @@ export default function withGettext(translations, pluralForm) {
1313
}
1414

1515
WithGettext.defaultProps = {
16-
catalog: translations,
16+
translations,
1717
plural: pluralForm,
1818
};
1919

0 commit comments

Comments
 (0)