Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get a list of Globalized fields in model?

I use gems globalize and globalize_accessors for translating my models. Here is an example:

# model.rb
class Model < ActiveRecord::Base
  translates :title, :description
  globalize_accessors
end

I want to be able to retreive the list of fields i.e. [:title, :description] in the form to loop over them.

I have poked around and the only thing I could find was #globalize_attribute_names method. However it returns a list of translated fields with locales ordered by original title:

[:title_en, :title_es, :title_xx, :description_en, ... ]

So, the question is - is there a way to get the list of fields I have specified in translates ?

I kind of fixed it like this, but it is not very nice:

  def translates
    globalize_attribute_names.map do |name|
      name[/(\w+)_\w{2}\z/]
      Regexp.last_match[1]
    end.uniq
  end
like image 512
firedev Avatar asked Nov 19 '25 21:11

firedev


1 Answers

Turns out it was:

#translated_attribute_names
like image 85
firedev Avatar answered Nov 22 '25 14:11

firedev