diff --git a/README.md b/README.md index f629124..5db5828 100644 --- a/README.md +++ b/README.md @@ -69,6 +69,10 @@ Product.statuses_lingo # => [["Active", "active"], ["Inactive", "inactive"]] Product.kinds_lingo # => [["Book", "book"], ["Food", "food"], ["Medical", "medical"], ["Other", "other"]] ``` +```ruby +Product.status_lingo(:active) # => "Active" +``` + ### Form translation ```ruby diff --git a/lib/enumlingo/helper.rb b/lib/enumlingo/helper.rb index 6c351b3..167bedc 100644 --- a/lib/enumlingo/helper.rb +++ b/lib/enumlingo/helper.rb @@ -14,6 +14,10 @@ def enumlingo(*attributes) [I18n.t("activerecord.attributes.#{model_name.i18n_key}.#{pluralized_attribute}.#{key}"), key] end end + + define_singleton_method "#{attribute}_lingo" do |key| + I18n.t("activerecord.attributes.#{model_name.i18n_key}.#{pluralized_attribute}.#{key}") + end end end end diff --git a/test/enumlingo_test.rb b/test/enumlingo_test.rb index 91b97f8..c056c16 100644 --- a/test/enumlingo_test.rb +++ b/test/enumlingo_test.rb @@ -34,4 +34,14 @@ class EnumlingoTest < ActiveSupport::TestCase I18n.locale = "zh-CN" assert_equal [["书籍", "book"], ["食品", "food"], ["药品", "medical"], ["Translation missing: zh-CN.activerecord.attributes.product.kinds.other", "other"]], Product.kinds_lingo end + + test "it return text of product kind" do + I18n.locale = "en" + assert_equal "Book", Product.kind_lingo(:book) + end + + test "it return text of product kind in Chinese" do + I18n.locale = "zh-CN" + assert_equal "书籍", Product.kind_lingo(:book) + end end