What I want to achieve: dynamically create category sorter using isotope.js from my Gallery.tag
To do that I need to
- loop out a lowercase unique tags for my html .class
- loop out the tag (using globalize 3 for translation) to display in the
<h2>
I cannot use @galleries
for both cases because upon switch language it will output chinese character to my .class which breaks the sorter. Therefore I created two arrays and combine them together with zip and the looping it with each.
I try to sort them by alphabetic order, but my corresponding Chinese tags are not following my English order.
galleries/index.html.erb
<% @uniq_test = [] %>
<% @galleries_order.each do |gallery| %>
<% next if @uniq_test.include?(gallery.tag) %>
<% @uniq_test << gallery.tag %>
<% end %>
<% @sorters = @sorters.map(&:downcase).sort! %>
<% @uniq_test = @uniq_test.map(&:downcase).sort! %>
<% @uniq_sorters = @uniq_test.zip(@sorters) %>
<div class="main">
<div class="gallery-select-wrapper">
<div class="sort-gallery-buttons animated slideInLeft text-center">
<h2 id="recent"class="recent"><%= t"galleries.sorter.recent"%></h2>
<% @uniq_sorters.each do |uniq, sorter| %>
<% if sorter != nil %>
<% str = "<h2 class='" + sorter + "'" + "id='"+ sorter + "'>"%>
<%= str.html_safe + uniq + "</h2>".html_safe %>
<% end %>
<% end %>
</div>
</div>
</div>
controllers/galleries.rb
def index
@galleries = Gallery.all.order("created_at DESC")
@galleries_order = Gallery.all.order("title ASC")
@sorters = Gallery.uniq.pluck(:tag)
gon.tags = Gallery.uniq.pluck(:tag)
end
en categories are [country, theme, project, war] zh categories are [主題,國家, 戰爭, 項目] <-- current (in en = theme, country, war, project) my categories are [國家, 主題, 項目, 戰爭] <-- goal (same as en)
in a nutshell I want chinese translation to follow my english alphabetic order.