]> git.openstreetmap.org Git - rails.git/commitdiff
Update to i18n-js 4.x
authorTom Hughes <tom@compton.nu>
Sun, 16 Mar 2025 16:16:22 +0000 (16:16 +0000)
committerTom Hughes <tom@compton.nu>
Mon, 17 Mar 2025 17:19:16 +0000 (17:19 +0000)
19 files changed:
.github/workflows/docker.yml
.github/workflows/tests.yml
.gitignore
CONFIGURE.md
Gemfile
Gemfile.lock
app/assets/javascripts/application.js
app/assets/javascripts/embed.js.erb
app/assets/javascripts/i18n.js [new file with mode: 0644]
app/assets/javascripts/i18n/.gitkeep [new file with mode: 0644]
config/environments/development.rb
config/environments/test.rb
config/i18n-js.yml [deleted file]
config/i18n.yml [new file with mode: 0644]
config/initializers/i18n.rb
i18n/templates/template.js.erb [new file with mode: 0644]
package.json
test/javascripts/osm_test.js
yarn.lock

index 8d0e9f615fd111c02981fb95720d4c4443e09b46..4cf099637ea6bcce96083982a631951dcb72639b 100644 (file)
@@ -27,7 +27,7 @@ jobs:
     - name: Prepare Database
       run: |
         docker compose run --rm web bundle exec rails db:migrate
-        docker compose run --rm web bundle exec rails i18n:js:export
+        docker compose run --rm web bundle exec i18n export
         docker compose run --rm web bundle exec rails assets:precompile
         docker compose run --rm web osmosis --rx docker/null-island.osm.xml --wd host=db database=openstreetmap user=openstreetmap password=openstreetmap validateSchemaVersion=no
     - name: Test Basic Website
index 841155bf643da3dcdee31b4ae284211dd9ed122a..d8a76bcb147789d9a17ac37c926c41b4ea3b56ed 100644 (file)
@@ -48,10 +48,10 @@ jobs:
         bundle exec rails db:migrate
         sed -f script/normalise-structure db/structure.sql > db/structure.actual
         diff -uw db/structure.expected db/structure.actual
-    - name: Export javascript strings
-      run: bundle exec rails i18n:js:export
     - name: Install node modules
       run: bundle exec bin/yarn install
+    - name: Export javascript strings
+      run: bundle exec i18n export
     - name: Compile assets
       run: bundle exec rails assets:precompile
     - name: Create tmp/pids directory
index 380a88047f9ad35e43edb8abe354958e78fdadbe..68ea67190b0654d8845b538bcd26c9b2a89c4f85 100644 (file)
@@ -6,7 +6,7 @@
 .ruby-gemset
 .ruby-version
 .vagrant
