全球化3宝石排序数组与翻译::

时间:2016-03-26 05:53:56

标签: ruby-on-rails sorting jquery-isotope rails-i18n globalize3

我想要实现的目标:使用我Gallery.tag

中的isotope.js动态创建类别排序器

要做到这一点,我需要

  1. 为我的html .class
  2. 循环出一个小写的唯一标签
  3. 循环标记(使用globalize 3进行翻译)以显示在<h2>
  4. 我无法在两种情况下都使用@galleries因为在切换语言时它会将中文字符输出到我的.class中,从而打破了分拣机。因此,我创建了两个数组,并将它们与zip组合在一起,然后将它们与每个数组相结合。

    我尝试按字母顺序对它们进行排序,但我的相应中文标签并未遵循我的英文顺序。

    画廊/ 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>
    

    控制器/ 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
    

    类别是[国家,主题,项目,战争] zh类别是[主题,国家,战争,项目]&lt; - 当前(在en =主题,国家,战争,项目) 我的分类是[国家,主题,项目,战争]&lt; - goal(与en相同)

    简而言之,我希望中文翻译遵循我的英文字母顺序。

    enter image description here

    enter image description here

1 个答案:

答案 0 :(得分:0)

解决了!简而言之,要使用globalize3 ::翻译模型对某些内容进行排序,我的最佳方法是按:id或我的情况:created_at排序,而不是按标题排序,我必须处理寻找方法排序汉字!由于我并不特别关心条款是否按字母顺序排列,只要它们同步,我就会感到高兴。

就这么简单,我肯定是在过度思考它!

画廊/ index.html中

<% @array = [] %>
<% @test.each do |test| %>
    <% next if @array.include?(test.tag) %>
    <% @array << test.tag %>
<% end %>

<% @array_en = [] %>
<% @test.each do |test| %>
    <% next if @array_en.include?(test.tag_en) %>
    <% @array_en << test.tag_en %>
<% end %>

<% @combined_array = @array.zip(@array_en) %>

<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>
    <% @combined_array.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>

控制器/ galleries.rb

@test = Gallery.all.sort { |p1, p2| p1.created_at <=> p2.created_at }