在 root_url 中添加 path_prefix
adding path_prefix in root_url
我在我的网站中添加了本地化并且工作正常,除了两点
- 例如,当我打开网站 www.test.help.com 时,它显示此 url 而不是它也应该显示语言环境,因为我在我的应用程序控制器中使用 path_prefix。
- 当我点击更改语言时,url 并没有立即改变
这是我的代码片段
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'
一旦我点击任何 link,然后路径前缀是可见的,但不是直接在打开网站时。
当您到达应用程序的第一个 url 时(root_url、users_url ...),url 之前会显示在您的网络浏览器中你进入 set_locale。
然后,如果您在应用程序中访问另一个 link,则语言环境将添加到 url 中。但无论如何,翻译将适用于您在 I18n.locale
.
中设置的语言环境
我在我的网站中添加了本地化并且工作正常,除了两点
- 例如,当我打开网站 www.test.help.com 时,它显示此 url 而不是它也应该显示语言环境,因为我在我的应用程序控制器中使用 path_prefix。
- 当我点击更改语言时,url 并没有立即改变
这是我的代码片段
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'
一旦我点击任何 link,然后路径前缀是可见的,但不是直接在打开网站时。
当您到达应用程序的第一个 url 时(root_url、users_url ...),url 之前会显示在您的网络浏览器中你进入 set_locale。
然后,如果您在应用程序中访问另一个 link,则语言环境将添加到 url 中。但无论如何,翻译将适用于您在 I18n.locale
.