在一个经典的多语言 rails 4 网站中,我想避免重复的内容问题。
我使用friendly-id 和globalize3 使网站多语言。
这是我的配置:
经典页面模型:
extend FriendlyId
friendly_id :title, use: [:slugged, :history]
translates :title, :content, :slug
第一条路线配置:
scope ":locale", /#{I18n.available_locales.join("|")}/ do
my_routes
end
#rails cast solution
match '*path', to: redirect("/#{I18n.default_locale}/%{path}"), constraints: lambda { |req| !req.path.starts_with? "/#{I18n.default_locale}/" }, via: :all
match '', to: redirect("/#{I18n.default_locale}"), via: :all
第一个应用程序应用程序控制器配置:
before_action :set_locale
def default_url_options(options = {})
{locale: I18n.locale}
end
private
def set_locale
I18n.locale = params[:locale] if params[:locale].present?
end
由于我希望用户在 URL 末尾没有 /the-default-locale 的情况下访问该站点,因此我将配置更改如下:
路线配置:
#Here I'm trying to avoid /en/content and /content to avoid duplication
match "/#{I18n.default_locale}/*path", to: redirect("/%{path}"), via: :all
scope "(:locale)", locale: /#{I18n.available_locales.join("|")}/ do
my_routes
end
#removed the rails cast fallback to default locale
应用控制器配置:
before_action :set_locale
def default_url_options(options = {})
{ :locale => ((I18n.locale == I18n.default_locale) ? nil : I18n.locale) }
end
private
def set_locale
I18n.locale = params[:locale] || I18n.default_locale
end
在语言之间切换的链接:
#here the French language is the default locale
<%= link_to_unless_current t("English"), locale: "en" %>
<%= link_to_unless_current t("French"), locale: nil %>
问题:
1- 使用友好的 id 和翻译后的 slug,您可以访问 mywebsite.com/mon_contenu 和 mywebsite/en/my_content。但是,如果您已经在 mywebsite.com/mon_contenu 上并且您单击英文开关,您将在 mywebsite.com/en/mon_contenu 上显示英文内容,但 url 不会切换到英文 slug。
这是否被视为重复内容?如果是的话,我该如何避免呢?
2- 如果内容未翻译,则使用 globalize 将显示默认语言环境内容。所以如果没有翻译,mywebsite.com/mon_contenu 和 mywebsite.com/en/my_content 可以用相同的语言显示相同的内容。
这又被视为重复吗?
考虑的选项
使用robot.txt 禁用某些路由,例如只允许对默认语言环境进行索引?
使用规范标签,但我不知道如何在布局中轻松设置它
你如何处理这种情况?
任何帮助/想法/评论/建议总是受欢迎的!
一如既往地感谢您的帮助。