GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — GitHub - thadeu/acts_enum_translable: 💎 ActiveRecord::Enum Translate with i18n

BranchesTags
Open more actions menu

Latest commit

 

History

11 Commits
11 Commits

Folders and files

NameName
Last commit message
Last commit date
bin
bin
 
 
doc
doc
 
 
lib
lib
 
 
test
test
 
 
.gitignore
.gitignore
 
 
.rubocop.yml
.rubocop.yml
 
 
Gemfile
Gemfile
 
 
MIT-LICENSE
MIT-LICENSE
 
 
README.md
README.md
 
 
Rakefile
Rakefile
 
 
_config.yml
_config.yml
 
 
acts_enum_translable.gemspec
acts_enum_translable.gemspec
 
 

Repository files navigation

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

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:

gem 'acts_enum_translable'

After:

$ bundle install

Configure enum native Rails

More details, please visit ActiveRecord::Enum

# 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

# 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

Configure Locale English for exemple

# 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

# 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

# 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.

# 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

# 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.

# 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.

About

💎 ActiveRecord::Enum Translate with i18n

Topics

activerecorde-enumenumenum-native-railsenumerableenumsrailsrails4rails5translable

Resources

Readme
MIT license
Activity

Stars

4 stars

Watchers

1 watching

Forks

1 fork
Report repository

Releases

Packages

Used by

Contributors

Languages

You are visitor 01402775 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.