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.
Ruby b2b-commercee-commerceecommerceecommerce-apiecommerce-frameworkecommerce-platformheadlessheadless-commerceheadless-ecommercemarketplacemulti-tenantmulti-vendormulti-vendor-ecommerceopen-sourcespree-commerce
FIX: admin - normalize translation locale to handle compound locales (fixes #13150) (#13190)
Ahmede52c98374ae1
diff --git a/admin/app/controllers/spree/admin/translations_controller.rb b/admin/app/controllers/spree/admin/translations_controller.rbindex c11bfd15f1..493c8a8e38 100644--- a/admin/app/controllers/spree/admin/translations_controller.rb+++ b/admin/app/controllers/spree/admin/translations_controller.rb@@ -1,14 +1,25 @@module Spreemodule Adminclass TranslationsController < Spree::Admin::BaseController+ # Set the resource being translated and any related databefore_action :set_resource, only: [:edit, :update]before_action :load_data, only: [:edit, :update]before_action :set_translation_locale, only: [:edit, :update]-+ helper_method :normalized_localehelper_method :resource_class+ # GET /admin/translations/:id/edit+ # Renders the edit translation page for the resourcedef edit; end+ # Normalizes a locale string by replacing '-' with '_' and downcasing+ def normalized_locale(locale)+ locale.to_s.downcase.tr('-', '_')+ end++ # PUT /admin/translations/:id+ # Updates the translations for the resource+ # Sets flash messages for success or failuredef updateif @resource.update(permitted_translation_params)flash.now[:success] = flash_message_for(@resource, :successfully_updated)@@ -32,10 +43,12 @@ module Spreeendend+ # Build translation field names with normalized locale suffixdef translation_fields(klass)- klass.translatable_fields.map { |field| "#{field}_#{@selected_translation_locale}" }+ klass.translatable_fields.map { |field| "#{field}_#{normalized_locale(@selected_translation_locale)}" }end+ # Determine the class of the resourcedef resource_class@resource_class ||= beginklass = params[:resource_type]@@ -44,10 +57,12 @@ module Spreeendend+ # Allowed translatable resources configured in Spreedef allowed_resource_classRails.application.config.spree.translatable_resourcesend+ # Set the resource object using friendly find if availabledef set_resource@resource = if resource_class.respond_to?(:friendly)resource_class.friendly.find(params[:id])@@ -56,10 +71,17 @@ module Spreeendend+ # Determine the translation locale to usedef set_translation_locale- @selected_translation_locale = params[:translation_locale].presence || @locales&.first || current_store.supported_locales_list.first+ raw_locale = params[:translation_locale].presence ||+ @locales&.first ||+ current_store.default_locale ||+ current_store.supported_locales_list.first++ @selected_translation_locale = normalized_locale(raw_locale)end+ # Load available locales for this resource, excluding defaultdef load_data@locales = (current_store.supported_locales_list - [@default_locale]).sortenddiff --git a/admin/app/views/spree/admin/translations/edit.html.erb b/admin/app/views/spree/admin/translations/edit.html.erbindex 71e353a87f..3903239c89 100644--- a/admin/app/views/spree/admin/translations/edit.html.erb+++ b/admin/app/views/spree/admin/translations/edit.html.erb@@ -5,10 +5,12 @@<div class="drawer-body"><ul class="nav nav-pills mb-3"><% @locales.each do |locale| %>+ <% normalized = normalized_locale(locale) %><%= nav_item(- Spree.t('i18n.this_file_language', locale: locale),- spree.edit_admin_translation_path(@resource, resource_type: resource_class.to_s, translation_locale: locale),- active: (locale == @selected_translation_locale)+ Spree.t('i18n.this_file_language', locale: locale),+ spree.edit_admin_translation_path(@resource, resource_type: resource_class.to_s, translation_locale: locale),+ active: (normalized == @selected_translation_locale)+) %><% end %></ul>diff --git a/admin/spec/controllers/spree/admin/translations_controller_spec.rb b/admin/spec/controllers/spree/admin/translations_controller_spec.rbindex 0e8180ab08..970c6168dc 100644--- a/admin/spec/controllers/spree/admin/translations_controller_spec.rb+++ b/admin/spec/controllers/spree/admin/translations_controller_spec.rb@@ -86,4 +86,32 @@ RSpec.describe Spree::Admin::TranslationsController, type: :controller doendendend++ describe 'Spree::Admin::TranslationsController translation_fields and normalized_locale' do+ let(:controller_instance) { described_class.new }+ let(:product_class) { Spree::Product }++ describe '#normalized_locale' do+ it 'converts en-GB to en_gb' do+ expect(controller_instance.normalized_locale('en-GB')).to eq('en_gb')+ end++ it 'converts fr to fr' do+ expect(controller_instance.normalized_locale('fr')).to eq('fr')+ end+ end++ describe '#translation_fields' do+ before do+ controller_instance.instance_variable_set(:@selected_translation_locale, 'en_gb')+ allow(product_class).to receive(:translatable_fields).and_return(['name', 'description'])+ end++ it 'returns fields with normalized locale suffix' do+ fields = controller_instance.send(:translation_fields, product_class)+ expect(fields).to eq(['name_en_gb', 'description_en_gb'])+ end+ end+ end+end