Skip to content

armgit / spree

Public Imported from GitHub
0

Open Source Platform for DTC, B2B Commerce, Marketplaces & Omnichannel. REST APIs, TypeScript SDKs, and production-ready Next.js storefront. Self-host it. Own your data. No vendor lock-in. Zero platform fees.

14.4 KB
Raw Blame

Translations in core

Status: In Progress — building migration steps 1–5 (core, API and emails); spree-starter and the spree_i18n release follow Target: Spree 6.0 Depends on: 6.0-admin-ui-owns-its-copy.md (the dashboard owns its own text) Author: Damian Legawiec Last updated: 2026-10-07 Tracking: Linear V-3733 · GitHub spree/spree#14830

Summary

Spree's Ruby-side text — customer emails, Store API error messages, model and attribute names inside validation messages, and the fallback labels on Admin API type catalogs — is translated through Spree.t, and the translations for every language except English live in a separate gem, spree_i18n. Both are left over from the server-rendered admin and storefront. Spree.t runs through Rails' view translation helper, which needs Action View and returns HTML (<span class="translation_missing">) for a missing key, so that markup leaks into JSON errors and dashboard notifications. spree_i18n ships 6.5 MB of locale files, about 60% of them for pages that no longer exist, and pulls in kaminari-i18n (which pulls in the full rails gem), i18n_data (which replaces the global I18n.backend) and spree_extension. This plan makes core call I18n.t directly, deprecates Spree.t as a thin proxy, and moves the translations still in use into core, so every install gets them without an extra gem.

