https://github.com/thadeu/acts_enum_translable
💎 ActiveRecord::Enum Translate with i18n
https://github.com/thadeu/acts_enum_translable
activerecorde-enum enum enum-native-rails enumerable enums rails rails4 rails5 translable
Last synced: 8 months ago
JSON representation
💎 ActiveRecord::Enum Translate with i18n
- Host: GitHub
- URL: https://github.com/thadeu/acts_enum_translable
- Owner: thadeu
- License: mit
- Created: 2017-04-16T02:55:59.000Z (about 9 years ago)
- Default Branch: master
- Last Pushed: 2017-04-18T12:32:09.000Z (about 9 years ago)
- Last Synced: 2025-07-01T06:09:17.738Z (12 months ago)
- Topics: activerecorde-enum, enum, enum-native-rails, enumerable, enums, rails, rails4, rails5, translable
- Language: Ruby
- Size: 122 KB
- Stars: 3
- Watchers: 1
- Forks: 1
- Open Issues: 3
-
Metadata Files:
- Readme: README.md
- License: MIT-LICENSE
Awesome Lists containing this project
README
# ActsEnumTranslable
ActsEnumTranslable convention translate enums with .yml file.
## Pull Request Ruby on Rails
I hope this functionality is accepted in the pull request I made in the rails project. If it is accepted, we will soon have a merge for rails 5.x. [Pull Request in to Rails 5.x](https://github.com/rails/rails/pull/28787)
## Usage
* Install Gem
* Configure enum native Rails
* Add acts_enum_translable in Model class
* Configure active_record attributes in file .yml
* Use methods
## Install Gem for Ruby on Rails
Add this line to your application's Gemfile:
```ruby
gem 'acts_enum_translable'
```
After:
```bash
$ bundle install
```
## Configure enum native Rails
More details, please visit [ActiveRecord::Enum](http://api.rubyonrails.org/classes/ActiveRecord/Enum.html)
```ruby
# example model
# user.rb
class User < ApplicationRecord
enum status: { inactive: 0, active: 1, blocked: 2 }
enum visible: [:hide, :show]
end
```
## Add acts_enum_translable in Model class
Add in Model for inherit methods
```ruby
# exemple model
# user.rb
class User < ApplicationRecord
enum status: { inactive: 0, active: 1, blocked: 2 }
enum visible: [:hide, :show]
# Add in Model for inherit methods
acts_enum_translable
end
```
## Configure active_record attributes in file .yml
More details, please visit section [4.5 Translations for Active Record Models](http://guides.rubyonrails.org/i18n.html#translations-for-active-record-models)
Configure *Locale English* for exemple
```ruby
# config/locales/en.activerecord.yml
en:
activerecord:
attributes:
users:
status:
inactive: "Inactive"
active: "Active"
blocked: "Blocked"
visible:
hide: 'Hide'
show: 'Show'
```
Configure *Locale Portuguese* for exemple
```ruby
# config/locales/pt.activerecord.yml
pt:
activerecord:
attributes:
users:
status:
inactive: "Inativo"
active: "Ativo"
blocked: "Bloqueado"
visible:
hide: 'Oculto'
show: 'VisÃvel'
```
## Use methods sufix with _translable for enums key
```ruby
# print status
user = User.new(status: :blocked, visible: :show)
user.status_translable
# english: Blocked
# portuguese: Bloqueado
# print visible
user.visible_translable
# english: Show
# portuguese: VisÃvel
```
## Use methods in Model class get enums for key
_Required key param._
```ruby
# return Array with Values
# in english
# I18n.locale = :en
User.enum_list(:status)
# => ["Inactive", "Active", "Blocked"]
# in portuguese
# I18n.locale = :pt
User.enum_list(:status)
# => ["Inativo", "Ativo", "Bloqueado"]
```
Now return array with values and keys or reverse
```ruby
# return Arrary with par [value, key]
# in english
# I18n.locale = :en
User.enum_with_keys(:status)
# => [["Inactive", 0], ["Active", 1], ["Blocked", 2]]
# in portuguese
# I18n.locale = :pt
User.enum_with_keys(:status)
# => [["Inativo", 0], ["Ativo", 1], ["Bloqueado", 2]]
# return par array [key, value]
User.enum_form(:status)
# => [[0, "Inativo"], [1, "Ativo"], [2, "Bloqueado"]]
```
### Use SimpleForm too
Easy return enums translated.
```ruby
# with I18n.locale = :pt or :en
<%= f.select :status, User.enum_form(:status), include_blank: true %>
```
## Contributing
We have a long list of valued contributors. Check them all at: https://github.com/Thadeu/acts_enum_translable.
## License
The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).