2

我对 globalize 和friendly_id 有疑问。该网站有 2 种语言 Ru 和 En。Gemfriendly_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

结尾

来自应用程序.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

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

4

1 回答 1

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)

于 2017-01-22T07:23:54.390 回答