Skip to content

Commit e886ad8

Browse files
authored
Merge pull request #101 from davidalejandroaguilar/davidalejandroaguilar/t-method-lazy-lookup
Allow "lazy" lookup of translations (`t(".my_key")`) and handle missing keys gracefully (Instead of replacing with `Translation missing:`)
2 parents 71db8cc + 2f39311 commit e886ad8

File tree

1 file changed

+19
-2
lines changed

1 file changed

+19
-2
lines changed

lib/phlex/rails/helpers.rb

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1156,8 +1156,25 @@ module SubmitTag
11561156
module T
11571157
extend Phlex::Rails::HelperMacros
11581158

1159-
# @!method t(...)
1160-
define_value_helper :t
1159+
def self.included(base)
1160+
base.extend(ClassMethods)
1161+
end
1162+
1163+
module ClassMethods
1164+
def translation_path
1165+
@translation_path ||= name&.dup.tap do |n|
1166+
n.gsub!("::", ".")
1167+
n.gsub!(/([a-z])([A-Z])/, '\1_\2')
1168+
n.downcase!
1169+
end
1170+
end
1171+
end
1172+
1173+
def t(key, **options)
1174+
key = "#{self.class.translation_path}#{key}" if key.start_with?(".")
1175+
1176+
helpers.t(key, **options)
1177+
end
11611178
end
11621179

11631180
module TelephoneField

0 commit comments

Comments
 (0)