i18n-js 3.6.0 → 3.9.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/.github/FUNDING.yml +3 -0
- data/.github/workflows/tests.yaml +100 -0
- data/CHANGELOG.md +77 -1
- data/README.md +482 -341
- data/app/assets/javascripts/i18n.js +24 -21
- data/i18n-js.gemspec +1 -1
- data/i18njs.png +0 -0
- data/lib/i18n/js/formatters/js.rb +9 -2
- data/lib/i18n/js/middleware.rb +1 -1
- data/lib/i18n/js/private/config_store.rb +31 -0
- data/lib/i18n/js/segment.rb +2 -1
- data/lib/i18n/js/utils.rb +13 -0
- data/lib/i18n/js/version.rb +1 -1
- data/lib/i18n/js.rb +25 -10
- data/package.json +2 -2
- data/spec/fixtures/js_available_locales_custom.yml +1 -0
- data/spec/js/json_parsable.spec.js +14 -0
- data/spec/js/localization.spec.js +14 -0
- data/spec/js/numbers.spec.js +4 -0
- data/spec/js/translate.spec.js +13 -3
- data/spec/js/translations.js +27 -2
- data/spec/ruby/i18n/js/segment_spec.rb +75 -8
- data/spec/ruby/i18n/js/utils_spec.rb +32 -0
- data/spec/ruby/i18n/js_spec.rb +103 -54
- data/spec/spec_helper.rb +1 -0
- data/yarn.lock +32 -25
- metadata +16 -9
- data/.travis.yml +0 -39
data/README.md
CHANGED
@@ -1,17 +1,22 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
1
|
+
<p align="center">
|
2
|
+
<img width="250" height="58" src="https://github.com/fnando/i18n-js/raw/main/i18njs.png" alt="i18n.js">
|
3
|
+
</p>
|
4
|
+
|
5
|
+
<p align="center">
|
6
|
+
It's a small library to provide the Rails I18n translations on the JavaScript.
|
7
|
+
</p>
|
8
|
+
|
9
|
+
<p align="center">
|
10
|
+
<a href="https://github.com/fnando/i18n-js/actions?query=workflow%3ATests"><img src="https://github.com/fnando/i18n-js/workflows/Tests/badge.svg" alt="Tests"></a>
|
11
|
+
<a href="http://badge.fury.io/rb/i18n-js"><img src="http://img.shields.io/gem/v/i18n-js.svg" alt="Gem Version"></a>
|
12
|
+
<a href="https://www.npmjs.com/package/i18n-js"><img src="https://img.shields.io/npm/v/i18n-js.svg" alt="npm"></a>
|
13
|
+
<a href="https://opensource.org/licenses/MIT"><img src="https://img.shields.io/badge/License-MIT-yellow.svg" alt="License: MIT"></a>
|
14
|
+
<a href="https://travis-ci.org/fnando/i18n-js"><img src="http://img.shields.io/travis/fnando/i18n-js.svg" alt="Build Status"></a>
|
15
|
+
<a href="https://coveralls.io/r/fnando/i18n-js"><img src="http://img.shields.io/coveralls/fnando/i18n-js.svg" alt="Coverage Status"></a>
|
16
|
+
<a href="https://gitter.im/fnando/i18n-js"><img src="https://img.shields.io/badge/gitter-join%20chat-1dce73.svg" alt="Gitter"></a>
|
17
|
+
</p>
|
18
|
+
|
19
|
+
---
|
15
20
|
|
16
21
|
Features:
|
17
22
|
|
@@ -23,8 +28,9 @@ Features:
|
|
23
28
|
- Lots more! :)
|
24
29
|
|
25
30
|
## Version Notice
|
26
|
-
The `master` branch (including this README) is for latest `3.0.0` instead of `2.x`.
|
27
31
|
|
32
|
+
The `main` branch (including this README) is for latest `3.0.0` instead of
|
33
|
+
`2.x`.
|
28
34
|
|
29
35
|
## Usage
|
30
36
|
|
@@ -33,14 +39,31 @@ The `master` branch (including this README) is for latest `3.0.0` instead of `2.
|
|
33
39
|
#### Rails app
|
34
40
|
|
35
41
|
Add the gem to your Gemfile.
|
42
|
+
|
36
43
|
```ruby
|
37
44
|
gem "i18n-js"
|
38
45
|
```
|
39
46
|
|
47
|
+
#### Rails with [webpacker](https://github.com/rails/webpacker)
|
48
|
+
|
49
|
+
If you're using `webpacker`, you may need to add the dependencies to your client
|
50
|
+
with:
|
51
|
+
|
52
|
+
```
|
53
|
+
yarn add i18n-js
|
54
|
+
# or, if you're using npm,
|
55
|
+
npm install i18n-js
|
56
|
+
```
|
57
|
+
|
58
|
+
For more details, see:
|
59
|
+
- [this gist](https://gist.github.com/bazzel/ecdff4718962e57c2d5569cf01d332fe)
|
60
|
+
- https://github.com/fnando/i18n-js/issues/597
|
61
|
+
|
40
62
|
#### Rails app with [Asset Pipeline](http://guides.rubyonrails.org/asset_pipeline.html)
|
41
63
|
|
42
|
-
If you're using the
|
43
|
-
then you
|
64
|
+
If you're using the
|
65
|
+
[asset pipeline](http://guides.rubyonrails.org/asset_pipeline.html), then you
|
66
|
+
must add the following line to your `app/assets/javascripts/application.js`.
|
44
67
|
|
45
68
|
```javascript
|
46
69
|
//
|
@@ -56,9 +79,9 @@ then you must add the following line to your `app/assets/javascripts/application
|
|
56
79
|
|
57
80
|
#### Rails app without [Asset Pipeline](http://guides.rubyonrails.org/asset_pipeline.html)
|
58
81
|
|
82
|
+
First, put this in your `application.html` (layout file). Then get the JS files
|
83
|
+
following the instructions below.
|
59
84
|
|
60
|
-
First, put this in your `application.html` (layout file).
|
61
|
-
Then get the JS files following the instructions below.
|
62
85
|
```erb
|
63
86
|
<%# This is just an example, you can put `i18n.js` and `translations.js` anywhere you like %>
|
64
87
|
<%# Unlike the Asset Pipeline example, you need to require both **in order** %>
|
@@ -66,55 +89,66 @@ Then get the JS files following the instructions below.
|
|
66
89
|
<%= javascript_include_tag "translations", skip_pipeline: true %>
|
67
90
|
```
|
68
91
|
|
69
|
-
**There are two ways to get `translations.js` (For Rails app without Asset
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
|
92
|
+
**There are two ways to get `translations.js` (For Rails app without Asset
|
93
|
+
Pipeline).**
|
94
|
+
|
95
|
+
1. This `translations.js` file can be automatically generated by the
|
96
|
+
`I18n::JS::Middleware`. Just add `config.middleware.use I18n::JS::Middleware`
|
97
|
+
to your `config/application.rb` file.
|
98
|
+
2. If you can't or prefer not to generate this file, you can move the middleware
|
99
|
+
line to your `config/environments/development.rb` file and run
|
100
|
+
`rake i18n:js:export` before deploying. This will export all translation
|
101
|
+
files, including the custom scopes you may have defined on
|
102
|
+
`config/i18n-js.yml`. If `I18n.available_locales` is set (e.g. in your Rails
|
103
|
+
`config/application.rb` file) then only the specified locales will be
|
104
|
+
exported. Current version of `i18n.js` will also be exported to avoid version
|
105
|
+
mismatching by downloading.
|
81
106
|
|
82
107
|
#### Export Configuration (For translations)
|
83
108
|
|
84
|
-
Exported translation files generated by `I18n::JS::Middleware` or
|
85
|
-
|
86
|
-
|
87
|
-
|
88
|
-
|
109
|
+
Exported translation files generated by `I18n::JS::Middleware` or
|
110
|
+
`rake i18n:js:export` can be customized with config file `config/i18n-js.yml`
|
111
|
+
(use `rails generate i18n:js:config` to create it). You can even get more files
|
112
|
+
generated to different folders and with different translations to best suit your
|
113
|
+
needs. The config file also affects developers using Asset Pipeline to require
|
114
|
+
translations. Except the option `file`, since all translations are required by
|
115
|
+
adding `//= require i18n/translations`.
|
89
116
|
|
90
117
|
Examples:
|
118
|
+
|
91
119
|
```yaml
|
92
120
|
translations:
|
93
|
-
- file:
|
94
|
-
|
95
|
-
- file:
|
96
|
-
|
121
|
+
- file: "public/javascripts/path-to-your-messages-file.js"
|
122
|
+
only: "*.date.formats"
|
123
|
+
- file: "public/javascripts/path-to-your-second-file.js"
|
124
|
+
only: ["*.activerecord", "*.admin.*.title"]
|
97
125
|
```
|
98
126
|
|
99
|
-
If `only` is omitted all the translations will be saved. Also, make sure you add
|
127
|
+
If `only` is omitted all the translations will be saved. Also, make sure you add
|
128
|
+
that initial `*`; it specifies that all languages will be exported. If you want
|
129
|
+
to export only one language, you can do something like this:
|
130
|
+
|
100
131
|
```yaml
|
101
132
|
translations:
|
102
|
-
- file:
|
103
|
-
|
104
|
-
- file:
|
105
|
-
|
133
|
+
- file: "public/javascripts/en.js"
|
134
|
+
only: "en.*"
|
135
|
+
- file: "public/javascripts/pt-BR.js"
|
136
|
+
only: "pt-BR.*"
|
106
137
|
```
|
107
138
|
|
108
|
-
Optionally, you can auto generate a translation file per available locale if you
|
139
|
+
Optionally, you can auto generate a translation file per available locale if you
|
140
|
+
specify the `%{locale}` placeholder.
|
141
|
+
|
109
142
|
```yaml
|
110
143
|
translations:
|
111
|
-
- file: "public/javascripts/i18n/%{locale}.js"
|
112
|
-
|
113
|
-
- file: "public/javascripts/frontend/i18n/%{locale}.js"
|
114
|
-
|
144
|
+
- file: "public/javascripts/i18n/%{locale}.js"
|
145
|
+
only: "*"
|
146
|
+
- file: "public/javascripts/frontend/i18n/%{locale}.js"
|
147
|
+
only: ["*.frontend", "*.users.*"]
|
115
148
|
```
|
116
149
|
|
117
150
|
You can also include ERB in your config file.
|
151
|
+
|
118
152
|
```yaml
|
119
153
|
translations:
|
120
154
|
<% Widgets.each do |widget| %>
|
@@ -123,39 +157,37 @@ translations:
|
|
123
157
|
<% end %>
|
124
158
|
```
|
125
159
|
|
126
|
-
You are able to exclude certain phrases or whole groups of phrases by
|
127
|
-
|
128
|
-
|
129
|
-
|
160
|
+
You are able to exclude certain phrases or whole groups of phrases by specifying
|
161
|
+
the YAML key(s) in the `except` configuration option. The outputted JS
|
162
|
+
translations file (exported or generated by the middleware) will omit any keys
|
163
|
+
listed in `except` configuration param:
|
130
164
|
|
131
165
|
```yaml
|
132
166
|
translations:
|
133
|
-
- except: [
|
167
|
+
- except: ["*.active_admin", "*.ransack", "*.activerecord.errors"]
|
134
168
|
```
|
135
169
|
|
136
|
-
|
137
170
|
#### Export Configuration (For other things)
|
138
171
|
|
139
|
-
- `I18n::JS.config_file_path`
|
140
|
-
|
141
|
-
Default: `config/i18n-js.yml`
|
142
|
-
Behaviour: Try to read the config file from that location
|
172
|
+
- `I18n::JS.config_file_path` Expected Type: `String` Default:
|
173
|
+
`config/i18n-js.yml` Behaviour: Try to read the config file from that location
|
143
174
|
|
144
|
-
- `I18n::JS.export_i18n_js_dir_path`
|
145
|
-
|
146
|
-
|
147
|
-
|
148
|
-
|
175
|
+
- `I18n::JS.export_i18n_js_dir_path` Expected Type: `String` Default:
|
176
|
+
`public/javascripts` Behaviour:
|
177
|
+
|
178
|
+
- Any `String`: considered as a relative path for a folder to `Rails.root` and
|
179
|
+
export `i18n.js` to that folder for `rake i18n:js:export`
|
149
180
|
- Any non-`String` (`nil`, `false`, `:none`, etc): Disable `i18n.js` exporting
|
150
181
|
|
151
|
-
- `I18n::JS.sort_translation_keys`
|
152
|
-
Expected Type: `Boolean`
|
153
|
-
Default: `true`
|
182
|
+
- `I18n::JS.sort_translation_keys` Expected Type: `Boolean` Default: `true`
|
154
183
|
Behaviour:
|
155
|
-
|
184
|
+
|
185
|
+
- Sets whether or not to deep sort all translation keys in order to generate
|
186
|
+
identical output for the same translations
|
156
187
|
- Set to true to ensure identical asset fingerprints for the asset pipeline
|
157
188
|
|
158
|
-
- You may also set `export_i18n_js` and `sort_translation_keys` in your config
|
189
|
+
- You may also set `export_i18n_js` and `sort_translation_keys` in your config
|
190
|
+
file, e.g.:
|
159
191
|
|
160
192
|
```yaml
|
161
193
|
export_i18n_js: false
|
@@ -168,21 +200,24 @@ translations:
|
|
168
200
|
- ...
|
169
201
|
```
|
170
202
|
|
171
|
-
To find more examples on how to use the configuration file please refer to the
|
203
|
+
To find more examples on how to use the configuration file please refer to the
|
204
|
+
tests.
|
172
205
|
|
173
206
|
#### Fallbacks
|
174
207
|
|
175
|
-
If you specify the `fallbacks` option, you will be able to fill missing
|
176
|
-
Default value is `true`.
|
208
|
+
If you specify the `fallbacks` option, you will be able to fill missing
|
209
|
+
translations with those inside fallback locale(s). Default value is `true`.
|
177
210
|
|
178
211
|
Examples:
|
212
|
+
|
179
213
|
```yaml
|
180
214
|
fallbacks: true
|
181
215
|
|
182
216
|
translations:
|
183
|
-
- file: "public/javascripts/i18n/%{locale}.js"
|
184
|
-
|
217
|
+
- file: "public/javascripts/i18n/%{locale}.js"
|
218
|
+
only: "*"
|
185
219
|
```
|
220
|
+
|
186
221
|
This will enable merging fallbacks into each file. (set to `false` to disable).
|
187
222
|
If you use `I18n` with fallbacks, the fallbacks defined there will be used.
|
188
223
|
Otherwise `I18n.default_locale` will be used.
|
@@ -191,9 +226,10 @@ Otherwise `I18n.default_locale` will be used.
|
|
191
226
|
fallbacks: :de
|
192
227
|
|
193
228
|
translations:
|
194
|
-
- file: "public/javascripts/i18n/%{locale}.js"
|
195
|
-
|
229
|
+
- file: "public/javascripts/i18n/%{locale}.js"
|
230
|
+
only: "*"
|
196
231
|
```
|
232
|
+
|
197
233
|
Here, the specified locale `:de` will be used as fallback for all locales.
|
198
234
|
|
199
235
|
```yaml
|
@@ -202,42 +238,60 @@ fallbacks:
|
|
202
238
|
de: "en"
|
203
239
|
|
204
240
|
translations:
|
205
|
-
- file: "public/javascripts/i18n/%{locale}.js"
|
206
|
-
|
241
|
+
- file: "public/javascripts/i18n/%{locale}.js"
|
242
|
+
only: "*"
|
207
243
|
```
|
208
|
-
|
244
|
+
|
245
|
+
Fallbacks defined will be used, if not defined (e.g. `:pl`) `I18n.fallbacks` or
|
246
|
+
`I18n.default_locale` will be used.
|
209
247
|
|
210
248
|
```yaml
|
211
249
|
fallbacks: :default_locale
|
212
250
|
|
213
251
|
translations:
|
214
|
-
- file: "public/javascripts/i18n/%{locale}.js"
|
215
|
-
|
252
|
+
- file: "public/javascripts/i18n/%{locale}.js"
|
253
|
+
only: "*"
|
216
254
|
```
|
217
|
-
|
255
|
+
|
256
|
+
Setting the option to `:default_locale` will enforce the fallback to use the
|
257
|
+
`I18n.default_locale`, ignoring `I18n.fallbacks`.
|
218
258
|
|
219
259
|
Examples:
|
260
|
+
|
220
261
|
```yaml
|
221
262
|
fallbacks: false
|
222
263
|
|
223
264
|
translations:
|
224
|
-
- file: "public/javascripts/i18n/%{locale}.js"
|
225
|
-
|
265
|
+
- file: "public/javascripts/i18n/%{locale}.js"
|
266
|
+
only: "*"
|
226
267
|
```
|
268
|
+
|
227
269
|
You must disable this feature by setting the option to `false`.
|
228
270
|
|
229
|
-
To find more examples on how to use the configuration file please refer to the
|
271
|
+
To find more examples on how to use the configuration file please refer to the
|
272
|
+
tests.
|
230
273
|
|
274
|
+
#### Available locales
|
275
|
+
|
276
|
+
By specifying option `js_available_locales` with a list of locales, this list
|
277
|
+
would be used instead of default `I18n.available_locales` to generate translations.
|
278
|
+
|
279
|
+
Example:
|
280
|
+
|
281
|
+
```yaml
|
282
|
+
js_available_locales: ["de", "en"]
|
283
|
+
```
|
231
284
|
|
232
285
|
#### Namespace
|
233
286
|
|
234
|
-
Setting the `namespace` option will change the namespace of the output
|
235
|
-
This can be useful in
|
287
|
+
Setting the `namespace` option will change the namespace of the output
|
288
|
+
Javascript file to something other than `I18n`. This can be useful in
|
289
|
+
no-conflict scenarios. Example:
|
236
290
|
|
237
291
|
```yaml
|
238
292
|
translations:
|
239
|
-
- file: "public/javascripts/i18n/translations.js"
|
240
|
-
|
293
|
+
- file: "public/javascripts/i18n/translations.js"
|
294
|
+
namespace: "MyNamespace"
|
241
295
|
```
|
242
296
|
|
243
297
|
will create:
|
@@ -247,19 +301,20 @@ MyNamespace.translations || (MyNamespace.translations = {});
|
|
247
301
|
MyNamespace.translations["en"] = { ... }
|
248
302
|
```
|
249
303
|
|
250
|
-
|
251
304
|
### Adding prefix & suffix to the translations file(s)
|
252
305
|
|
253
|
-
Setting the `prefix: "import I18n from 'i18n-js';\n"` option will add the line
|
254
|
-
|
255
|
-
|
306
|
+
Setting the `prefix: "import I18n from 'i18n-js';\n"` option will add the line
|
307
|
+
at the beginning of the resultant translation file. This can be useful to use
|
308
|
+
this gem with the [i18n-js](https://www.npmjs.com/package/i18n-js) npm package,
|
309
|
+
which is quite useful to use it with webpack. The user should provide the
|
310
|
+
semi-colon and the newline character if needed.
|
256
311
|
|
257
312
|
For example:
|
258
313
|
|
259
314
|
```yaml
|
260
315
|
translations:
|
261
|
-
- file: "public/javascripts/i18n/translations.js"
|
262
|
-
|
316
|
+
- file: "public/javascripts/i18n/translations.js"
|
317
|
+
prefix: "import I18n from 'i18n-js';\n"
|
263
318
|
```
|
264
319
|
|
265
320
|
will create:
|
@@ -269,47 +324,44 @@ import I18n from 'i18n-js';
|
|
269
324
|
I18n.translations || (I18n.translations = {});
|
270
325
|
```
|
271
326
|
|
272
|
-
|
273
327
|
`suffix` option is added in https://github.com/fnando/i18n-js/pull/561.
|
274
328
|
It's similar to `prefix` so won't explain it in details.
|
275
329
|
|
276
|
-
|
277
330
|
#### Pretty Print
|
278
331
|
|
279
|
-
Set the `pretty_print` option if you would like whitespace and indentation in
|
332
|
+
Set the `pretty_print` option if you would like whitespace and indentation in
|
333
|
+
your output file (default: false)
|
280
334
|
|
281
335
|
```yaml
|
282
336
|
translations:
|
283
|
-
- file: "public/javascripts/i18n/translations.js"
|
284
|
-
|
337
|
+
- file: "public/javascripts/i18n/translations.js"
|
338
|
+
pretty_print: true
|
285
339
|
```
|
286
340
|
|
287
|
-
|
288
341
|
#### Javascript Deep Merge (:js_extend option)
|
289
342
|
|
290
|
-
By default, the output file Javascript will call the `I18n.extend` method to
|
291
|
-
files are deep-merged with any locale data
|
292
|
-
|
343
|
+
By default, the output file Javascript will call the `I18n.extend` method to
|
344
|
+
ensure that newly loaded locale files are deep-merged with any locale data
|
345
|
+
already in memory. To disable this either globally or per-file, set the
|
346
|
+
`js_extend` option to false
|
293
347
|
|
294
348
|
```yaml
|
295
|
-
js_extend: false
|
349
|
+
js_extend: false # this will disable Javascript I18n.extend globally
|
296
350
|
translations:
|
297
|
-
- file: "public/javascripts/i18n/translations.js"
|
298
|
-
|
351
|
+
- file: "public/javascripts/i18n/translations.js"
|
352
|
+
js_extend: false # this will disable Javascript I18n.extend for this file
|
299
353
|
```
|
300
354
|
|
301
|
-
|
302
355
|
#### Vanilla JavaScript
|
303
356
|
|
304
|
-
Just add the `i18n.js` file to your page. You'll have to build the translations
|
305
|
-
by hand or using your favorite programming language. More info below.
|
306
|
-
|
357
|
+
Just add the `i18n.js` file to your page. You'll have to build the translations
|
358
|
+
object by hand or using your favorite programming language. More info below.
|
307
359
|
|
308
360
|
#### Via NPM with webpack and CommonJS
|
309
361
|
|
362
|
+
Add the following line to your package.json dependencies where version is the
|
363
|
+
version you want:
|
310
364
|
|
311
|
-
Add the following line to your package.json dependencies
|
312
|
-
where version is the version you want
|
313
365
|
```javascript
|
314
366
|
"i18n-js": "{version_constraint}"
|
315
367
|
|
@@ -317,17 +369,21 @@ where version is the version you want
|
|
317
369
|
// npm install requires it to be the gzipped tarball, see [npm install](https://www.npmjs.org/doc/cli/npm-install.html)
|
318
370
|
"i18n-js": "https://github.com/fnando/i18n-js/archive/{tag_name_or_branch_name_or_commit_sha}.tar.gz"
|
319
371
|
```
|
372
|
+
|
320
373
|
Run npm install then use via
|
374
|
+
|
321
375
|
```javascript
|
322
376
|
var i18n = require("i18n-js");
|
323
377
|
```
|
324
378
|
|
325
|
-
|
326
379
|
### Setting up
|
327
380
|
|
328
|
-
You **don't** need to set up a thing. The default settings will work just okay.
|
381
|
+
You **don't** need to set up a thing. The default settings will work just okay.
|
382
|
+
But if you want to split translations into several files or specify contexts,
|
383
|
+
you can follow the rest of this setting up section.
|
329
384
|
|
330
385
|
Set your locale is easy as
|
386
|
+
|
331
387
|
```javascript
|
332
388
|
I18n.defaultLocale = "pt-BR";
|
333
389
|
I18n.locale = "pt-BR";
|
@@ -335,9 +391,11 @@ I18n.currentLocale();
|
|
335
391
|
// pt-BR
|
336
392
|
```
|
337
393
|
|
338
|
-
**NOTE:** You can now apply your configuration **before I18n** is loaded like
|
394
|
+
**NOTE:** You can now apply your configuration **before I18n** is loaded like
|
395
|
+
this:
|
396
|
+
|
339
397
|
```javascript
|
340
|
-
I18n = {} // You must define this object in top namespace, which should be `window`
|
398
|
+
I18n = {}; // You must define this object in top namespace, which should be `window`
|
341
399
|
I18n.defaultLocale = "pt-BR";
|
342
400
|
I18n.locale = "pt-BR";
|
343
401
|
|
@@ -347,7 +405,8 @@ I18n.currentLocale();
|
|
347
405
|
// pt-BR
|
348
406
|
```
|
349
407
|
|
350
|
-
In practice, you'll have something like the following in your
|
408
|
+
In practice, you'll have something like the following in your
|
409
|
+
`application.html.erb`:
|
351
410
|
|
352
411
|
```erb
|
353
412
|
<script type="text/javascript">
|
@@ -361,7 +420,7 @@ You can use translate your messages:
|
|
361
420
|
```javascript
|
362
421
|
I18n.t("some.scoped.translation");
|
363
422
|
// or translate with explicit setting of locale
|
364
|
-
I18n.t("some.scoped.translation", {locale: "fr"});
|
423
|
+
I18n.t("some.scoped.translation", { locale: "fr" });
|
365
424
|
```
|
366
425
|
|
367
426
|
You can also interpolate values:
|
@@ -369,36 +428,38 @@ You can also interpolate values:
|
|
369
428
|
```javascript
|
370
429
|
// You need the `translations` object setup first
|
371
430
|
I18n.translations["en"] = {
|
372
|
-
greeting: "Hello %{name}"
|
373
|
-
}
|
431
|
+
greeting: "Hello %{name}",
|
432
|
+
};
|
374
433
|
|
375
|
-
I18n.t("greeting", {name: "John Doe"});
|
434
|
+
I18n.t("greeting", { name: "John Doe" });
|
376
435
|
```
|
436
|
+
|
377
437
|
You can set default values for missing scopes:
|
438
|
+
|
378
439
|
```javascript
|
379
440
|
// simple translation
|
380
|
-
I18n.t("some.missing.scope", {defaultValue: "A default message"});
|
441
|
+
I18n.t("some.missing.scope", { defaultValue: "A default message" });
|
381
442
|
|
382
443
|
// with interpolation
|
383
|
-
I18n.t("noun", {defaultValue: "I'm a {{noun}}", noun: "Mac"});
|
444
|
+
I18n.t("noun", { defaultValue: "I'm a {{noun}}", noun: "Mac" });
|
384
445
|
```
|
385
446
|
|
386
447
|
You can also provide a list of default fallbacks for missing scopes:
|
387
448
|
|
388
449
|
```javascript
|
389
450
|
// As a scope
|
390
|
-
I18n.t("some.missing.scope", {defaults: [{scope: "some.existing.scope"}]});
|
451
|
+
I18n.t("some.missing.scope", { defaults: [{ scope: "some.existing.scope" }] });
|
391
452
|
|
392
453
|
// As a simple translation
|
393
|
-
I18n.t("some.missing.scope", {defaults: [{message: "Some message"}]});
|
454
|
+
I18n.t("some.missing.scope", { defaults: [{ message: "Some message" }] });
|
394
455
|
```
|
395
456
|
|
396
|
-
Default values must be provided as an array of
|
397
|
-
|
398
|
-
|
457
|
+
Default values must be provided as an array of hashes where the key is the type
|
458
|
+
of translation desired, a `scope` or a `message`. The translation returned will
|
459
|
+
be either the first scope recognized, or the first message defined.
|
399
460
|
|
400
|
-
The translation will fallback to the `defaultValue` translation if no scope
|
401
|
-
|
461
|
+
The translation will fallback to the `defaultValue` translation if no scope in
|
462
|
+
`defaults` matches and if no default of type `message` is found.
|
402
463
|
|
403
464
|
Translation fallback can be enabled by enabling the `I18n.fallbacks` option:
|
404
465
|
|
@@ -408,9 +469,9 @@ Translation fallback can be enabled by enabling the `I18n.fallbacks` option:
|
|
408
469
|
</script>
|
409
470
|
```
|
410
471
|
|
411
|
-
By default missing translations will first be looked for in less
|
412
|
-
|
413
|
-
|
472
|
+
By default missing translations will first be looked for in less specific
|
473
|
+
versions of the requested locale and if that fails by taking them from your
|
474
|
+
`I18n.defaultLocale`.
|
414
475
|
|
415
476
|
```javascript
|
416
477
|
// if I18n.defaultLocale = "en" and translation doesn't exist
|
@@ -419,24 +480,28 @@ them from your `I18n.defaultLocale`.
|
|
419
480
|
I18n.t("some.missing.scope");
|
420
481
|
```
|
421
482
|
|
422
|
-
Custom fallback rules can also be specified for a particular language. There
|
423
|
-
|
483
|
+
Custom fallback rules can also be specified for a particular language. There are
|
484
|
+
three different ways of doing it so:
|
424
485
|
|
425
486
|
```javascript
|
426
487
|
I18n.locales.no = ["nb", "en"];
|
427
488
|
I18n.locales.no = "nb";
|
428
|
-
I18n.locales.no = function(locale){
|
489
|
+
I18n.locales.no = function (locale) {
|
490
|
+
return ["nb"];
|
491
|
+
};
|
429
492
|
```
|
430
493
|
|
494
|
+
### Translation Missing Behaviour Control
|
495
|
+
|
431
496
|
By default a missing translation will be displayed as
|
432
497
|
|
433
498
|
[missing "name of scope" translation]
|
434
499
|
|
435
|
-
While you are developing or if you do not want to provide a translation
|
436
|
-
|
500
|
+
While you are developing or if you do not want to provide a translation in the
|
501
|
+
default language you can set
|
437
502
|
|
438
503
|
```javascript
|
439
|
-
I18n.missingBehaviour=
|
504
|
+
I18n.missingBehaviour = "guess";
|
440
505
|
```
|
441
506
|
|
442
507
|
this will take the last section of your scope and guess the intended value.
|
@@ -446,28 +511,49 @@ Camel case becomes lower cased text and underscores are replaced with space
|
|
446
511
|
|
447
512
|
becomes "what is your favorite Christmas present"
|
448
513
|
|
449
|
-
|
450
|
-
|
514
|
+
#### Option `missingTranslationPrefix`
|
515
|
+
|
516
|
+
In order to still detect untranslated strings, you can set
|
517
|
+
`I18n.missingTranslationPrefix` to something like:
|
518
|
+
|
451
519
|
```javascript
|
452
|
-
I18n.missingTranslationPrefix =
|
520
|
+
I18n.missingTranslationPrefix = "EE: ";
|
453
521
|
```
|
454
522
|
|
455
523
|
And result will be:
|
524
|
+
|
456
525
|
```javascript
|
457
|
-
"EE: what is your favorite Christmas present"
|
526
|
+
"EE: what is your favorite Christmas present";
|
527
|
+
|
458
528
|
```
|
459
529
|
|
460
530
|
This will help you doing automated tests against your localisation assets.
|
461
531
|
|
462
|
-
|
532
|
+
#### Customize return when translation entry missing
|
533
|
+
|
534
|
+
Some people prefer returning `null`/`undefined` for missing translation:
|
535
|
+
|
463
536
|
```javascript
|
464
|
-
I18n.missingTranslation = function () {
|
537
|
+
I18n.missingTranslation = function (scope, options) {
|
538
|
+
return undefined;
|
539
|
+
};
|
465
540
|
```
|
466
541
|
|
542
|
+
### Option `defaultSeparator` (global) / `separator` (local)
|
543
|
+
|
544
|
+
Default separator of translation key is `.` (dot)
|
545
|
+
Meaning `I18n.t("scope.entry")` would search for translation entry `I18n.translations[locale].scope.entry`
|
546
|
+
Using a different separator can be done either globally or locally.
|
547
|
+
|
548
|
+
Globally: `I18n.defaultSeparator = newSeparator`
|
549
|
+
Locally: `I18n.t("full_sentences|Server Busy. Please retry later", {separator: '|'})`
|
550
|
+
|
551
|
+
### Pluralization
|
552
|
+
|
467
553
|
Pluralization is possible as well and by default provides English rules:
|
468
554
|
|
469
555
|
```javascript
|
470
|
-
I18n.t("inbox.counting", {count: 10}); // You have 10 messages
|
556
|
+
I18n.t("inbox.counting", { count: 10 }); // You have 10 messages
|
471
557
|
```
|
472
558
|
|
473
559
|
The sample above expects the following translation:
|
@@ -483,21 +569,34 @@ en:
|
|
483
569
|
|
484
570
|
**NOTE:** Rails I18n recognizes the `zero` option.
|
485
571
|
|
486
|
-
If you need special rules just define them for your language, for example
|
572
|
+
If you need special rules just define them for your language, for example
|
573
|
+
Russian, just add a new pluralizer:
|
487
574
|
|
488
575
|
```javascript
|
489
576
|
I18n.pluralization["ru"] = function (count) {
|
490
|
-
var key =
|
577
|
+
var key =
|
578
|
+
count % 10 == 1 && count % 100 != 11
|
579
|
+
? "one"
|
580
|
+
: [2, 3, 4].indexOf(count % 10) >= 0 &&
|
581
|
+
[12, 13, 14].indexOf(count % 100) < 0
|
582
|
+
? "few"
|
583
|
+
: count % 10 == 0 ||
|
584
|
+
[5, 6, 7, 8, 9].indexOf(count % 10) >= 0 ||
|
585
|
+
[11, 12, 13, 14].indexOf(count % 100) >= 0
|
586
|
+
? "many"
|
587
|
+
: "other";
|
491
588
|
return [key];
|
492
589
|
};
|
493
590
|
```
|
494
591
|
|
495
|
-
You can find all rules on
|
592
|
+
You can find all rules on
|
593
|
+
<https://unicode-org.github.io/cldr-staging/charts/37/supplemental/language_plural_rules.html>.
|
496
594
|
|
497
|
-
If you're using the same scope over and over again, you may use the `scope`
|
595
|
+
If you're using the same scope over and over again, you may use the `scope`
|
596
|
+
option.
|
498
597
|
|
499
598
|
```javascript
|
500
|
-
var options = {scope: "activerecord.attributes.user"};
|
599
|
+
var options = { scope: "activerecord.attributes.user" };
|
501
600
|
|
502
601
|
I18n.t("name", options);
|
503
602
|
I18n.t("email", options);
|
@@ -526,14 +625,13 @@ I18n.l("percentage", 123.45);
|
|
526
625
|
// 123.450%
|
527
626
|
```
|
528
627
|
|
529
|
-
To have more control over number formatting, you can use the
|
530
|
-
`I18n.
|
531
|
-
functions.
|
628
|
+
To have more control over number formatting, you can use the `I18n.toNumber`,
|
629
|
+
`I18n.toPercentage`, `I18n.toCurrency` and `I18n.toHumanSize` functions.
|
532
630
|
|
533
631
|
```javascript
|
534
|
-
I18n.toNumber(1000);
|
535
|
-
I18n.toCurrency(1000);
|
536
|
-
I18n.toPercentage(100);
|
632
|
+
I18n.toNumber(1000); // 1,000.000
|
633
|
+
I18n.toCurrency(1000); // $1,000.00
|
634
|
+
I18n.toPercentage(100); // 100.000%
|
537
635
|
```
|
538
636
|
|
539
637
|
The `toNumber` and `toPercentage` functions accept the following options:
|
@@ -546,9 +644,9 @@ The `toNumber` and `toPercentage` functions accept the following options:
|
|
546
644
|
See some number formatting examples:
|
547
645
|
|
548
646
|
```javascript
|
549
|
-
I18n.toNumber(1000, {precision: 0});
|
550
|
-
I18n.toNumber(1000, {delimiter: ".", separator: ","}); // 1.000,000
|
551
|
-
I18n.toNumber(1000, {delimiter: ".", precision: 0});
|
647
|
+
I18n.toNumber(1000, { precision: 0 }); // 1,000
|
648
|
+
I18n.toNumber(1000, { delimiter: ".", separator: "," }); // 1.000,000
|
649
|
+
I18n.toNumber(1000, { delimiter: ".", precision: 0 }); // 1.000
|
552
650
|
```
|
553
651
|
|
554
652
|
The `toCurrency` function accepts the following options:
|
@@ -564,7 +662,7 @@ The `toCurrency` function accepts the following options:
|
|
564
662
|
You can provide only the options you want to override:
|
565
663
|
|
566
664
|
```javascript
|
567
|
-
I18n.toCurrency(1000, {precision: 0}); // $1,000
|
665
|
+
I18n.toCurrency(1000, { precision: 0 }); // $1,000
|
568
666
|
```
|
569
667
|
|
570
668
|
The `toHumanSize` function accepts the following options:
|
@@ -574,6 +672,7 @@ The `toHumanSize` function accepts the following options:
|
|
574
672
|
- `delimiter`: defaults to `""`
|
575
673
|
- `strip_insignificant_zeros`: defaults to `false`
|
576
674
|
- `format`: defaults to `%n%u`
|
675
|
+
- `scope`: defaults to `""`
|
577
676
|
|
578
677
|
<!---->
|
579
678
|
|
@@ -582,18 +681,17 @@ I18n.toHumanSize(1234); // 1KB
|
|
582
681
|
I18n.toHumanSize(1234 * 1024); // 1MB
|
583
682
|
```
|
584
683
|
|
585
|
-
|
586
684
|
#### Date formatting
|
587
685
|
|
588
686
|
```javascript
|
589
687
|
// accepted formats
|
590
|
-
I18n.l("date.formats.short", "2009-09-18");
|
591
|
-
I18n.l("time.formats.short", "2009-09-18 23:12:43");
|
592
|
-
I18n.l("time.formats.short", "2009-11-09T18:10:34");
|
688
|
+
I18n.l("date.formats.short", "2009-09-18"); // yyyy-mm-dd
|
689
|
+
I18n.l("time.formats.short", "2009-09-18 23:12:43"); // yyyy-mm-dd hh:mm:ss
|
690
|
+
I18n.l("time.formats.short", "2009-11-09T18:10:34"); // JSON format with local Timezone (part of ISO-8601)
|
593
691
|
I18n.l("time.formats.short", "2009-11-09T18:10:34Z"); // JSON format in UTC (part of ISO-8601)
|
594
|
-
I18n.l("date.formats.short", 1251862029000);
|
595
|
-
I18n.l("date.formats.short", "09/18/2009");
|
596
|
-
I18n.l("date.formats.short",
|
692
|
+
I18n.l("date.formats.short", 1251862029000); // Epoch time
|
693
|
+
I18n.l("date.formats.short", "09/18/2009"); // mm/dd/yyyy
|
694
|
+
I18n.l("date.formats.short", new Date()); // Date object
|
597
695
|
```
|
598
696
|
|
599
697
|
You can also add placeholders to the date format:
|
@@ -602,14 +700,16 @@ You can also add placeholders to the date format:
|
|
602
700
|
I18n.translations["en"] = {
|
603
701
|
date: {
|
604
702
|
formats: {
|
605
|
-
ordinal_day: "%B %{day}"
|
606
|
-
}
|
607
|
-
}
|
608
|
-
}
|
609
|
-
|
703
|
+
ordinal_day: "%B %{day}",
|
704
|
+
},
|
705
|
+
},
|
706
|
+
};
|
707
|
+
|
708
|
+
I18n.l("date.formats.ordinal_day", "2009-09-18", { day: "18th" }); // Sep 18th
|
610
709
|
```
|
611
710
|
|
612
|
-
If you prefer, you can use the `I18n.toTime` and `I18n.strftime` functions to
|
711
|
+
If you prefer, you can use the `I18n.toTime` and `I18n.strftime` functions to
|
712
|
+
format dates.
|
613
713
|
|
614
714
|
```javascript
|
615
715
|
var date = new Date();
|
@@ -648,8 +748,10 @@ The accepted formats for `I18n.strftime` are:
|
|
648
748
|
Check out `spec/*.spec.js` files for more examples!
|
649
749
|
|
650
750
|
#### Using pluralization and number formatting together
|
651
|
-
|
652
|
-
|
751
|
+
|
752
|
+
Sometimes you might want to display translation with formatted number, like
|
753
|
+
adding thousand delimiters to displayed number You can do this:
|
754
|
+
|
653
755
|
```json
|
654
756
|
{
|
655
757
|
"en": {
|
@@ -661,153 +763,176 @@ You can do this:
|
|
661
763
|
}
|
662
764
|
}
|
663
765
|
```
|
766
|
+
|
664
767
|
```js
|
665
768
|
var point_in_number = 1000;
|
666
|
-
I18n.t(
|
769
|
+
I18n.t("point", {
|
770
|
+
count: point_in_number,
|
771
|
+
formatted_number: I18n.toNumber(point_in_number),
|
772
|
+
});
|
667
773
|
```
|
668
|
-
Output should be `1,000 points`
|
669
774
|
|
775
|
+
Output should be `1,000 points`
|
670
776
|
|
671
777
|
## Using multiple exported translation files on a page.
|
672
|
-
|
778
|
+
|
779
|
+
This method is useful for very large apps where a single contained
|
780
|
+
translations.js file is not desirable. Examples would be a global translations
|
781
|
+
file and a more specific route translation file.
|
673
782
|
|
674
783
|
### Rails without asset pipeline
|
675
|
-
|
676
|
-
|
677
|
-
|
678
|
-
|
679
|
-
|
680
|
-
|
681
|
-
|
682
|
-
|
683
|
-
|
684
|
-
|
685
|
-
|
686
|
-
|
687
|
-
|
688
|
-
|
689
|
-
|
690
|
-
|
691
|
-
|
692
|
-
|
693
|
-
|
694
|
-
|
695
|
-
|
696
|
-
|
697
|
-
|
698
|
-
|
699
|
-
|
700
|
-
|
701
|
-
|
702
|
-
|
703
|
-
|
704
|
-
|
705
|
-
|
706
|
-
|
707
|
-
|
708
|
-
|
709
|
-
|
710
|
-
|
711
|
-
|
712
|
-
|
713
|
-
|
714
|
-
|
715
|
-
|
716
|
-
|
784
|
+
|
785
|
+
1. Setup your `config/i18n-js.yml` to have multiple files and try to minimize
|
786
|
+
any overlap.
|
787
|
+
|
788
|
+
```yaml
|
789
|
+
sort_translation_keys: true
|
790
|
+
fallbacks: false
|
791
|
+
|
792
|
+
translations:
|
793
|
+
+ file: "app/assets/javascript/nls/welcome.js"
|
794
|
+
only:
|
795
|
+
+ '*.welcome.*'
|
796
|
+
|
797
|
+
+ file: "app/assets/javascript/nls/albums.js"
|
798
|
+
only:
|
799
|
+
+ '*.albums.*'
|
800
|
+
|
801
|
+
+ file: "app/assets/javascript/nls/global.js"
|
802
|
+
only:
|
803
|
+
+ '*'
|
804
|
+
# Exempt any routes specific translations from being
|
805
|
+
# included in the global translation file
|
806
|
+
except:
|
807
|
+
+ '*.welcome.*'
|
808
|
+
+ '*.albums.*'
|
809
|
+
```
|
810
|
+
|
811
|
+
When `rake i18n:js:export` is executed it will create 3 translations files that
|
812
|
+
can be loaded via the `javascript_include_tag`
|
813
|
+
|
814
|
+
2. Add the `javascript_include_tag` to your layout and to any route specific
|
815
|
+
files that will require it.
|
816
|
+
|
817
|
+
```ruby
|
818
|
+
# views/layouts/application.html.erb
|
819
|
+
<%= javascript_include_tag(
|
820
|
+
"i18n"
|
821
|
+
"nls/global"
|
822
|
+
) %>
|
823
|
+
```
|
824
|
+
|
825
|
+
and in the route specific
|
826
|
+
|
827
|
+
```ruby
|
828
|
+
# views/welcome/index.html.erb
|
829
|
+
<%= javascript_include_tag(
|
830
|
+
"nls/welcome"
|
831
|
+
) %>
|
832
|
+
```
|
717
833
|
|
718
834
|
3. Make sure that you add these files to your `config/application.rb`
|
719
835
|
|
720
|
-
|
721
|
-
|
722
|
-
|
723
|
-
|
724
|
-
|
725
|
-
|
836
|
+
```ruby
|
837
|
+
config.assets.precompile += %w(
|
838
|
+
i18n
|
839
|
+
nls/*
|
840
|
+
)
|
841
|
+
```
|
726
842
|
|
727
843
|
### Using require.js / r.js
|
728
844
|
|
729
845
|
To use this with require.js we are only going to change a few things from above.
|
730
846
|
|
731
|
-
1. In your `config/i18n-js.yml` we need to add a better location for the i18n to
|
732
|
-
|
733
|
-
|
734
|
-
|
735
|
-
|
736
|
-
|
737
|
-
|
738
|
-
|
739
|
-
|
740
|
-
|
741
|
-
|
742
|
-
|
743
|
-
|
744
|
-
|
745
|
-
|
746
|
-
|
747
|
-
|
748
|
-
|
749
|
-
|
750
|
-
|
751
|
-
|
752
|
-
|
753
|
-
|
754
|
-
|
755
|
-
|
756
|
-
|
757
|
-
|
758
|
-
|
759
|
-
|
760
|
-
|
761
|
-
|
762
|
-
|
763
|
-
|
764
|
-
|
765
|
-
|
766
|
-
|
767
|
-
|
768
|
-
|
769
|
-
|
770
|
-
|
771
|
-
|
772
|
-
|
773
|
-
|
774
|
-
|
775
|
-
|
776
|
-
|
777
|
-
|
778
|
-
|
779
|
-
|
780
|
-
|
781
|
-
|
782
|
-
|
783
|
-
|
784
|
-
|
785
|
-
|
786
|
-
|
787
|
-
|
788
|
-
|
847
|
+
1. In your `config/i18n-js.yml` we need to add a better location for the i18n to
|
848
|
+
be exported to. You want to use this location so that it can be properly
|
849
|
+
precompiled by r.js.
|
850
|
+
|
851
|
+
```yaml
|
852
|
+
export_i18n_js: "app/assets/javascript/nls"
|
853
|
+
```
|
854
|
+
|
855
|
+
2. In your `config/require.yml` we need to add a map, shim all the translations,
|
856
|
+
and include them into the appropriate modules
|
857
|
+
|
858
|
+
```yaml
|
859
|
+
# In your maps add (if you do not have this you will need to add it)
|
860
|
+
map:
|
861
|
+
'*':
|
862
|
+
i18n: 'nls/i18n'
|
863
|
+
|
864
|
+
# In your shims
|
865
|
+
shims:
|
866
|
+
nls/welcome:
|
867
|
+
deps:
|
868
|
+
+ i18n
|
869
|
+
|
870
|
+
nls/global:
|
871
|
+
deps:
|
872
|
+
+ i18n
|
873
|
+
|
874
|
+
# Finally in your modules
|
875
|
+
modules:
|
876
|
+
+ name: 'application'
|
877
|
+
include:
|
878
|
+
+ i18n
|
879
|
+
+ 'nls/global'
|
880
|
+
|
881
|
+
+ name: 'welcome'
|
882
|
+
exclude:
|
883
|
+
+ application
|
884
|
+
include:
|
885
|
+
+ 'nls/welcome'
|
886
|
+
```
|
887
|
+
|
888
|
+
3. When `rake assets:precompile` is executed it will optimize the translations
|
889
|
+
into the correct modules so they are loaded with their assigned module, and
|
890
|
+
loading them with requirejs is as simple as requiring any other shim.
|
891
|
+
|
892
|
+
```javascript
|
893
|
+
define(["welcome/other_asset", "nls/welcome"], function (otherAsset) {
|
894
|
+
// ...
|
895
|
+
});
|
896
|
+
```
|
897
|
+
|
898
|
+
4. (optional) As an additional configuration we can make a task to be run before
|
899
|
+
the requirejs optimizer. This will allow any automated scripts that run the
|
900
|
+
requirejs optimizer to export the strings before we run r.js.
|
901
|
+
|
902
|
+
```rake
|
903
|
+
# lib/tasks/i18n.rake
|
904
|
+
Rake::Task[:'i18n:js:export'].prerequisites.clear
|
905
|
+
task :'i18n:js:export' => :'i18n:js:before_export'
|
906
|
+
task :'requirejs:precompile:external' => :'i18n:js:export'
|
907
|
+
|
908
|
+
namespace :i18n do
|
909
|
+
namespace :js do
|
910
|
+
task :before_export => :'assets:environment' do
|
911
|
+
I18n.load_path += Dir[Rails.root.join('config', 'locales', '*.{yml,rb}')]
|
912
|
+
I18n.backend.load_translations
|
789
913
|
end
|
790
914
|
end
|
791
|
-
|
915
|
+
end
|
916
|
+
```
|
792
917
|
|
793
918
|
## Using I18n.js with other languages (Python, PHP, ...)
|
794
919
|
|
795
|
-
The JavaScript library is language agnostic; so you can use it with PHP, Python,
|
796
|
-
The only requirement is that you need to set the
|
920
|
+
The JavaScript library is language agnostic; so you can use it with PHP, Python,
|
921
|
+
[your favorite language here]. The only requirement is that you need to set the
|
922
|
+
`translations` attribute like following:
|
797
923
|
|
798
924
|
```javascript
|
799
925
|
I18n.translations = {};
|
800
926
|
|
801
927
|
I18n.translations["en"] = {
|
802
|
-
message: "Some special message for you"
|
803
|
-
}
|
928
|
+
message: "Some special message for you",
|
929
|
+
};
|
804
930
|
|
805
931
|
I18n.translations["pt-BR"] = {
|
806
|
-
message: "Uma mensagem especial para você"
|
807
|
-
}
|
932
|
+
message: "Uma mensagem especial para você",
|
933
|
+
};
|
808
934
|
```
|
809
935
|
|
810
|
-
|
811
936
|
## Known Issues
|
812
937
|
|
813
938
|
### Missing translations in precompiled file(s) after adding any new locale file
|
@@ -815,66 +940,80 @@ I18n.translations["pt-BR"] = {
|
|
815
940
|
Due to the design of `sprockets`:
|
816
941
|
|
817
942
|
- `depend_on` only takes file paths, not directory paths
|
818
|
-
- registered `preprocessors` are only run when the fingerprint of any asset
|
943
|
+
- registered `preprocessors` are only run when the fingerprint of any asset
|
944
|
+
file, including `.erb` files, is changed
|
945
|
+
|
946
|
+
This means that new locale files will not be detected, and so they will not
|
947
|
+
trigger a i18n-js refresh. There are a few approaches to work around this:
|
819
948
|
|
820
|
-
|
949
|
+
1. You can force i18n-js to update its translations by completely clearing the
|
950
|
+
assets cache. Use one of the following:
|
821
951
|
|
822
|
-
1. You can force i18n-js to update its translations by completely clearing the assets cache. Use one of the following:
|
823
|
-
|
824
952
|
```bash
|
825
953
|
$ rake assets:clobber
|
826
954
|
# Or, with older versions of Rails:
|
827
955
|
$ rake tmp:cache:clear
|
828
956
|
```
|
829
957
|
|
830
|
-
These commands will remove
|
958
|
+
These commands will remove _all_ fingerprinted assets, and you will have to
|
959
|
+
recompile them with
|
831
960
|
|
832
961
|
```bash
|
833
962
|
$ rake assets:precompile
|
834
963
|
```
|
835
964
|
|
836
|
-
or similar commands.
|
965
|
+
or similar commands. If you are precompiling assets on the target machine(s),
|
966
|
+
cached pages may be broken by this, so they will need to be refreshed.
|
837
967
|
|
838
968
|
2. You can change something in a different locale file.
|
839
969
|
|
840
970
|
3. Finally, you can change `config.assets.version`.
|
841
971
|
|
842
|
-
**Note:** See issue [#213](https://github.com/fnando/i18n-js/issues/213) for
|
972
|
+
**Note:** See issue [#213](https://github.com/fnando/i18n-js/issues/213) for
|
973
|
+
more details and discussion of this issue.
|
843
974
|
|
844
975
|
### Translations in JS are not updated when Sprockets not loaded before this gem
|
845
976
|
|
846
|
-
The "rails engine" declaration will try to detect existence of "sprockets"
|
847
|
-
If sprockets is loaded after this gem, the
|
848
|
-
making JS translations file cache to depend on content of
|
849
|
-
So ensure sprockets is loaded before this gem
|
977
|
+
The "rails engine" declaration will try to detect existence of "sprockets"
|
978
|
+
before adding the initailizer If sprockets is loaded after this gem, the
|
979
|
+
preprocessor for making JS translations file cache to depend on content of
|
980
|
+
locale files will not be hooked. So ensure sprockets is loaded before this gem
|
981
|
+
by moving the entry of sprockets in the Gemfile or adding "require" statements
|
982
|
+
for sprockets somewhere.
|
850
983
|
|
851
|
-
**Note:** See issue [#404](https://github.com/fnando/i18n-js/issues/404) for
|
984
|
+
**Note:** See issue [#404](https://github.com/fnando/i18n-js/issues/404) for
|
985
|
+
more details and discussion of this issue.
|
852
986
|
|
853
987
|
### JS `I18n.toCurrency` & `I18n.toNumber` cannot handle large integers
|
854
988
|
|
855
|
-
The above methods use `toFixed` and it only supports 53 bit integers.
|
856
|
-
|
989
|
+
The above methods use `toFixed` and it only supports 53 bit integers. Ref:
|
990
|
+
http://2ality.com/2012/07/large-integers.html
|
857
991
|
|
858
|
-
Feel free to find & discuss possible solution(s) at issue
|
992
|
+
Feel free to find & discuss possible solution(s) at issue
|
993
|
+
[#511](https://github.com/fnando/i18n-js/issues/511)
|
859
994
|
|
860
995
|
### Only works with `Simple` backend
|
861
996
|
|
862
|
-
If you set `I18n.backend` to something other than the default `Simple` backend,
|
997
|
+
If you set `I18n.backend` to something other than the default `Simple` backend,
|
998
|
+
you will likely get an exception like this:
|
863
999
|
|
864
1000
|
```
|
865
1001
|
Undefined method 'initialized?' for <your backend class>
|
866
1002
|
```
|
867
1003
|
|
868
|
-
For now, i18n-js is only compatible with the `Simple` backend.
|
869
|
-
|
1004
|
+
For now, i18n-js is only compatible with the `Simple` backend. If you need a
|
1005
|
+
more sophisticated backend for your rails application, like
|
1006
|
+
`I18n::Backend::ActiveRecord`, you can setup i18n-js to get translations from a
|
1007
|
+
separate `Simple` backend, by adding the following in an initializer:
|
870
1008
|
|
871
1009
|
```ruby
|
872
1010
|
I18n::JS.backend = I18n.backend
|
873
1011
|
I18n.backend = I18n::Backend::Chain.new(<your other backend(s)>, I18n.backend)
|
874
1012
|
```
|
875
1013
|
|
876
|
-
This will use your backend with the default `Simple` backend as fallback, while
|
877
|
-
|
1014
|
+
This will use your backend with the default `Simple` backend as fallback, while
|
1015
|
+
i18n-js only sees and uses the simple backend. This means however, that only
|
1016
|
+
translations from your static locale files will be present in JavaScript.
|
878
1017
|
|
879
1018
|
If you do cannot use a `Chain`-Backend for some reason, you can also set
|
880
1019
|
|
@@ -883,9 +1022,11 @@ I18n::JS.backend = I18n::Backend::Simple.new
|
|
883
1022
|
I18n.backend = <something different>
|
884
1023
|
```
|
885
1024
|
|
886
|
-
However, the automatic reloading of translations in developement will not work
|
887
|
-
This is because Rails calls `I18n.reload!` for each request in
|
888
|
-
|
1025
|
+
However, the automatic reloading of translations in developement will not work
|
1026
|
+
in this case. This is because Rails calls `I18n.reload!` for each request in
|
1027
|
+
development, but `reload!` will not be called on `I18n::JS.backend`, since it is
|
1028
|
+
a different object. One option would be to patch `I18n.reload!` in an
|
1029
|
+
initializer:
|
889
1030
|
|
890
1031
|
```ruby
|
891
1032
|
module I18n
|
@@ -896,11 +1037,12 @@ module I18n
|
|
896
1037
|
end
|
897
1038
|
```
|
898
1039
|
|
899
|
-
See issue [#428](https://github.com/fnando/i18n-js/issues/428) for more details
|
1040
|
+
See issue [#428](https://github.com/fnando/i18n-js/issues/428) for more details
|
1041
|
+
and discussion of this issue.
|
900
1042
|
|
901
1043
|
## Maintainer
|
902
1044
|
|
903
|
-
- Nando Vieira - <http://nandovieira.com
|
1045
|
+
- Nando Vieira - <http://nandovieira.com>
|
904
1046
|
|
905
1047
|
## Contributing
|
906
1048
|
|
@@ -908,13 +1050,14 @@ Once you've made your great commits:
|
|
908
1050
|
|
909
1051
|
1. [Fork](http://help.github.com/forking/) I18n.js
|
910
1052
|
2. Create a branch with a clear name
|
911
|
-
3. Make your changes (Please also add/change spec, README and CHANGELOG if
|
1053
|
+
3. Make your changes (Please also add/change spec, README and CHANGELOG if
|
1054
|
+
applicable)
|
912
1055
|
4. Push changes to the created branch
|
913
1056
|
5. [Create an Pull Request](http://github.com/fnando/i18n-js/pulls)
|
914
1057
|
6. That's it!
|
915
1058
|
|
916
|
-
Please respect the indentation rules and code style.
|
917
|
-
And
|
1059
|
+
Please respect the indentation rules and code style. And use 2 spaces, not tabs.
|
1060
|
+
And don't touch the versioning thing.
|
918
1061
|
|
919
1062
|
## Running tests
|
920
1063
|
|
@@ -936,21 +1079,19 @@ You can run both Ruby and JavaScript specs with `rake spec`.
|
|
936
1079
|
|
937
1080
|
(The MIT License)
|
938
1081
|
|
939
|
-
Permission is hereby granted, free of charge, to any person obtaining
|
940
|
-
|
941
|
-
|
942
|
-
|
943
|
-
|
944
|
-
|
945
|
-
|
946
|
-
|
947
|
-
|
948
|
-
|
949
|
-
|
950
|
-
|
951
|
-
|
952
|
-
|
953
|
-
IN
|
954
|
-
|
955
|
-
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
|
956
|
-
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
1082
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy of
|
1083
|
+
this software and associated documentation files (the 'Software'), to deal in
|
1084
|
+
the Software without restriction, including without limitation the rights to
|
1085
|
+
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
|
1086
|
+
the Software, and to permit persons to whom the Software is furnished to do so,
|
1087
|
+
subject to the following conditions:
|
1088
|
+
|
1089
|
+
The above copyright notice and this permission notice shall be included in all
|
1090
|
+
copies or substantial portions of the Software.
|
1091
|
+
|
1092
|
+
THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
1093
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
|
1094
|
+
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
|
1095
|
+
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
|
1096
|
+
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
1097
|
+
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|