Skip to content

Commit

Permalink
fix: i18n on resource class (#3450)
Browse files Browse the repository at this point in the history
* reproduction file

* theory

* rm memoization

* test

* fix locales generator spec

* lint
  • Loading branch information
Paul-Bob authored Nov 21, 2024
1 parent 5da7a10 commit 0ae95bc
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 9 deletions.
4 changes: 1 addition & 3 deletions lib/avo/resources/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -203,8 +203,6 @@ def name_from_translation_key(count:, default:)
end

def underscore_name
return @name if @name.present?

name.demodulize.underscore
end

Expand Down
5 changes: 5 additions & 0 deletions spec/dummy/config/locales/avo.pt.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
pt:
avo:
resource_translations:
product: "Produto"
22 changes: 16 additions & 6 deletions spec/features/avo/generators/locales_generator_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
11 changes: 11 additions & 0 deletions spec/features/avo/localization_spec.rb
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 0ae95bc

Please sign in to comment.