Skip to content
This repository has been archived by the owner on Sep 7, 2024. It is now read-only.

Commit

Permalink
Compound word class names properly underscored
Browse files Browse the repository at this point in the history
  • Loading branch information
dhnaranjo committed Nov 30, 2022
1 parent c57fc99 commit 7534ead
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 3 deletions.
11 changes: 9 additions & 2 deletions lib/phlex/translation.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,15 @@ module ClassMethods
attr_writer :translation_path

def translation_path
@translation_path ||= name&.split("::")&.join(".")&.downcase.to_s
end
@translation_path ||= name
&.split("::")
&.join(".")
&.gsub(/([A-Z]+)([A-Z][a-z])/,'\1_\2')
&.gsub(/([a-z\d])([A-Z])/,'\1_\2')
&.tr("-", "_")
&.downcase.to_s
end

end

def translate(key, **options)
Expand Down
8 changes: 7 additions & 1 deletion test/phlex/translation.rb
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,13 @@ def template
it "returns I18n scope using class anme" do
expect(Views::Articles::Form.translation_path).to be == "views.articles.form"
end
end
end

with "a class name with a multiple word class or module name" do
it "converts the names to snake case" do
expect(Views::Articles::OtherForm.translation_path).to be == "views.articles.other_form"
end
end

with "invalid class_name" do
it "returns empty string" do
Expand Down

0 comments on commit 7534ead

Please sign in to comment.