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.

15.0 KB
Raw Blame

Admin UI owns its copy

Status: Implemented — all four migration steps shipped for 6.0 Target: Spree 6.0 Depends on: 6.0-admin-spa.md (i18n conventions, mapSpreeErrorsToForm) Author: Damian Legawiec Last updated: 2026-09-05

Summary

The React dashboard renders two kinds of human text it did not write: the names and descriptions of pluggable types (price rules, promotion rules and actions, calculators, collection rules, routing rules, delivery method rules, commission rules, seller requirement kinds, integrations, the permission catalog) and the messages inside 422 responses. Both are produced in Ruby through Spree.t, in whatever locale the request resolved to, which the dashboard never sets. A merchant using the dashboard in Polish on a store whose default locale is English sees Polish chrome around English rule names, and the reverse on a Polish store. Fixing one type at a time (PR #14575 added en.yml keys for VolumeRule) keeps the split alive. This plan moves every user-visible string the dashboard shows into the dashboard's own locale files, and turns the Admin API's contribution into stable machine codes plus a plain fallback. The Store API is not touched.

Key Decisions (do not deviate without discussion)

  • The Admin API sends codes, the dashboard renders words. Type catalogs and record serializers keep a stable snake_case type; validation responses carry an error code per attribute with its interpolation parameters. Nothing in packages/dashboard* prints a Ruby-generated string when it has a key for the code.
  • Ruby keeps a human fallback, never the source of truth. label, description and message stay on the wire for integrations, curl, and extension types that ship no dashboard translation. They are documented as fallback text in the request locale, not as UI copy, and the dashboard uses them only when i18n.exists() is false for the code.
  • The Store API is out of scope. The error handler is shared by both halves, so the richer validation shape is added in the Admin base controller, not in Spree::Api::V3::ErrorHandler. Store responses stay byte-identical.
  • One helper, one key scheme, shared by both panels. typeLabel and typeDescription move from packages/dashboard/src/lib/type-labels.ts into @spree/dashboard-core, keyed as admin.types.<family>.<code>.{name,description}. The Seller Panel imports the same helper (dashboard is the reference, per the general rules).
  • UI language and content locale are different things. The dashboard keeps not sending x-spree-locale for its UI language. That header selects which Mobility translation of a product name the merchant reads and edits; binding it to the interface language would change the data a Polish admin edits on an English store. Content locale stays an explicit picker.
  • Type catalogs keep label/description as a documented fallback (2026-09-05). Dropping them would force every extension gem that ships a Ruby type to also ship a dashboard plugin with locales, or merchants read a raw code. Keeping them costs a few bytes per catalog entry and gives the migration itself a safety net. Built-in types still lose their Ruby copy methods and en.yml keys where those exist only to feed the wire; the fallback then resolves through human_name.
  • reject! takes the code as its first argument (2026-09-05): reject!(:not_awaiting_review), reject!(:below_minimum, count: 5). This reuses errors.add(:base, symbol, **params) unchanged, so Rails resolves the message from en.yml and records the code in errors.details with no second signature to keep in sync. Strings keep working, so conversion is incremental and no call site breaks.
  • Every core error call site converts in this run (2026-09-05). All errors.add and reject! sites carrying a preformatted string or a Spree.t/I18n.t message gain a symbolic code, so no message-only fallback path remains in core. Specs assert on errors.details codes rather than English sentences.
  • The code is added alongside the message, not instead of it (2026-09-05, found while implementing). Spree's error copy lives under en.spree.errors.messages, but a bare errors.add(:base, :some_code) resolves against Rails' top-level en.errors.messages and its activerecord.errors.* fallbacks — none of which Spree populates. Dropping the message would therefore replace every sentence with "Translation missing". The conversion is errors.add(:attribute, :code, message: Spree.t('errors.messages.code')): errors.details gains the code the dashboard translates, errors.messages keeps the text integrations already read, and interpolated messages keep their values. Moving the copy into Rails' scope was rejected — it would relocate hundreds of keys out of the spree: namespace that every extension's Spree.t call depends on.
  • Shared type-label and validation keys live in @spree/dashboard-core (2026-09-05), which already ships all six locale files. The Seller Panel imports the helper and inherits every language without duplicating locale files into packages/seller-dashboard.
  • Validation codes come from Rails, not from a new vocabulary. errors.add(:attribute, :symbol, **params) already yields errors.details; workflows already route reject! through ActiveModel::Errors and define model_name for exactly this. The work is converting string and Spree.t call sites to symbols, not inventing a parallel error registry.

Design Details

Type catalogs

Spree::PreferenceSchema.subclasses_with_preference_schema and the per-family discovery actions (price_rule_types, rule_types, calculator lists, seller requirement kinds) keep returning { type, label, description, preference_schema }. label and description are renamed nowhere and removed nowhere; their OpenAPI descriptions change to "English fallback for clients without their own translation of type". The record serializers (PriceRuleSerializer, PromotionRuleSerializer, PromotionActionSerializer and its nested calculator, CommissionRuleSerializer, DeliveryMethodRuleSerializer, OrderRoutingRuleSerializer, SellerRequirementSerializer, PermissionSerializer) drop the per-row label/description/name copies. A row is rendered from its type and the catalog the page already fetched, which is what the promotion editor does today. DeliveryMethodRuleSerializer#name goes as well: it currently reuses the attribute name a merchant would expect to be their own text.

Ruby loses the class-level copy methods that exist only for the wire: Spree::Calculator.description literals, the human_description / description pairs on rule base classes, and the price_rules.*, promotion_rule_types.*-style keys in spree/core/config/locales/en.yml. human_name stays as the fallback source and for console and log use. Payment method and integration display_name are brand names, not copy, and stay where they are.

Dashboard type labels

@spree/dashboard-core exports typeLabel(family, code, fallback) and typeDescription(family, code, fallback). Families:

Family Today Keys to add
promotion_rule 11 keys under admin.promotions.rule_types move
promotion_action 4 keys under admin.promotions.action_types move
calculator 11 name-only keys under admin.calculators move, add descriptions
order_routing_rule 3 keys move
collection_rule 3 keys move
price_rule none 6
commission_rule none 4
delivery_method_rule none 4
seller_requirement names only under admin.seller_requirements.kinds move, add descriptions
integration none per installed provider gem
permission none resource labels, descriptions and group labels

Every key lands in all six locale files of both packages. Consumers that still print the API string, and switch to the helper: price-list-form.tsx, settings/commission-rates.tsx, settings/seller-requirements.tsx, delivery-method-form.tsx, the seller panel delivery-methods.tsx, configure-integration-sheet.tsx, settings/integrations.tsx, permission-picker.tsx, calculator-summary.tsx in dashboard-ui (which takes the resolved label as a prop, since dashboard-ui is headless).

Extensions that add a Ruby type register their copy with the dashboard plugin they already ship for the editor form. defineDashboardPlugin gains a locales contribution merged into i18next at boot; a plugin without one falls back to the API label, which is the same experience as today.

Validation responses

Spree::Api::V3::Admin base overrides format_validation_details so an Admin 422 reads:

{
  "error": {
    "code": "validation_error",
    "message": "Name can't be blank, Quantity must be greater than 0",
    "details": {
      "name": [{ "code": "blank", "message": "can't be blank" }],
      "quantity": [{ "code": "greater_than", "message": "must be greater than 0", "count": 0 }]
    }
  }
}

code is the symbol from errors.details; extra keys are the interpolation options Rails recorded (count, value). A string added with errors.add(:base, "text") has no symbol and serializes with code: null, so the dashboard falls back to message. message is still the request locale's Rails translation, which keeps curl and integrations readable.

mapSpreeErrorsToForm in dashboard-core resolves each entry as admin.validation.codes.<attribute>.<code> then admin.validation.codes.<code>, with message as the last resort, and interpolates the extra keys. The root summary is rebuilt from the translated field messages instead of the server sentence when every entry translated; otherwise the server message stays.

Each entry also carries specific, which says whether the model gave that code its own wording. A code like invalid is generic and the client should prefer its own translation, but the webhook URL validation reports invalid and answers "must be a valid http or https URL" — translating the code there would throw away the only part that tells the merchant what to do. Only the server can tell the two apart, because the comparison is against the code's default in the locale the message was resolved in; a client would have to recognise that default in every language a store might trade in.

The codes namespace is deliberate: admin.validation.* already holds the client-side keys the forms' own Zod schemas read (required, min_length, invalid_email, …), and Rails' required (a missing belongs_to) means something different from the client's (an empty input). Sharing one namespace put "must exist" under every required field in the dashboard.

Rails' built-in codes (blank, taken, invalid, too_long, greater_than, inclusion, and the rest of ActiveModel::Errors) get one key each in admin.validation.codes. Custom codes get keys as their call sites are converted: about 84 errors.add sites with string or Spree.t messages, 45 workflow reject! calls that pass Spree.t or I18n.t text, and 6 literal English rejections in core become symbols with parameters. The en.yml entries they used to read stay, because the fallback message still resolves through them; only the dashboard stops depending on them.

Not moving

Store API serializers, controllers and error bodies. Transactional email copy. Anything the Store API exposes to shoppers. display_* money formatting, which is number formatting rather than copy.

Migration Path

All four steps shipped together for 6.0.

  1. Dashboard type labels (done). The helper moved into @spree/dashboard-core under one key scheme (admin.types.<family>.<code>.{name,description}), covering 60 type codes across nine families in all six locales. Every consumer that printed the API string now resolves the code. Two promotion rules (channel, market) and eight permission catalog resources that had silently fallen through to English were filled in at the same time.
  2. Admin validation shape (done). Spree::Api::V3::Admin::ValidationDetails overrides format_validation_details on both admin branches, so each entry reports { code, message, …interpolation }. mapSpreeErrorsToForm translates the code and falls back to the message. Store responses are unchanged, which a spec asserts directly.
  3. Core error codes (done). All 94 preformatted errors.add and reject! sites across core and the Stripe provider gained a symbolic code, with seven new en.yml keys for the messages that had none. Wording is byte-identical to what shipped before.
  4. Serializer shrink (done). The per-row label/description/name copies are gone from all seven rule and action serializers, along with the nested calculator's label. The type catalogs keep theirs as the documented fallback. defineDashboardPlugin gained a locales contribution so a gem registering a Ruby type has somewhere to put its copy.

Constraints on Current Work

  • A new pluggable type (rule, action, calculator, requirement kind) lands with its admin.types.<family>.<code>.{name,description} keys in every dashboard locale file. Do not add a Spree.t label for it in en.yml; the API label may keep reading human_name as the fallback.
  • A dashboard page that renders a type picker or a rule row calls typeLabel / typeDescription; never print entry.label, type.name or rule.description directly. The one exception is an integration's name, which is a brand (Stripe, EasyPost) and is not translated — its description is prose and does go through the helper.
  • New validation errors in core use a symbol and parameters (errors.add(:quantity, :greater_than, count: 0), reject!(:not_awaiting_review)), never a preformatted string, and the dashboard key for that code is added in the same PR.
  • Do not send the dashboard's UI language as x-spree-locale to the Admin API. That header means content locale.
  • Do not touch Spree::Api::V3::ErrorHandler or any Store serializer for this work.
  • Shared admin copy (type families, validation codes) goes in packages/dashboard-core/src/locales/*.json in all six languages, never in packages/seller-dashboard/src/locales/. The seller panel imports the helper rather than keeping its own copy of the keys.

Open Questions

None. The two questions this plan opened with (whether to keep the catalog label/description fallback, and what shape reject! should take) were settled on 2026-09-05 and moved into Key Decisions above.

References

  • PR #14575 — the per-type en.yml fix this plan replaces as an approach
  • packages/dashboard/src/lib/type-labels.ts — the existing partial helper
  • packages/dashboard-core/src/lib/form-errors.ts — mapSpreeErrorsToForm
  • spree/api/app/controllers/concerns/spree/api/v3/error_handler.rb — shared 422 rendering
  • spree/core/app/models/concerns/spree/preference_schema.rb — type catalog discovery
  • docs/plans/6.0-admin-spa.md — "Forms & i18n"