i18n-js 3.2.1 → 4.2.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.github/CODEOWNERS +4 -0
- data/.github/FUNDING.yml +3 -0
- data/.github/ISSUE_TEMPLATE/bug_report.md +41 -0
- data/.github/ISSUE_TEMPLATE/config.yml +5 -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 +73 -0
- data/.gitignore +13 -7
- data/.rubocop.yml +19 -0
- data/CHANGELOG.md +45 -386
- data/CODE_OF_CONDUCT.md +74 -0
- data/CONTRIBUTING.md +79 -0
- data/Gemfile +3 -0
- data/LICENSE.md +20 -0
- data/MIGRATING_FROM_V3_TO_V4.md +191 -0
- data/README.md +439 -791
- data/Rakefile +10 -20
- data/bin/release +81 -0
- data/exe/i18n +5 -0
- data/i18n-js.gemspec +51 -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 +95 -0
- data/lib/i18n-js/clean_hash.rb +13 -0
- data/lib/i18n-js/cli/check_command.rb +17 -0
- data/lib/i18n-js/cli/command.rb +79 -0
- data/lib/i18n-js/cli/export_command.rb +95 -0
- data/lib/i18n-js/cli/init_command.rb +52 -0
- data/lib/i18n-js/cli/lint_scripts_command.rb +157 -0
- data/lib/i18n-js/cli/lint_translations_command.rb +155 -0
- data/lib/i18n-js/cli/plugins_command.rb +67 -0
- data/lib/i18n-js/cli/ui.rb +64 -0
- data/lib/i18n-js/cli/version_command.rb +18 -0
- data/lib/i18n-js/cli.rb +66 -0
- data/lib/i18n-js/embed_fallback_translations_plugin.rb +70 -0
- data/lib/i18n-js/export_files_plugin.rb +103 -0
- data/lib/i18n-js/lint.js +150645 -0
- data/lib/i18n-js/lint.ts +196 -0
- data/lib/i18n-js/listen.rb +96 -0
- data/lib/i18n-js/plugin.rb +103 -0
- data/lib/i18n-js/schema.rb +216 -0
- data/lib/i18n-js/sort_hash.rb +12 -0
- data/lib/i18n-js/version.rb +5 -0
- data/lib/i18n-js.rb +107 -1
- data/package.json +5 -20
- metadata +153 -181
- data/.editorconfig +0 -24
- data/.npmignore +0 -27
- data/.travis.yml +0 -36
- data/Appraisals +0 -32
- data/app/assets/javascripts/i18n/filtered.js.erb +0 -23
- data/app/assets/javascripts/i18n/shims.js +0 -208
- data/app/assets/javascripts/i18n/translations.js +0 -3
- data/app/assets/javascripts/i18n.js +0 -1077
- 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_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/lib/i18n/js/dependencies.rb +0 -59
- 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 -23
- data/lib/i18n/js/formatters/js.rb +0 -31
- data/lib/i18n/js/formatters/json.rb +0 -13
- data/lib/i18n/js/middleware.rb +0 -82
- data/lib/i18n/js/private/hash_with_symbol_keys.rb +0 -36
- data/lib/i18n/js/segment.rb +0 -75
- data/lib/i18n/js/utils.rb +0 -65
- data/lib/i18n/js/version.rb +0 -7
- data/lib/i18n/js.rb +0 -259
- 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/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_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_and_pretty_print.yml +0 -7
- 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 -95
- data/spec/fixtures/merge_plurals.yml +0 -6
- 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 -265
- 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/locales.spec.js +0 -31
- data/spec/js/localization.spec.js +0 -48
- data/spec/js/numbers.spec.js +0 -170
- data/spec/js/placeholder.spec.js +0 -24
- data/spec/js/pluralization.spec.js +0 -211
- 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 -284
- data/spec/js/translations.js +0 -163
- 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 -219
- data/spec/ruby/i18n/js/utils_spec.rb +0 -106
- data/spec/ruby/i18n/js_spec.rb +0 -663
- data/spec/spec_helper.rb +0 -79
- data/yarn.lock +0 -131
@@ -0,0 +1,191 @@
|
|
1
|
+
# Migrating from v3 to v4
|
2
|
+
|
3
|
+
I18n-js v4 is a breaking change release and diverges quite a lot from how the
|
4
|
+
previous version worked. This guides summarizes the process of upgrading an app
|
5
|
+
that uses i18n-js v3 to v4.
|
6
|
+
|
7
|
+
## Development
|
8
|
+
|
9
|
+
Previously, you could use a middleware to export translations (some people even
|
10
|
+
used this in production 😬). In development, you can now use whatever your want,
|
11
|
+
because i18n-js doesn't make any assumptions. All you need to do is running
|
12
|
+
`i18n export`, either manually or by using something that listens to file
|
13
|
+
changes.
|
14
|
+
|
15
|
+
If you like watchman, you can use something like this:
|
16
|
+
|
17
|
+
```bash
|
18
|
+
#!/usr/bin/env bash
|
19
|
+
|
20
|
+
root=`pwd`
|
21
|
+
|
22
|
+
watchman watch-del "$root"
|
23
|
+
watchman watch-project "$root"
|
24
|
+
watchman trigger-del "$root" i18n
|
25
|
+
|
26
|
+
watchman -j <<-JSON
|
27
|
+
[
|
28
|
+
"trigger",
|
29
|
+
"$root",
|
30
|
+
{
|
31
|
+
"name": "i18n",
|
32
|
+
"expression": [
|
33
|
+
"anyof",
|
34
|
+
["match", "config/locales/**/*.yml", "wholename"],
|
35
|
+
["match", "config/locales/**/*.po", "wholename"],
|
36
|
+
["match", "config/i18n.yml", "wholename"]
|
37
|
+
],
|
38
|
+
"command": ["i18n", "export"]
|
39
|
+
}
|
40
|
+
]
|
41
|
+
JSON
|
42
|
+
|
43
|
+
# If you're running this through Foreman, then uncomment the following lines:
|
44
|
+
# while true; do
|
45
|
+
# sleep 1
|
46
|
+
# done
|
47
|
+
```
|
48
|
+
|
49
|
+
You can also use guard. Make sure you have both
|
50
|
+
[guard](https://rubygems.org/gems/guard) and
|
51
|
+
[guard-compat](https://rubygems.org/gems/guard-compat) installed and use
|
52
|
+
Guardfile file with the following contents:
|
53
|
+
|
54
|
+
```ruby
|
55
|
+
guard(:"i18n-js",
|
56
|
+
run_on_start: true,
|
57
|
+
config_file: "./config/i18n.yml",
|
58
|
+
require_file: "./config/environment.rb") do
|
59
|
+
watch(%r{^config/locales/.+\.(yml|po)$})
|
60
|
+
watch(%r{^config/i18n.yml$})
|
61
|
+
watch("Gemfile")
|
62
|
+
end
|
63
|
+
```
|
64
|
+
|
65
|
+
To run guard, use `guard start -i`.
|
66
|
+
|
67
|
+
Finally, you can use [listen](https://rubygems.org/gems/listen). Create the file
|
68
|
+
`config/initializers/i18n.rb` with the following content:
|
69
|
+
|
70
|
+
```ruby
|
71
|
+
Rails.application.config.after_initialize do
|
72
|
+
require "i18n-js/listen"
|
73
|
+
# This will only run in development.
|
74
|
+
I18nJS.listen
|
75
|
+
end
|
76
|
+
```
|
77
|
+
|
78
|
+
> **Warning**:
|
79
|
+
>
|
80
|
+
> No matter which approach you choose, the idea is that you _precompile_ your
|
81
|
+
> translations when going to production. DO NOT RUN any of the above in
|
82
|
+
> production.
|
83
|
+
|
84
|
+
## Exporting translations
|
85
|
+
|
86
|
+
The build process for i18n now relies on an external CLI called `i18n`. All you
|
87
|
+
need to do is executing `i18n export` in your build step to generate the json
|
88
|
+
files for your translations.
|
89
|
+
|
90
|
+
## Using your translations
|
91
|
+
|
92
|
+
The JavaScript package is now a separate thing and need to be installed using
|
93
|
+
your favorite tooling (e.g. yarn, npm, pnpm, etc).
|
94
|
+
|
95
|
+
```console
|
96
|
+
$ yarn add i18n-js@latest
|
97
|
+
$ npm i --save-dev i18n-js@latest
|
98
|
+
```
|
99
|
+
|
100
|
+
From now on, the way you load translations and set up I18n-js is totally up to
|
101
|
+
you, but means you need to load the json files and attach to the I18n-js
|
102
|
+
instance. This is how I do it in a project I'm doing right now (Rails 7 +
|
103
|
+
esbuild + TypeScript). First, we need to load the I18n-js configuration from the
|
104
|
+
main JavaScript file:
|
105
|
+
|
106
|
+
```typescript
|
107
|
+
// app/javascript/application.ts
|
108
|
+
import { i18n } from "./config/i18n";
|
109
|
+
```
|
110
|
+
|
111
|
+
Then we need to load our translations and instantiate the I18n-js class.
|
112
|
+
|
113
|
+
```typescript
|
114
|
+
// app/javascript/config/i18n.ts
|
115
|
+
import { I18n } from "i18n-js";
|
116
|
+
import translations from "translations.json";
|
117
|
+
|
118
|
+
// Fetch user locale from html#lang.
|
119
|
+
// This value is being set on `app/views/layouts/application.html.erb` and
|
120
|
+
// is inferred from `ACCEPT-LANGUAGE` header.
|
121
|
+
const userLocale = document.documentElement.lang;
|
122
|
+
|
123
|
+
export const i18n = new I18n();
|
124
|
+
i18n.store(translations);
|
125
|
+
i18n.defaultLocale = "en";
|
126
|
+
i18n.enableFallback = true;
|
127
|
+
i18n.locale = userLocale;
|
128
|
+
```
|
129
|
+
|
130
|
+
The best thing about the above is that it is a pretty straightforward pattern in
|
131
|
+
the JavaScript community. It doesn't rely on specific parts from Sprockets (I'm
|
132
|
+
not even using it on my projects) or eRb files.
|
133
|
+
|
134
|
+
## Ruby on Rails
|
135
|
+
|
136
|
+
### Upgrading the configuration file
|
137
|
+
|
138
|
+
The configuration file loaded from `config/i18n.yml` has changed. Given the v3
|
139
|
+
configuration below
|
140
|
+
|
141
|
+
```yaml
|
142
|
+
---
|
143
|
+
translations:
|
144
|
+
- file: "app/assets/javascripts/date_formats.js"
|
145
|
+
only: "*.date.formats"
|
146
|
+
- file: "app/assets/javascripts/other.js"
|
147
|
+
only: ["*.activerecord", "*.admin.*.title"]
|
148
|
+
- file: "app/assets/javascripts/everything_else.js"
|
149
|
+
except:
|
150
|
+
- "*.activerecord"
|
151
|
+
- "*.admin.*.title"
|
152
|
+
- "*.date.formats"
|
153
|
+
```
|
154
|
+
|
155
|
+
the equivalent configuration file for v4 would be
|
156
|
+
|
157
|
+
```yaml
|
158
|
+
---
|
159
|
+
translations:
|
160
|
+
- file: "app/assets/javascripts/date_formats.js"
|
161
|
+
patterns:
|
162
|
+
- "*.date.formats"
|
163
|
+
- file: "app/assets/javascripts/other.js"
|
164
|
+
patterns:
|
165
|
+
- "*.activerecord"
|
166
|
+
- "*.admin.*.title"
|
167
|
+
- file: "app/assets/javascripts/everything_else.js"
|
168
|
+
patterns:
|
169
|
+
# Notice the exclamation mark.
|
170
|
+
- "!*.activerecord"
|
171
|
+
- "!*.admin.*.title"
|
172
|
+
- "!*.date.formats"
|
173
|
+
```
|
174
|
+
|
175
|
+
Other configuration options:
|
176
|
+
|
177
|
+
- `export_i18n_js`: replaced by [export_files plugin](https://github.com/fnando/i18n-js#export_files)
|
178
|
+
- `fallbacks`: replaced by [embed_fallback_translations plugin](https://github.com/fnando/i18n-js#embed_fallback_translations)
|
179
|
+
- `js_available_locales`: removed (on v4 you can use groups, like in
|
180
|
+
`{pt-BR,en}.*`)
|
181
|
+
- `namespace`: removed without an equivalent
|
182
|
+
- `sort_translation_keys`: removed (on v4 keys will always be sorted)
|
183
|
+
- `translations[].prefix`: removed without an equivalent
|
184
|
+
- `translations[].pretty_print`: removed (on v4 files will always be exported in
|
185
|
+
a readable format)
|
186
|
+
|
187
|
+
### Placeholders
|
188
|
+
|
189
|
+
Previously, v3 had the `%{locale}` placeholder, which can be used as part of the
|
190
|
+
directory and/or file name. Now, the syntax is just `:locale`. Additionally, you
|
191
|
+
can also use `:digest`, which uses a MD5 hex digest of the exported file.
|