-app/assets/javascripts/i18n
+app/assets/javascripts/i18n/*.js
 config/credentials.yml.enc
 config/master.key
 config/environments/*.local.yml
@@ -14,6 +14,7 @@ config/settings.local.yml
 config/settings/*.local.yml
 coverage
 doc
+i18n/data
 log
 node_modules
 public/assets
index 29d1daad8bc513f82b46a43210cf90047c1c2a6f..815cf92f6e77b0fd66b5c563112429a1732371ee 100644 (file)
@@ -143,5 +143,5 @@ If you want to deploy `openstreetmap-website` for production use, you'll need to
 * It's not recommended to use `rails server` in production. Our recommended approach is to use [Phusion Passenger](https://www.phusionpassenger.com/). Instructions are available for [setting it up with most web servers](https://www.phusionpassenger.com/documentation_and_support#documentation).
 * Passenger will, by design, use the Production environment and therefore the production database - make sure it contains the appropriate data and user accounts.
 * The included version of the map call is quite slow and eats a lot of memory. You should consider using [CGIMap](https://github.com/zerebubuth/openstreetmap-cgimap) instead.
-* Make sure you generate the i18n files and precompile the production assets: `RAILS_ENV=production rails i18n:js:export assets:precompile`
+* Make sure you generate the i18n files and precompile the production assets: `RAILS_ENV=production bundle exec i18n export; bundle exec rails assets:precompile`
 * Make sure the web server user as well as the rails user can read, write and create directories in `tmp/`.
diff --git a/Gemfile b/Gemfile
index ad6b3b3ef2637cc257b7d973de8c1c4eff23d674..bcfafd3b3189ae8ee2ef012b69a8f8860c655bee 100644 (file)
--- a/Gemfile
+++ b/Gemfile
@@ -58,7 +58,7 @@ gem "delayed_job_active_record"
 gem "dry-validation"
 gem "frozen_record"
 gem "http_accept_language", "~> 2.1.1"
-gem "i18n-js", "~> 3.9.2"
+gem "i18n-js", "~> 4.2.3"
 gem "openstreetmap-deadlock_retry", ">= 1.3.1", :require => "deadlock_retry"
 gem "rack-cors"
 gem "rails-i18n", "~> 7.0.0"
index 54daa5bfe72f88432b4976a4437134cf5c5beeb4..021ed20489b010bad5c9a9b6e4bdad020331f9fa 100644 (file)
@@ -288,6 +288,7 @@ GEM
     git (1.19.1)
       addressable (~> 2.8)
       rchardet (~> 1.8)
+    glob (0.4.1)
     globalid (1.2.1)
       activesupport (>= 6.1)
     google-protobuf (3.25.6)
@@ -299,8 +300,9 @@ GEM
     http_accept_language (2.1.1)
     i18n (1.14.7)
       concurrent-ruby (~> 1.0)
-    i18n-js (3.9.2)
-      i18n (>= 0.6.6)
+    i18n-js (4.2.3)
+      glob (>= 0.4.0)
+      i18n
     i18n-tasks (1.0.15)
       activesupport (>= 4.0.2)
       ast (>= 2.1.0)
@@ -719,7 +721,7 @@ DEPENDENCIES
   gd2-ffij (>= 0.4.0)
   htmlentities
   http_accept_language (~> 2.1.1)
-  i18n-js (~> 3.9.2)
+  i18n-js (~> 4.2.3)
   i18n-tasks
   image_optim_rails
   image_processing
index 5c61b01ccfa6687587df15883533734f62530859..54a2cb3f68cfc1f8c2bdd2973f47028d1fcd2af6 100644 (file)
   const application_data = $("head").data();
   const locale = application_data.locale;
 
-  OSM.i18n = I18n;
-
-  OSM.i18n.default_locale = OSM.DEFAULT_LOCALE;
-  OSM.i18n.locale = locale;
+  OSM.i18n.defaultLocale = OSM.DEFAULT_LOCALE;
+  OSM.i18n.locale = application_data.locale;
 
   // '-' are replaced with '_' in https://github.com/eemeli/make-plural/tree/main/packages/plurals
   const pluralizer = plurals[locale.replace(/\W+/g, "_")] || plurals[locale.split("-")[0]];
   if (pluralizer) {
-    OSM.i18n.pluralization[locale] = (count) => [pluralizer(count), "other"];
+    OSM.i18n.pluralization.register(locale, (_, count) => [pluralizer(count), "other"]);
   }
 
   OSM.preferred_editor = application_data.preferredEditor;
index 297259043c212efed20aafec5db4704ee96c4ad5..c436e342b20f39051d4ba665f0e077e2b4a184a0 100644 (file)
@@ -6,16 +6,14 @@
 //= require i18n
 //= require i18n/embed
 
-const OSM = { i18n: I18n };
-
 if (navigator.languages) {
   OSM.i18n.locale = navigator.languages[0];
 } else if (navigator.language) {
   OSM.i18n.locale = navigator.language;
 }
 
-OSM.i18n.default_locale = <%= I18n.default_locale.to_json %>;
-OSM.i18n.fallbacks = true;
+OSM.i18n.defaultLocale = <%= I18n.default_locale.to_json %>;
+OSM.i18n.enableFallback = true;
 
 window.onload = function () {
   const args = Object.fromEntries(new URLSearchParams(location.search));
diff --git a/app/assets/javascripts/i18n.js b/app/assets/javascripts/i18n.js
new file mode 100644 (file)
index 0000000..05c80c6
--- /dev/null
@@ -0,0 +1,7 @@
+//= require i18n-js/dist/browser/index.js
+
+if (typeof OSM === "undefined") {
+  OSM = {};
+}
+
+OSM.i18n = new I18n.I18n();
diff --git a/app/assets/javascripts/i18n/.gitkeep b/app/assets/javascripts/i18n/.gitkeep
new file mode 100644 (file)
index 0000000..e69de29
index bc4b587486a3225b67cf0d1b74b83fd56f78d661..3bde022e577a6140fc2f4be24c567a1af08650ba 100644 (file)
@@ -64,9 +64,6 @@ Rails.application.configure do
   # Suppress logger output for asset requests.
   config.assets.quiet = true
 
-  # Export translations automatically.
-  config.middleware.use I18n::JS::Middleware
-
   # Raises error for missing translations.
   # config.i18n.raise_on_missing_translations = true
 
index 7ac52c5e4d3030b29432f45f2f7b06803d3ae1b9..300af0abd3301093fc020889d0c7a7ecadf5cd3d 100644 (file)
@@ -59,9 +59,6 @@ Rails.application.configure do
   # Tell Active Support which deprecation messages to disallow.
   config.active_support.disallowed_deprecation_warnings = []
 
-  # Export translations automatically.
-  config.middleware.use I18n::JS::Middleware
-
   # Raises error for missing translations.
   config.i18n.raise_on_missing_translations = true
 
diff --git a/config/i18n-js.yml b/config/i18n-js.yml
deleted file mode 100644 (file)
index b7f3b4d..0000000
+++ /dev/null
@@ -1,20 +0,0 @@
-export_i18n_js: false
-
-translations:
-  - file: "app/assets/javascripts/i18n/%{locale}.js"
-    pretty_print: true
-    only:
-    - "*.date"
-    - "*.time"
-    - "*.browse.start_rjs.*"
-    - "*.javascripts.*"
-    - "*.site.edit.*"
-    - "*.site.index.remote_failed"
-    - "*.site.sidebar.search_results"
-    - "*.diary_entries.edit.marker_text"
-    - "*.layouts.project_name.title"
-    - "*.geocoder.search_osm_nominatim.*"
-  - file: "app/assets/javascripts/i18n/embed.js"
-    pretty_print: true
-    only:
-    - "*.javascripts.embed.*"
diff --git a/config/i18n.yml b/config/i18n.yml
new file mode 100644 (file)
index 0000000..e2cff21
--- /dev/null
@@ -0,0 +1,23 @@
+embed_fallback_translations:
+  enabled: true
+export_files:
+  enabled: true
+  files:
+    - template: "i18n/templates/template.js.erb"
+      output: "app/assets/javascripts/i18n/%{base_name}.js"
+translations:
+  - file: "i18n/data/:locale.json"
+    patterns:
+    - "*.date.*"
+    - "*.time.*"
+    - "*.browse.start_rjs.*"
+    - "*.javascripts.*"
+    - "*.site.edit.*"
+    - "*.site.index.remote_failed"
+    - "*.site.sidebar.search_results"
+    - "*.diary_entries.edit.marker_text"
+    - "*.layouts.project_name.title"
+    - "*.geocoder.search_osm_nominatim.*"
+  - file: "i18n/data/embed.json"
+    patterns:
+    - "*.javascripts.embed.*"
index a316db408dfdc06e92a7abe02cbc0e79bd4bdb19..2a1dd51794c89ec110babae28d4ee7cb01a5f618 100644 (file)
@@ -38,5 +38,10 @@ if Rails.env.test?
 end
 
 Rails.configuration.after_initialize do
+  require "i18n-js/listen"
+
+  # This will only run in development.
+  I18nJS.listen
+
   I18n.available_locales
 end
diff --git a/i18n/templates/template.js.erb b/i18n/templates/template.js.erb
new file mode 100644 (file)
index 0000000..8ba9363
--- /dev/null
@@ -0,0 +1 @@
+OSM.i18n.store(<%= JSON.pretty_generate(translations) %>);
index 4812a8bf7d03f32386ce3177d67f29432a543beb..17207ee4d41a26610a79b2d133e9b873cd552bac 100644 (file)
@@ -3,6 +3,7 @@
   "private": true,
   "dependencies": {
     "cal-heatmap": "^4.2.4",
+    "i18n-js": "^4.5.1",
     "jquery-simulate": "^1.0.2",
     "js-cookie": "^3.0.0",
     "leaflet": "^1.8.0",
index 2f96ccb3d4a2de806ab3bca1a806847672c42d74..d70ddc13c60fd1d54984f3a0f30252078098d902 100644 (file)
@@ -4,7 +4,6 @@
 //= require leaflet/dist/leaflet-src
 //= require leaflet.osm
 //= require leaflet.map
-//= require i18n/translations
 
 describe("OSM", function () {
   describe(".apiUrl", function () {
index a384f4b0daf5e99f35703f0be569f3ce09876da9..477d8beeecf618d76d67f88697e64d209f7069f1 100644 (file)
--- a/yarn.lock
+++ b/yarn.lock
@@ -194,6 +194,11 @@ balanced-match@^1.0.0:
   resolved "https://registry.yarnpkg.com/balanced-match/-/balanced-match-1.0.2.tgz#e83e3a7e3f300b34cb9d87f615fa0cbf357690ee"
   integrity sha512-3oSeUO0TMV67hN1AmbXsK4yaqU7tjiHlbxRDZOpH0KW9+CeX4bRAaX0Anxt0tx2MrpRpWwQaPwIlISEJhYU5Pw==
 
+bignumber.js@*:
+  version "9.1.2"
+  resolved "https://registry.yarnpkg.com/bignumber.js/-/bignumber.js-9.1.2.tgz#b7c4242259c008903b13707983b5f4bbd31eda0c"
+  integrity sha512-2/mKyZH9K85bzOEfhXDBFZTGd1CTs+5IHpeFQo9luiBG7hghdC851Pj2WAhb6E3R6b9tZj/XKhbg4fum+Kepug==
+
 binary-search-bounds@^2.0.0:
   version "2.0.5"
   resolved "https://registry.yarnpkg.com/binary-search-bounds/-/binary-search-bounds-2.0.5.tgz#125e5bd399882f71e6660d4bf1186384e989fba7"
@@ -711,6 +716,15 @@ has-flag@^4.0.0:
   resolved "https://registry.yarnpkg.com/has-flag/-/has-flag-4.0.0.tgz#944771fd9c81c81265c4d6941860da06bb59479b"
   integrity sha512-EykJT/Q1KjTWctppgIAgfSO0tKVuZUjhgMr17kqTumMl6Afv3EISleU7qZUzoXDFTAHTDC4NOoG/ZxU3EvlMPQ==
 
+i18n-js@^4.5.1:
+  version "4.5.1"
+  resolved "https://registry.yarnpkg.com/i18n-js/-/i18n-js-4.5.1.tgz#12ea3d6333552ff75be0904ea50705f5a263d172"
+  integrity sha512-n7jojFj1WC0tztgr0I8jqTXuIlY1xNzXnC3mjKX/YjJhimdM+jXM8vOmn9d3xQFNC6qDHJ4ovhdrGXrRXLIGkA==
+  dependencies:
+    bignumber.js "*"
+    lodash "*"
+    make-plural "*"
+
 iconv-lite@0.6:
   version "0.6.3"
   resolved "https://registry.yarnpkg.com/iconv-lite/-/iconv-lite-0.6.3.tgz#a52f80bf38da1952eb5c681790719871a1a72501"
@@ -844,7 +858,12 @@ lodash.merge@^4.6.2:
   resolved "https://registry.yarnpkg.com/lodash.merge/-/lodash.merge-4.6.2.tgz#558aa53b43b661e1925a0afdfa36a9a1085fe57a"
   integrity sha512-0KpjqXRVvrYyCsX1swR/XTK0va6VQkQM6MNo7PqW77ByjAhoARA8EfrP1N4+KlKj8YS0ZUCtRT/YUuhyYDujIQ==
 
-make-plural@^7.4.0:
+lodash@*:
+  version "4.17.21"
+  resolved "https://registry.yarnpkg.com/lodash/-/lodash-4.17.21.tgz#679591c564c3bffaae8454cf0b3df370c3d6911c"
+  integrity sha512-v2kDEe57lecTulaDIuNTPy3Ry4gLGJ6Z1O3vE1krgXZNrsQ+LFTGHVxVjcXPs17LhbZVGedAJv8XZ1tvj5FvSg==
+
+make-plural@*, make-plural@^7.4.0:
   version "7.4.0"
   resolved "https://registry.yarnpkg.com/make-plural/-/make-plural-7.4.0.tgz#fa6990dd550dea4de6b20163f74e5ed83d8a8d6d"
   integrity sha512-4/gC9KVNTV6pvYg2gFeQYTW3mWaoJt7WZE5vrp1KnQDgW92JtYZnzmZT81oj/dUTqAIu0ufI2x3dkgu3bB1tYg==