Rails全球化+友好ID

时间:2016-01-19 13:52:23

标签: ruby-on-rails friendly-id globalize

我遇到了globalize和friendly_id的问题。该网站有2种语言Ru和En。 Gem friendly_id,globalize和friendly_id-globalize已配置并正常工作。如果我将语言从俄语改为英语,一切都很好:

http://127.0.0.1:3000/ru/o-saite -> http://127.0.0.1:3000/en/about-site

但是当我从英语改为俄语时,错误的重定向:

http://127.0.0.1:3000/en/about-site -> http://127.0.0.1:3000/ru/about-site

页面模型:

class Page < ActiveRecord::Base
  validates :title, :content, :slug, presence: true
  validates :slug, uniqueness: true
  validates :title, length: { minimum: 3, maximum: 255 }
  validates :content, length: { minimum: 5 }

  # globalize
  translates :title, :content, :slug

  # FriendlyId
  extend FriendlyId

  friendly_id :slug_candidates, use: [:slugged, :finders, :globalize]

  def slug_candidates
    [
      :title,
      [:title, :id]
    ]
  end

  def should_generate_new_friendly_id?
    title_changed?
  end

  def normalize_friendly_id(string)
    title.to_s.to_slug.normalize(transliterations: :russian).to_s
  end

end

迁移:

class TranslatePage < ActiveRecord::Migration
  def self.up
    Page.create_translation_table!({
      title: :string,
      content: :text,
      slug: :string
    }, {
      migrate_data: true
    })
  end

  def self.down
    Page.drop_translation_table! migrate_data: true
  end

来自application.rb

config.i18n.load_path += Dir[Rails.root.join('config', 'locales', '**', '*.{rb,yml}')]
config.i18n.default_locale = :ru
config.i18n.fallbacks = true

页面控制器

class PagesController < ApplicationController

  before_action :load_page, only: [:show]

  def show    
  end

  private
    def load_page
      @page = Page.friendly.find(params[:id])
      redirect_to action: action_name, id: @page.friendly_id, status: 301 unless @page.friendly_id == params[:id]
    end

    def page_params
      params.require(:page).permit(:title, :content,:slug, :published)
    end

end

可能是什么问题?

解决吗

问题出现在观点中。在layouts / aplication.html.slim中:

ul class='change_lang'
 li = link_to_unless I18n.locale == :en, "EN", locale: :en
 li = link_to_unless I18n.locale == :ru, "RU", locale: :ru

现在在pages / show.slim

- content_for :change_lang do
  li
    - link = I18n.with_locale(:ru){page_path(@page, locale: 'ru')}
    = link_to 'RU', link
  li
    - link = I18n.with_locale(:en){page_path(@page, locale: 'en')}
    = link_to 'EN', link

在layouts / aplication.html.slim

ul class='change_lang'
  - if content_for?(:change_lang)
    = yield :change_lang
  - else
    li = link_to_unless I18n.locale == :en, "EN", locale: :en
    li = link_to_unless I18n.locale == :ru, "RU", locale: :ru

https://github.com/norman/friendly_id-globalize/issues/7

还有更简约的方法。但是这个方法服务器挂了。

1 个答案:

答案 0 :(得分:0)

我有一个通用的答案。 如果它适合你,请现在让我。 如果你有一个名称空间,你可以在:shop设置,如果不是url_for(obj)

你的application_helper.rb

中的

def languages(obj=nil)
    content_for(:switch_locale) do
      I18n.available_locales.each do |locale|
        I18n.with_locale(locale) do
          concat(
            if obj
             content_tag(:li, (link_to locale, url_for([:shop, obj]) ))
            else
             content_tag(:li, (link_to locale, url_for(locale: locale.to_s) ))
            end              
          )
        end
      end
    end
  end

在你的观点中

- languages @category

或只是

- languages

在application.html。(erb / haml.slim)或您要呈现翻译的任何位置。 =yield(:switch_locale)