How to use i18n with Rails 4 listings

Rails 4 Active Record Enums are great, but what is the right template for translating from i18n?

+56
enums ruby-on-rails-4
Apr 03 '14 at 3:25
source share
16 answers

I did not find a specific template either, so I just added:

en: user_status: active: Active pending: Pending... archived: Archived 

to an arbitrary .yml file. Then in my views:

 I18n.t :"user_status.#{user.status}" 
+35
Apr 16 '14 at 9:09
source share

Starting with Rails 5, all models inherit from ApplicationRecord .

 class User < ApplicationRecord enum status: [:active, :pending, :archived] end 

I use this superclass to implement a general enumeration translation solution:

 class ApplicationRecord < ActiveRecord::Base self.abstract_class = true def self.human_enum_name(enum_name, enum_value) I18n.t("activerecord.attributes.#{model_name.i18n_key}.#{enum_name.to_s.pluralize}.#{enum_value}") end end 

Then I add translations to my .yml file:

 en: activerecord: attributes: user: statuses: active: "Active" pending: "Pending" archived: "Archived" 

Finally, to get the translation, I use:

 User.human_enum_name(:status, :pending) => "Pending" 
+36
Mar 31 '16 at 13:58
source share

Here is a view:

 select_tag :gender, options_for_select(Profile.gender_attributes_for_select) 

Here is a model (you can move this code to an assistant or decorator actually)

 class Profile < ActiveRecord::Base enum gender: {male: 1, female: 2, trans: 3} # @return [Array<Array>] def self.gender_attributes_for_select genders.map do |gender, _| [I18n.t("activerecord.attributes.#{model_name.i18n_key}.genders.#{gender}"), gender] end end end 

And here is the locale file:

 en: activerecord: attributes: profile: genders: male: Male female: Female trans: Trans 
+26
May 17 '14 at 10:31
source share

To make internationalization look like any other attribute, I followed the nested attribute, as you can see here .

If you have a User class:

 class User < ActiveRecord::Base enum role: [ :teacher, :coordinator ] end 

And a yml like this:

 pt-BR: activerecord: attributes: user/role: # You need to nest the values under model_name/attribute_name coordinator: Coordenador teacher: Professor 

You can use:

 User.human_attribute_name("role.#{@user.role}") 
+13
May 20, '15 at 23:19
source share

Model:

 enum stage: { starting: 1, course: 2, ending: 3 } def self.i18n_stages(hash = {}) stages.keys.each { |key| hash[I18n.t("checkpoint_stages.#{key}")] = key } hash end 

Language:

 checkpoint_stages: starting: Saída course: Percurso ending: Chegada 

And in appearance (.slim):

 = f.input_field :stage, collection: Checkpoint.i18n_stages, as: :radio_buttons 
+7
Jul 16 '14 at 14:52
source share

I created a stone for this.

http://rubygems.org/gems/translated_attribute_value

Add to your gemfile:

 gem 'translated_attribute_value' 

If you have a status field for the user:

 pt-BR: activerecord: attributes: user: status_translation: value1: 'Translation for value1' value2: 'Translation for value2' 

And, in your opinion, you can do this:

 user.status_translated 

It works with an active record, mongoid, or any other class with getter / seters:

https://github.com/viniciusoyama/translated_attribute_value

+3
Sep 16 '14 at 18:11
source share

When developing user3647358's answer, you can very accurately understand what you are used to when translating attribute names.

Locale File:

 en: activerecord: attributes: profile: genders: male: Male female: Female trans: Trans 

Translate by calling I18n # t:

 profile = Profile.first I18n.t(profile.gender, scope: [:activerecord, :attributes, :profile, :genders]) 
+3
Sep 17 '14 at 19:35
source share

By combining the answers of Repolês and Aliaksandr , for Rails 5 we can build 2 methods that allow you to translate a single value or set of values ​​from the enum attribute.

Set up translations in the .yml file:

 en: activerecord: attributes: user: statuses: active: "Active" pending: "Pending" archived: "Archived" 

In the ApplicationRecord class, from which all models are inherited, we define a method that processes translations for one value, and another that processes arrays by calling it:

 class ApplicationRecord < ActiveRecord::Base self.abstract_class = true def self.translate_enum_name(enum_name, enum_value) I18n.t("activerecord.attributes.#{model_name.i18n_key}.#{enum_name.to_s.pluralize}.#{enum_value}") end def self.translate_enum_collection(enum_name) enum_values = self.send(enum_name.to_s.pluralize).keys enum_values.map do |enum_value| self.translate_enum_name enum_name, enum_value end end end 