Key Decisions (do not deviate without discussion)

  • Core calls I18n.t with full keys. I18n.t('spree.free'), never I18n.t(:free, scope: :spree). A full key is greppable and lets i18n-tasks match calls to keys, which it cannot do through Spree.t's hidden prefix.
  • Spree.t / Spree.translate stay as a deprecated proxy until 6.1. One line onto I18n.t with the spree scope prepended, plus a Spree::Deprecation warning. No Action View, no HTML. Extensions keep working and see the warning.
  • Every core call converts in the same change. Otherwise each app would see deprecation warnings it cannot act on, caused by Spree itself.
  • Missing keys are plain text in production. Whatever I18n.t returns, never markup.
  • Locale files live in the gem that owns the keys. spree/core, spree/api and spree/emails each ship config/locales/<locale>.yml with the keys their en.yml has — no file carries keys its gem does not use.
  • Only keys that exist in English are copied. A key with no English source is dead by definition. The language name key spree.i18n.this_file_language is kept in every file.
  • rails-i18n becomes a dependency of spree_core. It translates the standard validation messages, dates and numbers that emails and API errors use. It depends only on i18n and railties.
  • i18n_data, kaminari-i18n and spree_extension are not carried over. Localized country names already come from the countries gem (Spree::Country#localized_name); nothing renders Kaminari pagination; the extension loader is Spree 5's.
  • The language-name list moves into core (Spree::Locale#name reads it today through SpreeI18n::Locale), and Spree.available_locales reads the locale files core ships instead of SpreeI18n::Locale.all.
  • The Kaminari shim in Spree::Base is removed. It existed only because spree_i18n loaded Kaminari; Spree paginates with Pagy.
  • spree/testing_support/i18n is removed. It counted translations by wrapping Spree.t, which core no longer calls. i18n-tasks replaces it.
  • Spree's own specs raise on a missing key (2026-10-07). The test app template sets Rails' config.i18n.raise_on_missing_translations = true, so a typo or a key removed too eagerly fails the suite. Chosen over raising in apps' development and test environments, which would let one extension's missing key break a merchant's app, and over i18n-tasks missing alone, which cannot see the ~40 keys built with interpolation. Apps are unaffected.
  • Core turns on English fallback unless the app chose its own (2026-10-07). Every language covers only about 58% of the English keys, so without fallback a German store shows "Translation missing" for the rest in development and test, and in production for apps not built from the starter. The engine sets config.i18n.fallbacks = [:en] before the app's initializers run when the app has not configured it. Rails' generated fallbacks = true (default locale only) becomes [default_locale, :en], since it means "use fallbacks" rather than "never English". An explicit fallback list, or false, still wins — spree_i18n forced true.
  • One English (2026-10-07). Regional English (en-GB, en-AU, en-IN, en-NZ) is not shipped; it falls back to en. Other regional variants keep only what differs from their base language (Design Details).
  • spree_i18n gets an empty 6.0.0 release, then the repository is archived (2026-10-07). 5.3.3 declares spree_core >= 5.4.0.alpha, so it still installs next to 6.0, and its full files would load after core's and win. 6.0.0 depends on spree_core ~> 6.0, ships no locale files and warns on load to remove it, so bundle update alone moves an app off the old files. Archiving without a release was rejected because anyone who misses the upgrade note keeps the old translations, kaminari-i18n and the global I18n.backend swap; refusing to boot was rejected as a hard failure over something harmless to remove.
  • Dead English keys are pruned in the same change (2026-10-07), before the translations are copied, so no dead key is ever moved into core. Accepted trade-off: the checker cannot see every read. In its default mode it treats a key built with interpolation ("spree.promotion_rule_types.#{type}.name") as using the whole family, which is safe. It cannot see a full key passed around as a string ('spree.privacy_policy' handed to a helper), or the names the five one-level readers build ("spree.#{name}"). Removing a key is therefore a reviewed step, not a command; the raise-on-missing spec handler is the safety net.

Design Details

Spree.t

# spree/core/lib/spree/i18n.rb
module Spree
  class << self
    def translate(key, **options)
      Spree::Deprecation.warn('Spree.t is deprecated and will be removed in Spree 6.1. Use I18n.t with the full key, e.g. I18n.t("spree.free").')
      I18n.t(key, **options, scope: [:spree, *options[:scope]])
    end
    alias t translate
  end
end

require 'action_view' and TranslationHelperWrapper go away. What callers lose, on purpose:

View helper behaviour After
Missing key → <span class="translation_missing" title="…">Foo</span> I18n.t's plain text
*_html keys marked HTML-safe Not used from Ruby anywhere in core
Relative .key lookup Only works in a view; already raised

Converting the calls

About 650 call sites (roughly 450 in app/lib, the rest in specs, plus Stripe, EasyPost and four developer docs). A script rewrites the literal forms:

  • Spree.t(:free) → I18n.t('spree.free')
  • Spree.t('api.unauthorized') → I18n.t('spree.api.unauthorized')
  • Spree.t("promotion_rule_types.#{type}.name", default: …) → I18n.t("spree.promotion_rule_types.#{type}.name", default: …)

The ~25 calls passing scope:, the one passing an array of keys and the four without parentheses are converted by hand, folding the scope into the key.

Locale files

For each of the 47 spree_i18n languages, a one-off script loads the language file, keeps the keys that exist in each gem's en.yml, and writes spree/<gem>/config/locales/<locale>.yml. Measured on 2026-10-07: 1,039 of the 1,792 English keys have a real translation in every language (about 97% differ from English), and the trimmed set for all languages is about 2.7 MB against 6.5 MB today. After pruning, the files ship about 1.9 MB across the three gems. The script is not committed — the files are maintained directly from then on.

A translation is copied only when it uses exactly the placeholders (%{amount}) its English text uses: an extra one raises at runtime and a missing one drops information. Plural forms English lacks (few, many) are kept and compared with English's other; misspelt forms are dropped. About 220 translations were left out this way and fall back to English.

Since every language covers only about 58% of the English keys, the rest fall back to English (Key Decisions).

[:en] rather than true: true falls back to the app's default locale, which for a store defaulting to Polish is just as incomplete. Spree also leaves Rails' own keys (number formats, standard validation messages) to rails-i18n and copies none of them, since its versions loaded later would override the correct ones. Spree.available_locales is the shipped languages the app allows and I18n accepts, plus English and the app's default; the countries gem is configured from the configuration alone (Spree.configured_locales), so boot loads no translations. Store setup finds the records it seeded by a stable attribute, never by their translated name: the default stock location by its default flag, store pickup by its pickup provider. Matched by name, a store seeded in one language and set up in another would get duplicates.

Missing keys in Spree's specs

# spree/core/lib/generators/spree/dummy/templates/rails/test.rb
config.i18n.raise_on_missing_translations = true

true, not :strict: Spree's own lookups raise, while model and attribute names keep Rails' humanized fallback.

English fallback

# spree/core/lib/spree/core/engine.rb
initializer 'spree.i18n.fallbacks', before: :load_config_initializers do |app|
  fallbacks = app.config.i18n.fallbacks
  if fallbacks == true
    app.config.i18n.fallbacks = [app.config.i18n.default_locale || :en, :en].uniq
  elsif fallbacks.is_a?(ActiveSupport::OrderedOptions) && fallbacks.empty?
    app.config.i18n.fallbacks = [:en]
  end
end

Pruning dead keys

After the calls use full keys, i18n-tasks unused lists English keys nothing reads. One checker config, spree/core/config/i18n-tasks.yml, covers every Spree gem: it searches all their code and email templates and reads all their locale files, and one spec (spree/core/spec/i18n_spec.rb) runs it. Families read only through a helper the checker cannot follow (the reporting catalog, the permission catalog) are listed under ignore_unused.

Five places build a one-level key from a value, so any spree.<name> may be read through them: coupon result codes, workflow rejection symbols rendered by the API error handler, payment source types, import types and price list match policies. A one-level key is removed only when none of them can produce its name and no code passes it as a string. Measured on 2026-10-07: 361 English keys removed (333 one-level labels from the old admin, 16 nested core keys, 37 API keys including the v2 API's, 9 email keys of the retired shipment mailer). Translations are copied after pruning, so dead keys never reach the other languages.

Regional variants

Regional English (en-GB, en-AU, en-IN, en-NZ) is not shipped (2026-10-07): measured against en, their differences were mostly stale copies ("Taxon" where English now says "Category") or mistakes (an order's status translated as "County"), and a regional request falls back to en. Other variants (de-CH, es-MX, es-CL, es-EC, pt-BR) differ for real and keep only the keys that differ from their base language, which they fall back to.

Loading only the languages an app uses

An app that sets config.i18n.available_locales loads only those languages' Spree files, plus the base language behind each regional one and English: parsing all of them costs about 150 ms and 14 MB per process even when the app then discards them. Spree.available_locales reads the same setting, never the loaded translations, so asking for it does not force every file to load at boot.

Language names

lib/spree_i18n/locales_and_local_language_names.csv (264 rows) moves to spree/core/config/language_names.csv behind a small reader on Spree::Locale. Spree::Locale#name keeps its order: the locale file's spree.i18n.this_file_language, then the list, then the code.

Migration Path

  1. Deprecate Spree.t and convert core. New Spree.t, the conversion script, hand-converted scoped calls, spec updates, docs examples (custom-api-authentication.mdx, checkout.mdx, 5.6-to-6.0.mdx), and raise_on_missing_translations in the test app template.
  2. Prune dead English keys. Reviewed removals of what i18n-tasks unused reports (Design Details → Pruning dead keys).
  3. Move the translations in. Trimmed locale files per gem, the language name list, rails-i18n in spree_core.gemspec, Spree.available_locales and Spree::Locale#name without SpreeI18n, fallbacks.
  4. Remove the leftovers. Kaminari shim in Spree::Base, spree/testing_support/i18n.rb and its CHECK_TRANSLATIONS requires, gem 'spree_i18n' from spree/template.rb, the "install spree_i18n" section of docs/developer/core-concepts/translations.mdx.
  5. Upgrade guide. 5.6-to-6.0.mdx: remove spree_i18n from the Gemfile; Spree.t is deprecated; a missing key no longer renders HTML.
  6. spree-starter. Remove gem 'spree_i18n'.
  7. spree_i18n repository. Release 6.0.0 (depends on spree_core ~> 6.0, no locale files, warns on load), then archive.

Constraints on Current Work

  • Write new translation calls as I18n.t('spree.<key>'), never Spree.t.
  • Never return or build HTML from a translation in Ruby; the API and emails escape their own output.
  • Add new keys to en.yml of the gem that uses them. Other languages fall back to English until translated; never copy English text into another language's file.
  • Do not add translations to the spree_i18n repository; they belong in core.
  • Before removing a key i18n-tasks unused reports, check it is not passed around as a string or reachable through one of the five one-level readers (Design Details → Pruning dead keys). Keys read only through a helper go under ignore_unused in the gem's i18n-tasks.yml.

Open Questions

None — all four were settled on 2026-10-07 (Key Decisions).

References

  • docs/plans/6.0-admin-ui-owns-its-copy.md — the dashboard translates its own text; Ruby text is fallback for the Admin API
  • docs/plans/decisions.md — 2026-09-05 "The dashboard owns every string it shows"
  • spree/core/lib/spree/i18n.rb, spree/core/app/models/spree/locale.rb
  • https://github.com/spree/spree_i18n