i18n-js 4.0.0 → 4.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 0b2fdace9fca14d471ff34c3b24055d4093d82ce2a1f72f2df06364a98454a31
4
- data.tar.gz: 9ea6bd71626357c8fce24c28e991b9edc96f8a3230e55c4d55cf1dec3813c94e
3
+ metadata.gz: 6dac70b6202710a44a8303deff7206848d702b4cdb3cc926552aec41114ff745
4
+ data.tar.gz: 2631494d675d0d8459ab5450bb634fee6c305dd6c01d32bb03d647ac2a657d9e
5
5
  SHA512:
6
- metadata.gz: 2c8da0348d02f04d805a013dadd24beb08fb7de555bfdded51e4e6c7c29ba392978a723b7e95be9d752dda2b35bbe6d6aefe4d4cf9166c1ddca09f003712533f
7
- data.tar.gz: 882af1975b1f2e479ad4d8bdcbe812175fae165c80a2f915223e14fc71fb0b3236774254e103754169eab5a3d577e219e10803fd05e14fa3bac82471e4c770e1
6
+ metadata.gz: 67c7b4a499d4848967461c1d3a798de2d814af972049f6977d7cf4dc5c6781bfc6ecdc1129068f963ba8dc115ebe5c219f078ef00b92204f86c54a114d55aba3
7
+ data.tar.gz: 329799184d84e27455783a6a4522b9a0cce446ba33ab3d5cfe74e22d08f685736abb4329e2506e1c2d83504d963c777317f8998a838fb3fae8c358f65569c5b5
@@ -9,12 +9,15 @@ on:
9
9
 
10
10
  jobs:
11
11
  build:
12
- name: Tests with Ruby ${{ matrix.ruby }} and ${{ matrix.gemfile }}
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@v2-beta
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
@@ -9,3 +9,5 @@
9
9
  /vendor/
10
10
  *.log
11
11
  *.lock
12
+ /lib/**/*.js
13
+ /test/output
data/.rubocop.yml CHANGED
@@ -14,3 +14,6 @@ Naming/FileName:
14
14
  Exclude:
15
15
  - lib/i18n-js.rb
16
16
  - lib/guard/i18n-js.rb
17
+
18
+ Style/PerlBackrefs:
19
+ Enabled: false
data/CHANGELOG.md CHANGED
@@ -11,6 +11,35 @@ Prefix your message with one of the following:
11
11
  - [Security] in case of vulnerabilities.
12
12
  -->
13
13
 
14
- ## Jul 29, 2022
14
+ ## v4.2.0 - Dec 10, 2022
15
+
16
+ - [Added] Add `I18nJS::Plugin.after_export(files:, config:)` method, that's
17
+ called whenever whenever I18nJS finishes exporting files. You can use it to
18
+ further process files, or generate new files based on the exported files.
19
+ - [Added] Bult-in plugin `I18nJS::ExportFilesPlugin`, which allows exporting
20
+ files out of the translations file by using a custom template.
21
+
22
+ ## v4.1.0 - Dec 09, 2022
23
+
24
+ - [Added] Parse configuration files as erb.
25
+ - [Changed] `I18n.listen(run_on_start:)` was added to control if files should be
26
+ exported during `I18n.listen`'s boot. The default value is `true`.
27
+ - [Added] Now it's possible to transform translations before exporting them
28
+ using a stable plugin api.
29
+ - [Added] Built-in plugin `I18nJS::EmbedFallbackTranslationsPlugin`, which
30
+ allows embedding missing translations on exported files.
31
+ - [Deprecated] The `i18n check` has been deprecated. Use
32
+ `i18n lint:translations` instead.
33
+ - [Added] Use `i18n lint:scripts` to lint JavaScript/TypeScript.
34
+ - [Fixed] Expand paths passed to `I18nJS.listen(locales_dir:)`.
35
+
36
+ ## v4.0.1 - Aug 25, 2022
37
+
38
+ - [Fixed] Shell out export to avoid handling I18n reloading heuristics.
39
+ - [Changed] `I18nJS.listen` now accepts a directories list to watch.
40
+ - [Changed] `I18nJS.listen` now accepts
41
+ [listen](https://rubygems.org/gems/listen) options via `:options`.
42
+
43
+ ## v4.0.0 - Jul 29, 2022
15
44
 
16
45
  - Official release of i18n-js v4.0.0.
@@ -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/packages/guard) and
51
+ [guard-compat](https://rubygems.org/packages/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`: removed without an equivalent
178
+ - `fallbacks`: removed without an equivalent
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.