From 0ae95bcb4c01a3f933d29aaf841118f63b95f870 Mon Sep 17 00:00:00 2001 From: Paul Bob <69730720+Paul-Bob@users.noreply.github.com> Date: Thu, 21 Nov 2024 17:00:07 +0200 Subject: [PATCH 1/2] fix: i18n on resource class (#3450) * reproduction file * theory * rm memoization * test * fix locales generator spec * lint --- lib/avo/resources/base.rb | 4 +--- spec/dummy/config/locales/avo.pt.yml | 5 +++++ .../avo/generators/locales_generator_spec.rb | 22 ++++++++++++++----- spec/features/avo/localization_spec.rb | 11 ++++++++++ 4 files changed, 33 insertions(+), 9 deletions(-) create mode 100644 spec/dummy/config/locales/avo.pt.yml create mode 100644 spec/features/avo/localization_spec.rb diff --git a/lib/avo/resources/base.rb b/lib/avo/resources/base.rb index c51c58f0ff..b8b8cae16e 100644 --- a/lib/avo/resources/base.rb +++ b/lib/avo/resources/base.rb @@ -179,7 +179,7 @@ def translation_key end def name - @name ||= name_from_translation_key(count: 1, default: class_name.underscore.humanize) + name_from_translation_key(count: 1, default: class_name.underscore.humanize) end alias_method :singular_name, :name @@ -203,8 +203,6 @@ def name_from_translation_key(count:, default:) end def underscore_name - return @name if @name.present? - name.demodulize.underscore end diff --git a/spec/dummy/config/locales/avo.pt.yml b/spec/dummy/config/locales/avo.pt.yml new file mode 100644 index 0000000000..ccced052e0 --- /dev/null +++ b/spec/dummy/config/locales/avo.pt.yml @@ -0,0 +1,5 @@ +--- +pt: + avo: + resource_translations: + product: "Produto" diff --git a/spec/features/avo/generators/locales_generator_spec.rb b/spec/features/avo/generators/locales_generator_spec.rb index 492e1f232e..bb2b97d542 100644 --- a/spec/features/avo/generators/locales_generator_spec.rb +++ b/spec/features/avo/generators/locales_generator_spec.rb @@ -3,11 +3,19 @@ RSpec.feature "locales generator", type: :feature do it "generates the files" do - # Backup the en locale - en_locale_backup = Rails.root.join("config", "locales", "avo.en.yml.bak") - FileUtils.cp(Rails.root.join("config", "locales", "avo.en.yml"), en_locale_backup) + # Define locales to backup + backup_locales = %w[en pt] - locales = %w[en fr nn nb pt-BR pt ro tr ar ja es] + # Backup locales + backup_files = {} + backup_locales.each do |locale| + original_file = Rails.root.join("config", "locales", "avo.#{locale}.yml") + backup_file = Rails.root.join("config", "locales", "avo.#{locale}.yml.bak") + FileUtils.cp(original_file, backup_file) if File.exist?(original_file) + backup_files[locale] = {original: original_file, backup: backup_file} + end + + locales = %w[ar de en es fr it ja nb nl nn pl pt-BR pt ro ru tr uk zh] files = locales.map do |locale| Rails.root.join("config", "locales", "avo.#{locale}.yml").to_s @@ -21,7 +29,9 @@ check_files_and_clean_up files - # Restore the en locale - FileUtils.mv(en_locale_backup, Rails.root.join("config", "locales", "avo.en.yml")) + # Restore locales from backup + backup_files.each do |locale, paths| + FileUtils.mv(paths[:backup], paths[:original]) if File.exist?(paths[:backup]) + end end end diff --git a/spec/features/avo/localization_spec.rb b/spec/features/avo/localization_spec.rb new file mode 100644 index 0000000000..d0d59ad491 --- /dev/null +++ b/spec/features/avo/localization_spec.rb @@ -0,0 +1,11 @@ +require "rails_helper" + +RSpec.feature "Localization spec", type: :feature do + describe "force_locale" do + it "translates the resource name on the create button" do + visit avo.resources_products_path(force_locale: :pt) + + expect(page).to have_text "Criar novo produto" + end + end +end From c3134f5a8a7707219fef031ae11ac150c8fd4af1 Mon Sep 17 00:00:00 2001 From: Paul Bob Date: Thu, 21 Nov 2024 17:09:12 +0200 Subject: [PATCH 2/2] Bumped avo to 3.14.2 --- Gemfile.lock | 2 +- gemfiles/rails_6.1_ruby_3.1.4.gemfile.lock | 2 +- gemfiles/rails_6.1_ruby_3.3.0.gemfile.lock | 2 +- gemfiles/rails_7.1_ruby_3.1.4.gemfile.lock | 2 +- gemfiles/rails_7.1_ruby_3.3.0.gemfile.lock | 2 +- gemfiles/rails_7.2.0.beta2_ruby_3.1.4.gemfile.lock | 2 +- gemfiles/rails_7.2.0.beta2_ruby_3.3.0.gemfile.lock | 2 +- gemfiles/rails_8.0_ruby_3.1.4.gemfile.lock | 2 +- gemfiles/rails_8.0_ruby_3.3.0.gemfile.lock | 2 +- lib/avo/version.rb | 2 +- 10 files changed, 10 insertions(+), 10 deletions(-) diff --git a/Gemfile.lock b/Gemfile.lock index 6b0a9517c9..c18acb06f4 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -107,7 +107,7 @@ GIT PATH remote: . specs: - avo (3.14.1) + avo (3.14.2) actionview (>= 6.1) active_link_to activerecord (>= 6.1) diff --git a/gemfiles/rails_6.1_ruby_3.1.4.gemfile.lock b/gemfiles/rails_6.1_ruby_3.1.4.gemfile.lock index 1335b326cb..5f7f327a2b 100644 --- a/gemfiles/rails_6.1_ruby_3.1.4.gemfile.lock +++ b/gemfiles/rails_6.1_ruby_3.1.4.gemfile.lock @@ -13,7 +13,7 @@ PATH PATH remote: .. specs: - avo (3.14.1) + avo (3.14.2) actionview (>= 6.1) active_link_to activerecord (>= 6.1) diff --git a/gemfiles/rails_6.1_ruby_3.3.0.gemfile.lock b/gemfiles/rails_6.1_ruby_3.3.0.gemfile.lock index 6529072f02..e75ed2b5ef 100644 --- a/gemfiles/rails_6.1_ruby_3.3.0.gemfile.lock +++ b/gemfiles/rails_6.1_ruby_3.3.0.gemfile.lock @@ -13,7 +13,7 @@ PATH PATH remote: .. specs: - avo (3.14.1) + avo (3.14.2) actionview (>= 6.1) active_link_to activerecord (>= 6.1) diff --git a/gemfiles/rails_7.1_ruby_3.1.4.gemfile.lock b/gemfiles/rails_7.1_ruby_3.1.4.gemfile.lock index 80c41040be..78e0d5589f 100644 --- a/gemfiles/rails_7.1_ruby_3.1.4.gemfile.lock +++ b/gemfiles/rails_7.1_ruby_3.1.4.gemfile.lock @@ -13,7 +13,7 @@ PATH PATH remote: .. specs: - avo (3.14.1) + avo (3.14.2) actionview (>= 6.1) active_link_to activerecord (>= 6.1) diff --git a/gemfiles/rails_7.1_ruby_3.3.0.gemfile.lock b/gemfiles/rails_7.1_ruby_3.3.0.gemfile.lock index fa760526c7..1d6c320942 100644 --- a/gemfiles/rails_7.1_ruby_3.3.0.gemfile.lock +++ b/gemfiles/rails_7.1_ruby_3.3.0.gemfile.lock @@ -13,7 +13,7 @@ PATH PATH remote: .. specs: - avo (3.14.1) + avo (3.14.2) actionview (>= 6.1) active_link_to activerecord (>= 6.1) diff --git a/gemfiles/rails_7.2.0.beta2_ruby_3.1.4.gemfile.lock b/gemfiles/rails_7.2.0.beta2_ruby_3.1.4.gemfile.lock index e5c60c808b..ab64da212c 100644 --- a/gemfiles/rails_7.2.0.beta2_ruby_3.1.4.gemfile.lock +++ b/gemfiles/rails_7.2.0.beta2_ruby_3.1.4.gemfile.lock @@ -13,7 +13,7 @@ PATH PATH remote: .. specs: - avo (3.14.1) + avo (3.14.2) actionview (>= 6.1) active_link_to activerecord (>= 6.1) diff --git a/gemfiles/rails_7.2.0.beta2_ruby_3.3.0.gemfile.lock b/gemfiles/rails_7.2.0.beta2_ruby_3.3.0.gemfile.lock index e5c60c808b..ab64da212c 100644 --- a/gemfiles/rails_7.2.0.beta2_ruby_3.3.0.gemfile.lock +++ b/gemfiles/rails_7.2.0.beta2_ruby_3.3.0.gemfile.lock @@ -13,7 +13,7 @@ PATH PATH remote: .. specs: - avo (3.14.1) + avo (3.14.2) actionview (>= 6.1) active_link_to activerecord (>= 6.1) diff --git a/gemfiles/rails_8.0_ruby_3.1.4.gemfile.lock b/gemfiles/rails_8.0_ruby_3.1.4.gemfile.lock index 38b8e93a57..a9176473fa 100644 --- a/gemfiles/rails_8.0_ruby_3.1.4.gemfile.lock +++ b/gemfiles/rails_8.0_ruby_3.1.4.gemfile.lock @@ -112,7 +112,7 @@ PATH PATH remote: .. specs: - avo (3.14.1) + avo (3.14.2) actionview (>= 6.1) active_link_to activerecord (>= 6.1) diff --git a/gemfiles/rails_8.0_ruby_3.3.0.gemfile.lock b/gemfiles/rails_8.0_ruby_3.3.0.gemfile.lock index 38b8e93a57..a9176473fa 100644 --- a/gemfiles/rails_8.0_ruby_3.3.0.gemfile.lock +++ b/gemfiles/rails_8.0_ruby_3.3.0.gemfile.lock @@ -112,7 +112,7 @@ PATH PATH remote: .. specs: - avo (3.14.1) + avo (3.14.2) actionview (>= 6.1) active_link_to activerecord (>= 6.1) diff --git a/lib/avo/version.rb b/lib/avo/version.rb index b732c8fea9..7832708a2d 100644 --- a/lib/avo/version.rb +++ b/lib/avo/version.rb @@ -1,3 +1,3 @@ module Avo - VERSION = "3.14.1" unless const_defined?(:VERSION) + VERSION = "3.14.2" unless const_defined?(:VERSION) end