i18n-js 4.0.0 → 4.2.3
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/workflows/ruby-tests.yml +22 -1
- data/.gitignore +2 -0
- data/.rubocop.yml +3 -0
- data/CHANGELOG.md +48 -1
- data/MIGRATING_FROM_V3_TO_V4.md +192 -0
- data/README.md +322 -25
- data/bin/release +81 -0
- data/i18n-js.gemspec +5 -2
- data/lib/guard/i18n-js.rb +21 -4
- data/lib/i18n-js/clean_hash.rb +13 -0
- data/lib/i18n-js/cli/check_command.rb +7 -147
- data/lib/i18n-js/cli/command.rb +10 -0
- data/lib/i18n-js/cli/export_command.rb +17 -3
- data/lib/i18n-js/cli/lint_scripts_command.rb +159 -0
- data/lib/i18n-js/cli/lint_translations_command.rb +157 -0
- data/lib/i18n-js/cli/plugins_command.rb +67 -0
- data/lib/i18n-js/cli.rb +12 -1
- 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 +25 -10
- data/lib/i18n-js/plugin.rb +103 -0
- data/lib/i18n-js/schema.rb +153 -41
- data/lib/i18n-js/sort_hash.rb +12 -0
- data/lib/i18n-js/version.rb +1 -1
- data/lib/i18n-js.rb +37 -3
- data/package.json +10 -0
- metadata +36 -11
- data/i18njs.png +0 -0
- data/images/i18njs-check.gif +0 -0
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: '08fcf5b6b9e3cff755d11708a88e88cc8d30609251cba16f52e396a86014ac85'
|
|
4
|
+
data.tar.gz: 90b3fa30d3e6115fe76f81b5621caba070160cf585890f122aa8f646ee10fd2e
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 02d7a1a0d46b452727465a35cc9a9c46f4e8fe3d0baee2680e75d70fd6512972c7a28481795f9d0a07295612bd8553b932fb27fae2e86f83adc39e4d8aec5209
|
|
7
|
+
data.tar.gz: 9987a9679a3aa39f50f70e4b7b67469c2c7549b61b738f3104b837119a1afeb253e932119f24bdb39f29ffdb9b1840aa4f9b20035d1a70f71e51cfc8f0310b03
|
|
@@ -9,12 +9,15 @@ on:
|
|
|
9
9
|
|
|
10
10
|
jobs:
|
|
11
11
|
build:
|
|
12
|
-
name:
|
|
12
|
+
name:
|
|
13
|
+
Tests with Ruby ${{ matrix.ruby }}, Node ${{ matrix.node }} and ${{
|
|
14
|
+
matrix.gemfile }}
|
|
13
15
|
runs-on: "ubuntu-latest"
|
|
14
16
|
strategy:
|
|
15
17
|
fail-fast: false
|
|
16
18
|
matrix:
|
|
17
19
|
ruby: ["2.7", "3.0", "3.1"]
|
|
20
|
+
node: ["16", "18"]
|
|
18
21
|
gemfile:
|
|
19
22
|
- Gemfile
|
|
20
23
|
if: |
|
|
@@ -32,6 +35,23 @@ jobs:
|
|
|
32
35
|
${{ runner.os }}-${{ matrix.ruby }}-gems-${{
|
|
33
36
|
hashFiles(matrix.gemfile) }}
|
|
34
37
|
|
|
38
|
+
- uses: actions/cache@v3
|
|
39
|
+
id: npm-cache
|
|
40
|
+
with:
|
|
41
|
+
path: vendor/bundle
|
|
42
|
+
key: >
|
|
43
|
+
${{ runner.os }}-${{ matrix.node }}-npm-${{
|
|
44
|
+
hashFiles('package.json') }}
|
|
45
|
+
|
|
46
|
+
- name: Set up Node
|
|
47
|
+
uses: actions/setup-node@v3.6.0
|
|
48
|
+
with:
|
|
49
|
+
node-version: ${{ matrix.node }}
|
|
50
|
+
|
|
51
|
+
- name: Install npm dependencies
|
|
52
|
+
run: |
|
|
53
|
+
yarn install
|
|
54
|
+
|
|
35
55
|
- name: Set up Ruby
|
|
36
56
|
uses: ruby/setup-ruby@v1
|
|
37
57
|
with:
|
|
@@ -49,4 +69,5 @@ jobs:
|
|
|
49
69
|
env:
|
|
50
70
|
BUNDLE_GEMFILE: ${{ matrix.gemfile }}
|
|
51
71
|
run: |
|
|
72
|
+
yarn compile
|
|
52
73
|
bundle exec rake
|
data/.gitignore
CHANGED
data/.rubocop.yml
CHANGED
data/CHANGELOG.md
CHANGED
|
@@ -11,6 +11,53 @@ Prefix your message with one of the following:
|
|
|
11
11
|
- [Security] in case of vulnerabilities.
|
|
12
12
|
-->
|
|
13
13
|
|
|
14
|
-
##
|
|
14
|
+
## v4.2.3 - Mar 29, 2023
|
|
15
|
+
|
|
16
|
+
- [Fixed] Load plugins when running `i18n lint:*` commands.
|
|
17
|
+
|
|
18
|
+
## v4.2.2 - Dec 30, 2022
|
|
19
|
+
|
|
20
|
+
- [Changed] Do not re-export files whose contents haven't changed.
|
|
21
|
+
- [Changed] Translations will always be deep sorted.
|
|
22
|
+
- [Fixed] Remove procs from translations before exporting files.
|
|
23
|
+
|
|
24
|
+
## v4.2.1 - Dec 25, 2022
|
|
25
|
+
|
|
26
|
+
- [Changed] Change plugin api to be based on instance methods. This avoids
|
|
27
|
+
having to pass in the config for each and every method. It also allows us
|
|
28
|
+
adding helper methods to the base class.
|
|
29
|
+
- [Fixed] Fix performance issues with embed fallback translations' initial
|
|
30
|
+
implementation.
|
|
31
|
+
|
|
32
|
+
## v4.2.0 - Dec 10, 2022
|
|
33
|
+
|
|
34
|
+
- [Added] Add `I18nJS::Plugin.after_export(files:, config:)` method, that's
|
|
35
|
+
called whenever whenever I18nJS finishes exporting files. You can use it to
|
|
36
|
+
further process files, or generate new files based on the exported files.
|
|
37
|
+
- [Added] Bult-in plugin `I18nJS::ExportFilesPlugin`, which allows exporting
|
|
38
|
+
files out of the translations file by using a custom template.
|
|
39
|
+
|
|
40
|
+
## v4.1.0 - Dec 09, 2022
|
|
41
|
+
|
|
42
|
+
- [Added] Parse configuration files as erb.
|
|
43
|
+
- [Changed] `I18n.listen(run_on_start:)` was added to control if files should be
|
|
44
|
+
exported during `I18n.listen`'s boot. The default value is `true`.
|
|
45
|
+
- [Added] Now it's possible to transform translations before exporting them
|
|
46
|
+
using a stable plugin api.
|
|
47
|
+
- [Added] Built-in plugin `I18nJS::EmbedFallbackTranslationsPlugin`, which
|
|
48
|
+
allows embedding missing translations on exported files.
|
|
49
|
+
- [Deprecated] The `i18n check` has been deprecated. Use
|
|
50
|
+
`i18n lint:translations` instead.
|
|
51
|
+
- [Added] Use `i18n lint:scripts` to lint JavaScript/TypeScript.
|
|
52
|
+
- [Fixed] Expand paths passed to `I18nJS.listen(locales_dir:)`.
|
|
53
|
+
|
|
54
|
+
## v4.0.1 - Aug 25, 2022
|
|
55
|
+
|
|
56
|
+
- [Fixed] Shell out export to avoid handling I18n reloading heuristics.
|
|
57
|
+
- [Changed] `I18nJS.listen` now accepts a directories list to watch.
|
|
58
|
+
- [Changed] `I18nJS.listen` now accepts
|
|
59
|
+
[listen](https://rubygems.org/gems/listen) options via `:options`.
|
|
60
|
+
|
|
61
|
+
## v4.0.0 - Jul 29, 2022
|
|
15
62
|
|
|
16
63
|
- Official release of i18n-js v4.0.0.
|
|
@@ -0,0 +1,192 @@
|
|
|
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
|
+
- "*"
|
|
171
|
+
- "!*.activerecord"
|
|
172
|
+
- "!*.admin.*.title"
|
|
173
|
+
- "!*.date.formats"
|
|
174
|
+
```
|
|
175
|
+
|
|
176
|
+
Other configuration options:
|
|
177
|
+
|
|
178
|
+
- `export_i18n_js`: replaced by [export_files plugin](https://github.com/fnando/i18n-js#export_files)
|
|
179
|
+
- `fallbacks`: replaced by [embed_fallback_translations plugin](https://github.com/fnando/i18n-js#embed_fallback_translations)
|
|
180
|
+
- `js_available_locales`: removed (on v4 you can use groups, like in
|
|
181
|
+
`{pt-BR,en}.*`)
|
|
182
|
+
- `namespace`: removed without an equivalent
|
|
183
|
+
- `sort_translation_keys`: removed (on v4 keys will always be sorted)
|
|
184
|
+
- `translations[].prefix`: removed without an equivalent
|
|
185
|
+
- `translations[].pretty_print`: removed (on v4 files will always be exported in
|
|
186
|
+
a readable format)
|
|
187
|
+
|
|
188
|
+
### Placeholders
|
|
189
|
+
|
|
190
|
+
Previously, v3 had the `%{locale}` placeholder, which can be used as part of the
|
|
191
|
+
directory and/or file name. Now, the syntax is just `:locale`. Additionally, you
|
|
192
|
+
can also use `:digest`, which uses a MD5 hex digest of the exported file.
|