繁体   English   中英

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

[英]Globalize 3 gem sorting array with translation::

我想要实现的目标:使用我的Gallery.tag isotope.js动态创建类别排序器

要做到这一点,我需要

  1. 循环出我的html .class的小写唯一标签
  2. 循环标记(使用globalize 3进行翻译)以在<h2>显示

我不能在两种情况下都使用@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个类别是[国家,主题,项目,战争] zh个类别是[主题,国家,战争,项目] <-当前(在en =主题,国家,战争,项目)我的类别是[国家,主题,项目,战争] <-目标(与en相同)

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

在此输入图像描述

在此输入图像描述

解决了! 简而言之,要使用globalize3 :: translation模型进行排序,我的最佳方法是按以下方式排序:id或在我的情况下:created_at而不是title,我必须处理找到一种排序汉字的方法! 由于我并不特别在意这些术语是否按字母顺序排列,只要它们同步,我就很高兴。

就这么简单,我绝对是在思考它!

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM