【发布时间】:2016-04-25 00:48:24
【问题描述】:
我对 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
还有更简约的方法。但是这个方法服务器挂了。
【问题讨论】:
-
我认为重定向在您的控制器中,对吧?可以加入吗?
-
嗨,MichalI。我在帖子中添加控制器。
-
因此,如果您打开 Rails 控制台并执行
I18n.locale=:ru; Page.first.friendly_id,您会得到俄语名称,但I18n.locale=:en; Page.first.friendly_id是英语,对吧? -
>> I18n.locale=:ru; Page.first.friendly_idPage Load (0.8ms) SELECT "pages".* FROM "pages" ORDER BY "pages"."id" ASC LIMIT 1 Page::Translation Load (0.4ms) SELECT "page_translations".* FROM "page_translations" WHERE "page_translations"."page_id" = $1 [["page_id", 1]] => "o-saite" -
>>I18n.locale=:en; Page.first.friendly_idPage Load (0.9ms) SELECT "pages".* FROM "pages" ORDER BY "pages"."id" ASC LIMIT 1 Page::Translation Load (0.5ms) SELECT "page_translations".* FROM "page_translations" WHERE "page_translations"."page_id" = $1 [["page_id", 1]] => "about-site"
标签: ruby-on-rails friendly-id globalize