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
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,13 @@ product.kind_lingo # => "书籍"
### Options translation

```ruby
# Translate options with key
Product.statuses_lingo # => [["Active", "active"], ["Inactive", "inactive"]]
Product.kinds_lingo # => [["Book", "book"], ["Food", "food"], ["Medical", "medical"], ["Other", "other"]]

# Translate options with value
Product.statuses_lingo_values # => [["Active", 0], ["Inactive", 1]]
Product.kinds_lingo_values # => [["Book", 0], ["Food", 1], ["Medical", 2], ["Other", 3]]
```

```ruby
Expand Down
8 changes: 7 additions & 1 deletion lib/enumlingo/helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ def enumlingo(*attributes)
I18n.t("activerecord.attributes.#{model_name.i18n_key}.#{pluralized_attribute}.#{value}")
end

define_singleton_method "#{attribute.to_s.pluralize}_lingo" do
define_singleton_method "#{pluralized_attribute}_lingo" do
send(attribute.to_s.pluralize).map do |key, value|
[I18n.t("activerecord.attributes.#{model_name.i18n_key}.#{pluralized_attribute}.#{key}"), key]
end
Expand All @@ -18,6 +18,12 @@ def enumlingo(*attributes)
define_singleton_method "#{attribute}_lingo" do |key|
I18n.t("activerecord.attributes.#{model_name.i18n_key}.#{pluralized_attribute}.#{key}")
end

define_singleton_method "#{pluralized_attribute}_lingo_values" do |locale = I18n.locale|
send(attribute.to_s.pluralize).map do |key, value|
[I18n.t("activerecord.attributes.#{model_name.i18n_key}.#{pluralized_attribute}.#{key}", locale: locale), value]
end
end
end
end
end
4 changes: 4 additions & 0 deletions test/enumlingo_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -44,4 +44,8 @@ class EnumlingoTest < ActiveSupport::TestCase
I18n.locale = "zh-CN"
assert_equal "书籍", Product.kind_lingo(:book)
end

test "return options of product kind value with custom translation" do
assert_equal [["Book", 0], ["Food", 1], ["Medical", 2], ["Other", 3]], Product.kinds_lingo_values(:en)
end
end