Skip to content

Commit

Permalink
Fix updating translations
Browse files Browse the repository at this point in the history
  • Loading branch information
mad-eel committed May 29, 2024
1 parent 4f7b341 commit c792bfb
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 8 deletions.
21 changes: 17 additions & 4 deletions app/controllers/concerns/spree/admin/translatable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,26 @@ def save_translation_values
locales_to_update = current_store_locales & params_locales

locales_to_update.each do |locale|
translation = @object.translations.find_or_initialize_by(locale: locale)
translation_params.each do |attribute, translations|
translation.public_send("#{attribute}=", translations[locale])
if update_translation_value?(locale)
translation = @object.translations.find_or_initialize_by(locale: locale)
translation_params.each do |attribute, translations|
translation.public_send("#{attribute}=", translations[locale])
end
translation.save!
else
Mobility.with_locale(locale) do
translation_params.each do |attribute, translations|
@object.public_send("#{attribute}=", translations[locale])
end
@object.save!
end
end
translation.save!
end
end

def update_translation_value?(locale)
Spree.always_use_translations? || I18n.default_locale.to_s != locale
end
end
end
end
56 changes: 52 additions & 4 deletions spec/features/admin/products/edit/translations_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,62 @@
stub_authorization!

let(:store) { Spree::Store.default }
let(:product) { create(:product, stores: [store]) }
let(:product) { create(:product, stores: [store], name: 'Old Product Name EN') }

context 'managing translations' do
context 'when there is more than one locale configured for a store' do
before do
store.update!(default_locale: 'en', supported_locales: 'en,fr')
end

it 'allows an admin to update all translations' do
visit spree.admin_product_path(product)

click_link 'Translations'

fill_in 'translation_name_en', with: 'Product Name EN', fill_options: { clear: :backspace }
fill_in 'translation_name_fr', with: 'Product Name FR'
click_button 'Update'

wait_for_turbo

expect(product.translations.count).to eq(1)

translation_fr = product.translations.find_by!(locale: 'fr')
expect(translation_fr.name).to eq('Product Name FR')
expect(translation_fr.slug).to eq('product-name-fr')

expect(product.reload.name).to eq('Product Name EN')
expect(product.name_en).to eq('Product Name EN')
end

context 'when changing translations after switching store default locale' do
before do
store.update!(default_locale: 'fr')
end

it 'allows an admin to update all translations' do
visit spree.admin_product_path(product.id)

click_link 'Translations'

fill_in 'translation_name_en', with: 'Product Name EN', fill_options: { clear: :backspace }
fill_in 'translation_name_fr', with: 'Product Name FR'
click_button 'Update'

wait_for_turbo

expect(product.translations.count).to eq(1)

translation_fr = product.translations.find_by!(locale: 'fr')
expect(translation_fr.name).to eq('Product Name FR')
expect(translation_fr.slug).to eq('product-name-fr')

expect(product.reload.name).to eq('Product Name EN')
expect(product.name_en).to eq('Product Name EN')
end
end

context 'when there are no translations for a given language' do
let(:new_translation) { 'This is a test French translation' }
let(:expected_new_slug) { 'this-is-a-test-french-translation' }
Expand All @@ -25,7 +73,7 @@

wait_for_turbo

expect(product.translations.count).to eq(2)
expect(product.translations.count).to eq(1)

translation_fr = product.translations.find_by!(locale: 'fr')
expect(translation_fr.name).to eq(new_translation)
Expand Down Expand Up @@ -55,7 +103,7 @@

wait_for_turbo

expect(product.translations.count).to eq(2)
expect(product.translations.count).to eq(1)

translation_fr = product.translations.find_by!(locale: 'fr')
expect(translation_fr.name).to eq(new_name_translation)
Expand All @@ -75,7 +123,7 @@

wait_for_turbo

expect(product.translations.count).to eq(2)
expect(product.translations.count).to eq(1)

translation_fr = product.translations.find_by!(locale: 'fr')
expect(translation_fr.name).to eq(new_name_translation)
Expand Down

0 comments on commit c792bfb

Please sign in to comment.