i18n-js 3.9.2 → 4.0.0.alpha1
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/CODEOWNERS +4 -0
- data/.github/FUNDING.yml +1 -1
- data/.github/ISSUE_TEMPLATE/bug_report.md +41 -0
- data/.github/ISSUE_TEMPLATE/feature_request.md +23 -0
- data/.github/PULL_REQUEST_TEMPLATE.md +38 -0
- data/.github/dependabot.yml +15 -0
- data/.github/workflows/ruby-tests.yml +61 -0
- data/.gitignore +11 -7
- data/.rubocop.yml +12 -0
- data/CHANGELOG.md +12 -571
- data/CODE_OF_CONDUCT.md +74 -0
- data/CONTRIBUTING.md +79 -0
- data/Gemfile +3 -0
- data/LICENSE.md +20 -0
- data/README.md +86 -1043
- data/Rakefile +10 -20
- data/exe/i18n +5 -0
- data/i18n-js.gemspec +47 -29
- data/lib/guard/i18n-js/templates/Guardfile +10 -0
- data/lib/guard/i18n-js/version.rb +13 -0
- data/lib/guard/i18n-js.rb +78 -0
- data/lib/i18n-js/cli/command.rb +43 -0
- data/lib/i18n-js/cli/export_command.rb +108 -0
- data/lib/i18n-js/cli/init_command.rb +50 -0
- data/lib/i18n-js/cli/ui.rb +30 -0
- data/lib/i18n-js/cli/version_command.rb +18 -0
- data/lib/i18n-js/cli.rb +47 -0
- data/lib/i18n-js/listen.rb +80 -0
- data/lib/i18n-js/schema.rb +92 -0
- data/lib/i18n-js/version.rb +5 -0
- data/lib/i18n-js.rb +66 -1
- metadata +125 -201
- data/.editorconfig +0 -24
- data/.github/workflows/tests.yaml +0 -106
- data/.npmignore +0 -27
- data/Appraisals +0 -52
- data/app/assets/javascripts/i18n/filtered.js.erb +0 -23
- data/app/assets/javascripts/i18n/shims.js +0 -240
- data/app/assets/javascripts/i18n/translations.js +0 -3
- data/app/assets/javascripts/i18n.js +0 -1095
- data/gemfiles/i18n_0_6.gemfile +0 -7
- data/gemfiles/i18n_0_7.gemfile +0 -7
- data/gemfiles/i18n_0_8.gemfile +0 -7
- data/gemfiles/i18n_0_9.gemfile +0 -7
- data/gemfiles/i18n_1_0.gemfile +0 -7
- data/gemfiles/i18n_1_1.gemfile +0 -7
- data/gemfiles/i18n_1_10.gemfile +0 -7
- data/gemfiles/i18n_1_2.gemfile +0 -7
- data/gemfiles/i18n_1_3.gemfile +0 -7
- data/gemfiles/i18n_1_4.gemfile +0 -7
- data/gemfiles/i18n_1_5.gemfile +0 -7
- data/gemfiles/i18n_1_6.gemfile +0 -7
- data/gemfiles/i18n_1_7.gemfile +0 -7
- data/gemfiles/i18n_1_8.gemfile +0 -7
- data/gemfiles/i18n_1_9.gemfile +0 -7
- data/i18njs.png +0 -0
- data/lib/i18n/js/dependencies.rb +0 -67
- data/lib/i18n/js/engine.rb +0 -87
- data/lib/i18n/js/fallback_locales.rb +0 -70
- data/lib/i18n/js/formatters/base.rb +0 -25
- data/lib/i18n/js/formatters/js.rb +0 -39
- data/lib/i18n/js/formatters/json.rb +0 -13
- data/lib/i18n/js/middleware.rb +0 -82
- data/lib/i18n/js/private/config_store.rb +0 -31
- data/lib/i18n/js/private/hash_with_symbol_keys.rb +0 -36
- data/lib/i18n/js/segment.rb +0 -81
- data/lib/i18n/js/utils.rb +0 -91
- data/lib/i18n/js/version.rb +0 -7
- data/lib/i18n/js.rb +0 -274
- data/lib/rails/generators/i18n/js/config/config_generator.rb +0 -19
- data/lib/rails/generators/i18n/js/config/templates/i18n-js.yml +0 -27
- data/lib/tasks/export.rake +0 -8
- data/package.json +0 -25
- data/spec/fixtures/custom_path.yml +0 -5
- data/spec/fixtures/default.yml +0 -5
- data/spec/fixtures/erb.yml +0 -5
- data/spec/fixtures/except_condition.yml +0 -7
- data/spec/fixtures/js_available_locales_custom.yml +0 -1
- data/spec/fixtures/js_export_dir_custom.yml +0 -7
- data/spec/fixtures/js_export_dir_none.yml +0 -6
- data/spec/fixtures/js_extend_parent.yml +0 -6
- data/spec/fixtures/js_extend_segment.yml +0 -6
- data/spec/fixtures/js_file_per_locale.yml +0 -7
- data/spec/fixtures/js_file_per_locale_with_fallbacks_as_default_locale_symbol.yml +0 -4
- data/spec/fixtures/js_file_per_locale_with_fallbacks_as_hash.yml +0 -6
- data/spec/fixtures/js_file_per_locale_with_fallbacks_as_locale.yml +0 -4
- data/spec/fixtures/js_file_per_locale_with_fallbacks_as_locale_without_fallback_translations.yml +0 -4
- data/spec/fixtures/js_file_per_locale_with_fallbacks_enabled.yml +0 -4
- data/spec/fixtures/js_file_per_locale_without_fallbacks.yml +0 -4
- data/spec/fixtures/js_file_with_namespace_prefix_and_pretty_print.yml +0 -9
- data/spec/fixtures/js_sort_translation_keys_false.yml +0 -6
- data/spec/fixtures/js_sort_translation_keys_true.yml +0 -6
- data/spec/fixtures/json_only.yml +0 -18
- data/spec/fixtures/locales.yml +0 -133
- data/spec/fixtures/merge_plurals.yml +0 -6
- data/spec/fixtures/merge_plurals_with_no_overrides.yml +0 -4
- data/spec/fixtures/merge_plurals_with_partial_overrides.yml +0 -4
- data/spec/fixtures/millions.yml +0 -4
- data/spec/fixtures/multiple_conditions.yml +0 -7
- data/spec/fixtures/multiple_conditions_per_locale.yml +0 -7
- data/spec/fixtures/multiple_files.yml +0 -7
- data/spec/fixtures/no_config.yml +0 -2
- data/spec/fixtures/no_scope.yml +0 -4
- data/spec/fixtures/simple_scope.yml +0 -5
- data/spec/js/currency.spec.js +0 -62
- data/spec/js/current_locale.spec.js +0 -19
- data/spec/js/dates.spec.js +0 -276
- data/spec/js/defaults.spec.js +0 -31
- data/spec/js/extend.spec.js +0 -110
- data/spec/js/interpolation.spec.js +0 -124
- data/spec/js/jasmine/MIT.LICENSE +0 -20
- data/spec/js/jasmine/jasmine-html.js +0 -190
- data/spec/js/jasmine/jasmine.css +0 -166
- data/spec/js/jasmine/jasmine.js +0 -2476
- data/spec/js/jasmine/jasmine_favicon.png +0 -0
- data/spec/js/json_parsable.spec.js +0 -14
- data/spec/js/locales.spec.js +0 -31
- data/spec/js/localization.spec.js +0 -78
- data/spec/js/numbers.spec.js +0 -174
- data/spec/js/placeholder.spec.js +0 -24
- data/spec/js/pluralization.spec.js +0 -228
- data/spec/js/prepare_options.spec.js +0 -41
- data/spec/js/require.js +0 -2083
- data/spec/js/specs.html +0 -49
- data/spec/js/specs_requirejs.html +0 -72
- data/spec/js/translate.spec.js +0 -304
- data/spec/js/translations.js +0 -188
- data/spec/js/utility_functions.spec.js +0 -20
- data/spec/ruby/i18n/js/fallback_locales_spec.rb +0 -84
- data/spec/ruby/i18n/js/segment_spec.rb +0 -286
- data/spec/ruby/i18n/js/utils_spec.rb +0 -138
- data/spec/ruby/i18n/js_spec.rb +0 -797
- data/spec/spec_helper.rb +0 -80
- data/yarn.lock +0 -138
data/README.md
CHANGED
|
@@ -1,1099 +1,142 @@
|
|
|
1
|
-
|
|
2
|
-
<img width="250" height="58" src="https://github.com/fnando/i18n-js/raw/main/i18njs.png" alt="i18n.js">
|
|
3
|
-
</p>
|
|
1
|
+
# i18n-js
|
|
4
2
|
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
3
|
+
[](https://github.com/fnando/i18n-js)
|
|
4
|
+
[](https://codeclimate.com/github/fnando/i18n-js)
|
|
5
|
+
[](https://rubygems.org/gems/i18n-js)
|
|
6
|
+
[](https://rubygems.org/gems/i18n-js)
|
|
8
7
|
|
|
9
|
-
|
|
10
|
-
|
|
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>
|
|
8
|
+
Export [i18n](https://rubygems.org/gems/i18n) translations to JSON. A perfect
|
|
9
|
+
fit if you want to export translations to JavaScript.
|
|
17
10
|
|
|
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
|
-
```
|
|
11
|
+
Oh, you don't use Ruby? No problem! You can still use i18n-js and the
|
|
12
|
+
[companion JavaScript package](https://npmjs.com/package/i18n-js).
|
|
455
13
|
|
|
456
|
-
|
|
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.
|
|
14
|
+
## Installation
|
|
459
15
|
|
|
460
|
-
|
|
461
|
-
|
|
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");
|
|
16
|
+
```bash
|
|
17
|
+
gem install i18n-js
|
|
718
18
|
```
|
|
719
19
|
|
|
720
|
-
|
|
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
|
|
20
|
+
Or add the following line to your project's Gemfile:
|
|
750
21
|
|
|
751
|
-
|
|
752
|
-
|
|
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
|
-
});
|
|
22
|
+
```ruby
|
|
23
|
+
gem "i18n-js", "~> 4.0.0.alpha1"
|
|
772
24
|
```
|
|
773
25
|
|
|
774
|
-
|
|
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.
|
|
26
|
+
## Usage
|
|
781
27
|
|
|
782
|
-
|
|
28
|
+
About patterns:
|
|
783
29
|
|
|
784
|
-
|
|
785
|
-
|
|
30
|
+
- Patterns can use `*` as a wildcard and can appear more than once.
|
|
31
|
+
- `*` will include everything
|
|
32
|
+
- `*.messages.*`
|
|
33
|
+
- Patterns starting with `!` are excluded.
|
|
34
|
+
- `!*.activerecord.*` will exclude all ActiveRecord translations.
|
|
786
35
|
|
|
787
|
-
|
|
788
|
-
sort_translation_keys: true
|
|
789
|
-
fallbacks: false
|
|
36
|
+
The config file:
|
|
790
37
|
|
|
38
|
+
```yml
|
|
39
|
+
---
|
|
791
40
|
translations:
|
|
792
|
-
|
|
793
|
-
|
|
794
|
-
|
|
795
|
-
|
|
796
|
-
|
|
797
|
-
|
|
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
|
|
41
|
+
- file: app/frontend/locales/en.json
|
|
42
|
+
patterns:
|
|
43
|
+
- "*"
|
|
44
|
+
- "!*.activerecord"
|
|
45
|
+
- "!*.errors"
|
|
46
|
+
- "!*.number.nth"
|
|
825
47
|
|
|
826
|
-
|
|
827
|
-
|
|
828
|
-
|
|
829
|
-
"nls/welcome"
|
|
830
|
-
) %>
|
|
48
|
+
- file: app/frontend/locales/:locale.json
|
|
49
|
+
patterns:
|
|
50
|
+
- "*"
|
|
831
51
|
```
|
|
832
52
|
|
|
833
|
-
|
|
53
|
+
The Ruby API:
|
|
834
54
|
|
|
835
55
|
```ruby
|
|
836
|
-
|
|
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.
|
|
56
|
+
require "i18n-js"
|
|
845
57
|
|
|
846
|
-
|
|
847
|
-
|
|
848
|
-
precompiled by r.js.
|
|
849
|
-
|
|
850
|
-
```yaml
|
|
851
|
-
export_i18n_js: "app/assets/javascript/nls"
|
|
58
|
+
I18nJS.call(config_file: "config/i18n.yml")
|
|
59
|
+
I18nJS.call(config: config)
|
|
852
60
|
```
|
|
853
61
|
|
|
854
|
-
|
|
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'
|
|
62
|
+
The CLI API:
|
|
879
63
|
|
|
880
|
-
|
|
881
|
-
|
|
882
|
-
|
|
883
|
-
include:
|
|
884
|
-
+ 'nls/welcome'
|
|
64
|
+
```console
|
|
65
|
+
$ i18n init --config config/i18n.yml
|
|
66
|
+
$ i18n export --config config/i18n.yml --require config/environment.rb
|
|
885
67
|
```
|
|
886
68
|
|
|
887
|
-
|
|
888
|
-
|
|
889
|
-
|
|
69
|
+
By default, `i18n` will use `config/i18n.yml` and `config/environment.rb` as the
|
|
70
|
+
configuration files. If you don't have these files, then you'll need to specify
|
|
71
|
+
both `--config` and `--require`.
|
|
890
72
|
|
|
891
|
-
|
|
892
|
-
define(["welcome/other_asset", "nls/welcome"], function (otherAsset) {
|
|
893
|
-
// ...
|
|
894
|
-
});
|
|
895
|
-
```
|
|
73
|
+
## Automatically export translations
|
|
896
74
|
|
|
897
|
-
|
|
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.
|
|
75
|
+
### Using guard
|
|
900
76
|
|
|
901
|
-
|
|
902
|
-
|
|
903
|
-
|
|
904
|
-
task :'i18n:js:export' => :'i18n:js:before_export'
|
|
905
|
-
task :'requirejs:precompile:external' => :'i18n:js:export'
|
|
77
|
+
Install [guard](https://rubygems.org/packages/guard) and
|
|
78
|
+
[guard-compat](https://rubygems.org/packages/guard-compat). Then create a
|
|
79
|
+
Guardfile with the following configuration:
|
|
906
80
|
|
|
907
|
-
|
|
908
|
-
|
|
909
|
-
|
|
910
|
-
|
|
911
|
-
|
|
912
|
-
|
|
913
|
-
|
|
81
|
+
```ruby
|
|
82
|
+
guard(:"i18n-js",
|
|
83
|
+
run_on_start: true,
|
|
84
|
+
config_file: "./config/i18n.yml",
|
|
85
|
+
require_file: "./config/environment.rb") do
|
|
86
|
+
watch(%r{^(app|config)/locales/.+\.(yml|po)$})
|
|
87
|
+
watch(%r{^config/i18n.yml$})
|
|
88
|
+
watch("Gemfile")
|
|
914
89
|
end
|
|
915
90
|
```
|
|
916
91
|
|
|
917
|
-
|
|
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`:
|
|
92
|
+
If you files in a different location, the remember to configure file paths
|
|
93
|
+
accordingly.
|
|
940
94
|
|
|
941
|
-
|
|
942
|
-
- registered `preprocessors` are only run when the fingerprint of any asset
|
|
943
|
-
file, including `.erb` files, is changed
|
|
95
|
+
Now you can run `guard start -i`.
|
|
944
96
|
|
|
945
|
-
|
|
946
|
-
trigger a i18n-js refresh. There are a few approaches to work around this:
|
|
97
|
+
### Using listen
|
|
947
98
|
|
|
948
|
-
|
|
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:
|
|
99
|
+
Create a file under `config/initializers/i18n.rb` with the following content:
|
|
1010
100
|
|
|
1011
101
|
```ruby
|
|
1012
|
-
|
|
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.
|
|
102
|
+
# frozen_string_literal: true
|
|
1019
103
|
|
|
1020
|
-
|
|
104
|
+
require "i18n-js/listen"
|
|
1021
105
|
|
|
1022
|
-
|
|
1023
|
-
I18n::JS.backend = I18n::Backend::Simple.new
|
|
1024
|
-
I18n.backend = <something different>
|
|
106
|
+
I18nJS.listen
|
|
1025
107
|
```
|
|
1026
108
|
|
|
1027
|
-
|
|
1028
|
-
|
|
1029
|
-
|
|
1030
|
-
a different object. One option would be to patch `I18n.reload!` in an
|
|
1031
|
-
initializer:
|
|
109
|
+
The code above will watch for changes based on `config/i18n.yml` and
|
|
110
|
+
`config/locales`. You can customize these options with
|
|
111
|
+
`I18nJS.listen(config_file: "config/i18n.yml", locales_dir: "config/locales")`.
|
|
1032
112
|
|
|
1033
|
-
|
|
1034
|
-
module I18n
|
|
1035
|
-
def self.reload!
|
|
1036
|
-
I18n::JS.backend.reload!
|
|
1037
|
-
super
|
|
1038
|
-
end
|
|
1039
|
-
end
|
|
1040
|
-
```
|
|
113
|
+
### Integrating with your frontend
|
|
1041
114
|
|
|
1042
|
-
|
|
1043
|
-
|
|
115
|
+
You're done exporting files, now what? Well, go to
|
|
116
|
+
[i18n](https://github.com/fnando/i18n) to discover how to use the NPM package
|
|
117
|
+
that loads all the exported translation.
|
|
1044
118
|
|
|
1045
119
|
## Maintainer
|
|
1046
120
|
|
|
1047
|
-
- Nando Vieira
|
|
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
|
|
121
|
+
- [Nando Vieira](https://github.com/fnando)
|
|
1065
122
|
|
|
1066
|
-
|
|
123
|
+
## Contributors
|
|
1067
124
|
|
|
1068
|
-
|
|
125
|
+
- https://github.com/fnando/i18n-js/contributors
|
|
1069
126
|
|
|
1070
|
-
|
|
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.
|
|
127
|
+
## Contributing
|
|
1077
128
|
|
|
1078
|
-
|
|
129
|
+
For more details about how to contribute, please read
|
|
130
|
+
https://github.com/fnando/i18n-js/blob/main/CONTRIBUTING.md.
|
|
1079
131
|
|
|
1080
132
|
## License
|
|
1081
133
|
|
|
1082
|
-
|
|
1083
|
-
|
|
1084
|
-
|
|
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:
|
|
134
|
+
The gem is available as open source under the terms of the
|
|
135
|
+
[MIT License](https://opensource.org/licenses/MIT). A copy of the license can be
|
|
136
|
+
found at https://github.com/fnando/i18n-js/blob/main/LICENSE.md.
|
|
1090
137
|
|
|
1091
|
-
|
|
1092
|
-
copies or substantial portions of the Software.
|
|
138
|
+
## Code of Conduct
|
|
1093
139
|
|
|
1094
|
-
|
|
1095
|
-
|
|
1096
|
-
|
|
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.
|
|
140
|
+
Everyone interacting in the i18n-js project's codebases, issue trackers, chat
|
|
141
|
+
rooms and mailing lists is expected to follow the
|
|
142
|
+
[code of conduct](https://github.com/fnando/i18n-js/blob/main/CODE_OF_CONDUCT.md).
|