ruby-on-rails-4globalize

Globalize accessors on subset of available locales


By design, some classes will deal with only a subset of available languages.

the globalize-accessors gem is quite useful, however, the rendering requires that the following be defined

Class.globalize_attribute_names

so while available_locales = [:en, :ru, :fr, :de], the goal is to work with a smaller array [:en, :ru]

The documentation states Calling globalize_accessors with no options will therefore generate accessor methods for all translated fields and available languages. But the purported way to invoke is in the model

globalize_accessors :locales => [:en, :fr], :attributes => [:title] 

How can the globalize_accessorsmethod refer to an array, something generated by the likes of

@post.owner.ownerlocales.pluck('locale')

(although the array values are quoted...)


Solution

  • A working solution found but that does not address the above question, is based on the fact that globalize-accessors

    gives you access to methods: title_pl, title_en, title_pl=, title_en=

    Thus, a controller method that generates a whitelist

    @locales =  []
    @post.owner.ownerlocales.each do |ol|
      locale = ol.locale
      @locales.push(locale)
    end
    

    ... then process in the view filtering out the globalize_processors from whitelist

    <% Post.globalize_attribute_names.each do |lang| %>
      <% my_string = lang.to_s %>
      <% @locales.each do |locale| %>
        <% checkstring = "_" + locale %>
        <% if my_string.include? checkstring %>
          <div class="row">
            <%= t(lang[0..-4]) %> &nbsp;&nbsp;-&nbsp;&nbsp; <%= lang[-2, 2] %> <br />
            <%= f.text_area lang, rows: "3" %>
          </div>
        <% end %>
      <% end %>
    <% end %>
    

    Not efficient, functional.