【发布时间】:2017-03-17 17:12:10
【问题描述】:
我在application.rb 中设置了locale 的基本选项:
config.i18n.available_locales = [:pl, :en]
config.i18n.default_locale = :pl
并且还限定了路线:
Rails.application.routes.draw do
get '/:locale', to: 'home#index'
root to: 'home#index'
scope ":locale", locale: /#{I18n.available_locales.join("|")}/ do
get 'settings', to: 'home#settings'
end
end
这样我可以访问我在www.mysite.com 和www.mysite.com/en or pl 下的根站点,以及在url 中包含语言环境时我的设置站点。
现在假设用户输入了www.mysite.com/settings。我想让我的应用知道 url 中没有语言环境,所以去抓取default_locale,设置它,然后重定向到www.mysite.com/pl/settings。
我该怎么做?
PS 我也将这些添加到我的ApplicationController:
before_action :set_locale
def set_locale
I18n.locale = params[:locale] || I18n.default_locale
end
def default_url_options
{ locale: I18n.locale }
end
【问题讨论】:
标签: ruby-on-rails redirect rails-i18n