i18n-js 2.1.2 → 3.9.2

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