3
3
[ ![ npm] ( https://img.shields.io/npm/v/react-localized.svg )] ( https://www.npmjs.com/package/react-localized )
4
4
[ ![ npm] ( https://img.shields.io/npm/v/preact-localized.svg )] ( https://www.npmjs.com/package/preact-localized )
5
5
6
- Internationalization for React and Preact components.
6
+ Complete internationalization tool for React and Preact components.
7
7
8
8
Features:
9
- - based on ` gettext ` format, uses translation ` .po ` files
10
9
11
- - translation strings can be extracted automatically from source files
10
+ - based on [ gettext] ( https://www.gnu.org/software/gettext/manual/gettext.html ) format, uses translation ` .po ` files
11
+
12
+ - translation strings for ` .po ` files can be extracted automatically from project source files
12
13
13
14
- plural forms are supported
14
15
16
+ - gettext function aliases are supported
17
+
18
+ - JavaScript string templates are supported
19
+
15
20
- locale data is extendable, for example by adding date formats, currencies, etc.
16
21
17
22
- locale data can be separated from the main bundle by using dynamic import
18
23
19
- ### Component example
24
+ ### Basic component example
20
25
21
26
``` js
22
27
import { useLocales } from ' react-localized' // or from 'preact-localized'
@@ -25,15 +30,21 @@ export default () => {
25
30
const { gettext } = useLocales ()
26
31
return (
27
32
<>
28
- { gettext (' Hello, world !' ) } // Привет, мир !
33
+ { gettext (' Hello, World !' ) } // Привет, Мир !
29
34
< / >
30
35
)
31
36
}
32
37
```
33
38
39
+ ### Complex example
40
+
41
+ [ Live demo] ( http://fakundo.github.io/react-localized/ )
42
+ |
43
+ [ Source] ( https://github.com/fakundo/react-localized/tree/master/examples )
44
+
34
45
### Installation and usage
35
46
36
- #### 1. Use existing ` .po ` files with translation or generate them from your project source files
47
+ #### 1. Use existing ` .po ` files with translation messages or generate them from your project source files
37
48
38
49
To generate ` .po ` files you can use the extractor CLI tool. Extractor searches your project source files for functions like ` gettext ` , ` ngettext ` , etc. Extractor also can update existing ` .po ` files without erasing existing translations in those files.
39
50
@@ -47,18 +58,20 @@ react-localized-extractor [options]
47
58
Options:
48
59
--version Show version number [boolean]
49
60
--help Show help [boolean]
50
- --locales, -l List of desired locales [array ] [required]
61
+ --locales, -l List of desired locales (comma separated) [string ] [required]
51
62
--source, -s Source files pattern
52
- [string] [default: "./src/**/*.@(js|ts|jsx|tsx)"]
53
- --output, -o Output path [string] [default: "./locales"]
54
- --save-pot Should save .pot file [boolean] [default: false]
63
+ [string] [default: "./src/**/*.@(js|ts|jsx|tsx)"]
64
+ --output, -o Output .po files directory [string] [default: "./locales"]
65
+ --alias, -a Function alias [string]
66
+ --save-pot Should create catalog .pot file in output directory
67
+ [boolean] [default: false]
55
68
```
56
69
57
70
``` console
58
- react-localized-extractor -l ru -s ./src/**/*.js -o ./locales
71
+ react-localized-extractor -l ru
59
72
```
60
73
61
- #### 2. Add / edit translation in ` .po ` files
74
+ #### 2. Modify ` .po ` files to add / edit translation
62
75
63
76
Use your favourite editor for ` .po ` files. I recommend you to use ` Poedit ` .
64
77
@@ -91,11 +104,11 @@ npm i react-localized
91
104
npm i preact-localized
92
105
```
93
106
94
- #### 5. Create locale data
107
+ #### 5. Create data object for each locale
95
108
96
- Use the second argument for extra data such as date formats, currencies, and so on.
109
+ Use the ` createLocale ` function from the package. The first argument is for translation messages taken from ` .po ` file. The second argument is for extra data such as date formats, currencies, and so on.
97
110
98
- Example of the file ` ru.js `
111
+ Example of the file ` ru.js ` for russian locale
99
112
100
113
``` js
101
114
import { createLocale } from ' react-localized' // or from 'preact-localized'
@@ -106,7 +119,7 @@ const extra = { ... }
106
119
export default createLocale (messages, extra)
107
120
```
108
121
109
- ##### 6. Render provider
122
+ #### 6. Render provider component
110
123
111
124
``` js
112
125
import { LocalizedProvider } from ' react-localized' // or from 'preact-localized'
@@ -119,7 +132,10 @@ const ru = () => import('ru.js').then(data => data.default) // separated from th
119
132
const locales = { fr, de, ru }
120
133
121
134
export default () => (
122
- < LocalizedProvider locales= {locales} selected= " fr" >
135
+ < LocalizedProvider
136
+ locales= {locales}
137
+ selected= " fr"
138
+ >
123
139
{ ({ localeReady }) => (
124
140
localeReady
125
141
? ' render children'
@@ -138,10 +154,10 @@ export default () => {
138
154
const { gettext , ngettext , ... extra } = useLocales ()
139
155
return (
140
156
<>
141
- { gettext (' Hello, world !' ) } // Привет, мир !
142
- { ngettext (' %s apple' , ' %s apples' , 1 , 1 ) } // 1 яблоко
143
- { ngettext (' %s apple' , ' %s apples' , 2 , 2 ) } // 2 яблока
144
- { ngettext (' %s apple' , ' %s apples' , 10 , 10 ) } // 10 яблок
157
+ { gettext (' Hello, World !' ) } // Привет, Мир !
158
+ { ngettext (' apple' , ' apples' , 1 ) } // яблоко
159
+ { ngettext (' apple' , ' apples' , 2 ) } // яблока
160
+ { ngettext (' apple' , ' apples' , 5 ) } // яблок
145
161
< / >
146
162
)
147
163
}
@@ -154,10 +170,9 @@ import { withLocales } from 'react-localized' // or from 'preact-localized'
154
170
155
171
class LocalizedComponent extends Component {
156
172
render () {
157
- const { gettext , pgettext , formatDate , formats } = this .props // 'formatDate' and 'formats' are extra data passed to the 'createLocale'
173
+ const { pgettext , formatDate , formats } = this .props // 'formatDate' and 'formats' are extra data passed to the 'createLocale'
158
174
return (
159
175
<>
160
- { gettext (' My name is %s' , ' John' ) } // Мое имя John
161
176
{ pgettext (' Context' , ' Text with context' ) } // Текст с контекстом
162
177
{ formatDate (new Date (), formats .date ) } // 1 января 2020
163
178
< / >
@@ -167,28 +182,91 @@ class LocalizedComponent extends Component {
167
182
168
183
export default withLocales ()(LocalizedComponent)
169
184
```
185
+ ### Using string templates
170
186
171
- ### See example
187
+ ``` js
188
+ import { useLocales } from ' react-localized' // or from 'preact-localized'
172
189
173
- [ Demo] ( http://fakundo.github.io/react-localized/ )
174
- |
175
- [ Source] ( https://github.com/fakundo/react-localized/tree/master/examples )
190
+ export default () => {
191
+ const { gettext , ngettext , pgettext , npgettext } = useLocales ()
192
+ const name = ' Anna'
193
+ const i = 2
194
+ return (
195
+ <>
196
+ { gettext` My name is ${ name} ` } // Мое имя Anna
197
+ { ngettext` ${ i} apple` ` ${ i} apples` (i) } // 2 яблока
198
+ { pgettext (' Ctx' )` My name is ${ name} ` }
199
+ { npgettext (' Ctx' )` ${ i} apple` ` ${ i} apples` (i) }
200
+ < / >
201
+ )
202
+ }
203
+ ```
204
+
205
+ ### Using function aliases
206
+
207
+ Use ` LocalizedProvider ` ` alias ` prop to define function aliases.
208
+
209
+ Example of alias for ` gettext ` only
210
+
211
+ ``` js
212
+ < LocalizedProvider alias= " __" / >
213
+ ```
214
+
215
+ Example of aliases for ` gettext ` and ` ngettext `
216
+
217
+ ``` js
218
+ < LocalizedProvider alias= {{ gettext: ' __' , ngettext: ' __n' }} / >
219
+ ```
220
+
221
+ Example of alias usage
222
+
223
+ ``` js
224
+ import { useLocales } from ' react-localized' // or from 'preact-localized'
225
+
226
+ export default () => {
227
+ const { __ , __n } = useLocales ()
228
+ const name = ' Anna'
229
+ return (
230
+ <>
231
+ { __ (' Hello, World!' ) }
232
+ { __` My name is ${ name} ` }
233
+ { __n` apple` ` apples` (5 ) }
234
+ < / >
235
+ )
236
+ }
237
+ ```
238
+
239
+ Also configure extractor.
240
+
241
+ Example of alias for ` gettext ` only
242
+
243
+ ``` console
244
+ react-localized-extractor -l ru -a __
245
+ ```
246
+
247
+ Example of aliases for ` gettext ` and ` ngettext `
248
+
249
+ ``` console
250
+ react-localized-extractor -l ru -a.gettext __ -a.ngettext __n
251
+ ```
176
252
177
253
### API
178
254
179
255
#### LocalizedProvider props
180
256
181
257
- ` locales ` - defined locales
182
258
- ` selected ` - selected locale (default ` en ` )
259
+ - ` alias ` - function aliases (string or object)
183
260
- ` children({ localeReady }) `
184
261
185
262
#### Data returned by hook / props passed to the child of the HOC
186
263
187
264
- ` locale `
188
- - ` gettext(input, ...injections) `
189
- - ` ngettext(singular, plural, n, ...injections) `
190
- - ` pgettext(context, input, ...injections) `
191
- - ` npgettext(context, singular, plural, n, ...injections) `
265
+ - ` gettext(text) `
266
+ - ` ngettext(text, textPlural, n) `
267
+ - ` pgettext(context, text) `
268
+ - ` npgettext(context, text, textPlural, n) `
269
+ - ` ...aliases ` - defined function aliases
192
270
- ` ...extra ` - extra data passed to the ` createLocale `
193
271
194
272
### License
0 commit comments