Enumlingo
Enumlingo is a simple gem that helps you translate enum values in your Rails app.
Usage
extend Enumlingo
in your model and call enumlingo with the enums you want to translate.
class Product < ApplicationRecord
extend Enumlingo
enum status: %i[active inactive]
enum kind: %i[book food medical other]
enumlingo :status, :kind
end
define the translations in your locale file
en:
activerecord:
attributes:
product:
statuses:
active: "Active"
inactive: "Inactive"
kinds:
book: "Book"
food: "Food"
medical: "Medical"
other: "Other"
"zh-CN":
activerecord:
attributes:
product:
statuses:
active: "激活"
inactive: "未激活"
kinds:
book: "书籍"
food: "食品"
medical: "医疗"
other: "其他"
Value translation
product = Product.new(status: :active, kind: :book)
product.status_lingo
product.kind_lingo
I18n.locale = "zh-CN"
product.status_lingo
product.kind_lingo
Options translation
Product.statuses_lingo
Product.kinds_lingo
Product.statuses_lingo_values
Product.kinds_lingo_values
Product.status_lingo(:active)
Form translation
<%= form_for @product do |f| %>
<%= f.select :status, Product.statuses_lingo %>
<%= f.select :kind, Product.kinds_lingo %>
...
<% end %>
Installation
Add this line to your application's Gemfile:
gem "enumlingo"
And then execute:
$ bundle
Or install it yourself as:
$ gem install enumlingo
Contributing
Contribution directions go here.
License
The gem is available as open source under the terms of the MIT License.