我已经在我的网站上添加了本地化,并且工作正常,除了两点
这是我的代码段
application.rb中
include HttpAcceptLanguage::AutoLocale
before_action :set_locale
def default_url_options(options = {})
{ :path_prefix => I18n.locale }
end
def set_locale
if cookies[:educator_locale] && I18n.available_locales.include?(cookies[:educator_locale].to_sym)
l = cookies[:educator_locale].to_sym
else
if params[:path_prefix].present?
l = params[:path_present]
cookies.permanent[:educator_locale] = l
else
if (http_accept_language.preferred_language_from(http_accept_language.user_preferred_languages).include? "en")
l = 'en'
cookies.permanent[:educator_locale] = l
end
if ( http_accept_language.preferred_language_from(http_accept_language.user_preferred_languages).include? "fr")
l = 'fr'
cookies.permanent[:educator_locale] = l
end
end
cookies.permanent[:educator_locale] = l
end
I18n.locale = l
end
我的设置控制器
def change_locale
l = params[:locale].to_s.strip.to_sym
# puts "---------"
# puts l
l = I18n.default_locale unless I18n.available_locales.include?(l)
cookies.permanent[:educator_locale] = l
url_hash = Rails.application.routes.recognize_path URI(request.referer).path url_hash[:locale] =l
redirect_to url_hash
端
然后我的路线
get '/change_locale/:locale', to: 'settings#change_locale', as: :change_locale
get "home/index"
root 'home#index'
点击任何链接后,路径前缀可见,但不会直接打开网站。
答案 0 :(得分:0)
当您到达应用程序中的第一个url(root_url,users_url ...)时,在您进入set_locale之前,网址会显示在您的网络浏览器中。
然后,如果您访问应用程序中的其他链接,则会在网址中添加区域设置。但是翻译仍然适用于您在I18n.locale
中设置的区域设置。