3

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
firedev
  • 20,898
  • 20
  • 64
  • 94

1 Answers1

6

Turns out it was:

#translated_attribute_names
firedev
  • 20,898
  • 20
  • 64
  • 94