【发布时间】:2015-09-10 09:03:42
【问题描述】:
我已经在我的网站中添加了本地化,它工作正常,除了两点
- 当我打开网站(例如 www.test.help.com)时,它会显示此 url,而当我在应用程序控制器中使用 path_prefix 时,它也应该显示语言环境。
- 当我点击更改语言时,网址并没有立即改变
这是我的代码 sn-p
应用程序.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'
单击任何链接后,路径前缀可见,但在打开网站时不直接可见。
【问题讨论】:
-
在 set_locale 函数中,您没有任何默认值,否则
if的任何情况下都无效。 -
但我只在英文和法文浏览器上进行了测试,这些情况至少应该适用于这些情况
-
好的,但是根据语言环境,主页 (route_path) 上的翻译工作正常吗?
-
是的,当我点击主页或任何其他链接时,它可以工作
标签: ruby-on-rails ruby-on-rails-4 localization routes rails-i18n