In our representations, we can then translate the individual values:

 <p>User Status: <%= User.translate_enum_name :status, @user.status %></p> 

Or the whole set of enumeration values:

 <%= f.select(:status, User.translate_enum_collection :status) %> 
+3
Feb 24 '17 at 18:42
source share

Try enum_help gem. From the description:

ActiveRecord :: Enum help for working with I18n and simple_form.

+2
Apr 24 '15 at 13:18
source share

Heres a t_enum helper method that I use.

 <%= t_enum(@user, :status) %> 

enum_helper.rb:

 module EnumHelper def t_enum(inst, enum) value = inst.send(enum); t_enum_class(inst.class, enum, value) end def t_enum_class(klass, enum, value) unless value.blank? I18n.t("activerecord.enums.#{klass.to_s.demodulize.underscore}.#{enum}.#{value}") end end end 

user.rb:

 class User < ActiveRecord::Base enum status: [:active, :pending, :archived] end 

en.yml:

 en: activerecord: enums: user: status: active: "Active" pending: "Pending..." archived: "Archived" 
+2
Feb 25 '16 at 18:00
source share

Model:

 class User < ActiveRecord::Base enum role: [:master, :apprentice] end 

Locale File:

 en: activerecord: attributes: user: master: Master apprentice: Apprentice 

Using:

 User.human_attribute_name(:master) # => Master User.human_attribute_name(:apprentice) # => Apprentice 
+1
Sep 30 '16 at 0:22
source share

I prefer a simple helper in application_helper

  def translate_enum(object, enum_name) I18n.t("activerecord.attributes.#{object.model_name.i18n_key}.#{enum_name.to_s.pluralize}.#{object.send(enum_name)}") end 

Then in my YML file:

 fr: activerecord: attributes: my_model: my_enum_plural: pending: "En cours" accepted: "Accepté" refused: "Refusé" 
+1
May 10 '17 at 8:28
source share

Another way, I find it more convenient, using the problem in models

Concern:

 module EnumTranslation extend ActiveSupport::Concern def t_enum(enum) I18n.t "activerecord.attributes.#{self.class.name.underscore}.enums.#{enum}.#{self.send(enum)}" end end 

YML:

 fr: activerecord: attributes: campaign: title: Titre short_description: Description courte enums: status: failed: "Echec" 

View:

 <% @campaigns.each do |c| %> <%= c.t_enum("status") %> <% end %> 

Remember to add worry to your model:

 class Campaign < ActiveRecord::Base include EnumTranslation enum status: [:designed, :created, :active, :failed, :success] end 
0
Aug 31 '16 at 4:49
source share

You can simply add an assistant:

 def my_something_list modes = 'activerecord.attributes.mymodel.my_somethings' I18n.t(modes).map {|k, v| [v, k]} end 

and install it as usual:

 en: activerecord: attributes: mymodel: my_somethings: my_enum_value: "My enum Value!" 

then use it with your choice: my_something_list

0
Jan 20 '17 at 12:57
source share

Try using TranslateEnum for these purposes.

 class Post < ActiveRecord::Base enum status: { published: 0, archive: 1 } translate_enum :status end Post.translated_status(:published) Post.translated_statuses @post = Post.new(status: :published) @post.translated_status 
0
Mar 07 '17 at 19:52
source share
 class ApplicationRecord < ActiveRecord::Base self.abstract_class = true def self.enum(definitions) defind_i18n_text(definitions) if definitions.delete(:_human) super(definitions) end def self.defind_i18n_text(definitions) scope = i18n_scope definitions.each do |name, values| next if name.to_s.start_with?('_') define_singleton_method("human_#{name.to_s.tableize}") do p values values.map { |key, _value| [key, I18n.t("#{scope}.enums.#{model_name.i18n_key}.#{name}.#{key}")] }.to_h end define_method("human_#{name}") do I18n.t("#{scope}.enums.#{model_name.i18n_key}.#{name}.#{send(name)}") end end end end en: activerecord: enums: mymodel: my_somethings: my_enum_value: "My enum Value!" enum status: [:unread, :down], _human: true 
0
Nov 28 '17 at 2:32 on
source share



All Articles