Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 4 additions & 0 deletions lib/enumlingo/helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
10 changes: 10 additions & 0 deletions test/enumlingo_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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