From: Tom Hughes Date: Sun, 16 Mar 2025 16:16:22 +0000 (+0000) Subject: Update to i18n-js 4.x X-Git-Tag: live~2^2 X-Git-Url: https://git.openstreetmap.org./rails.git/commitdiff_plain/d52c7943f24aae4ede55f1ef055af415dcf3da59?hp=-c Update to i18n-js 4.x --- d52c7943f24aae4ede55f1ef055af415dcf3da59 diff --git a/.github/workflows/docker.yml b/.github/workflows/docker.yml index 8d0e9f615..4cf099637 100644 --- a/.github/workflows/docker.yml +++ b/.github/workflows/docker.yml @@ -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 diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml index 841155bf6..d8a76bcb1 100644 --- a/.github/workflows/tests.yml +++ b/.github/workflows/tests.yml @@ -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 diff --git a/.gitignore b/.gitignore index 380a88047..68ea67190 100644 --- a/.gitignore +++ b/.gitignore @@ -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 diff --git a/CONFIGURE.md b/CONFIGURE.md index 29d1daad8..815cf92f6 100644 --- a/CONFIGURE.md +++ b/CONFIGURE.md @@ -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 ad6b3b3ef..bcfafd3b3 100644 --- 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" diff --git a/Gemfile.lock b/Gemfile.lock index 54daa5bfe..021ed2048 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -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 diff --git a/app/assets/javascripts/application.js b/app/assets/javascripts/application.js index 5c61b01cc..54a2cb3f6 100644 --- a/app/assets/javascripts/application.js +++ b/app/assets/javascripts/application.js @@ -19,15 +19,13 @@ 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; diff --git a/app/assets/javascripts/embed.js.erb b/app/assets/javascripts/embed.js.erb index 297259043..c436e342b 100644 --- a/app/assets/javascripts/embed.js.erb +++ b/app/assets/javascripts/embed.js.erb @@ -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 index 000000000..05c80c623 --- /dev/null +++ b/app/assets/javascripts/i18n.js @@ -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 index 000000000..e69de29bb diff --git a/config/environments/development.rb b/config/environments/development.rb index bc4b58748..3bde022e5 100644 --- a/config/environments/development.rb +++ b/config/environments/development.rb @@ -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 diff --git a/config/environments/test.rb b/config/environments/test.rb index 7ac52c5e4..300af0abd 100644 --- a/config/environments/test.rb +++ b/config/environments/test.rb @@ -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 index b7f3b4dbc..000000000 --- a/config/i18n-js.yml +++ /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 index 000000000..e2cff2109 --- /dev/null +++ b/config/i18n.yml @@ -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.*" diff --git a/config/initializers/i18n.rb b/config/initializers/i18n.rb index a316db408..2a1dd5179 100644 --- a/config/initializers/i18n.rb +++ b/config/initializers/i18n.rb @@ -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 index 000000000..8ba93638e --- /dev/null +++ b/i18n/templates/template.js.erb @@ -0,0 +1 @@ +OSM.i18n.store(<%= JSON.pretty_generate(translations) %>); diff --git a/package.json b/package.json index 4812a8bf7..17207ee4d 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/test/javascripts/osm_test.js b/test/javascripts/osm_test.js index 2f96ccb3d..d70ddc13c 100644 --- a/test/javascripts/osm_test.js +++ b/test/javascripts/osm_test.js @@ -4,7 +4,6 @@ //= require leaflet/dist/leaflet-src //= require leaflet.osm //= require leaflet.map -//= require i18n/translations describe("OSM", function () { describe(".apiUrl", function () { diff --git a/yarn.lock b/yarn.lock index a384f4b0d..477d8beee 100644 --- 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==