Rails i18n passing a URL parameter
I'm using i18n with Rails succesfully but when I pass a parameter in the url i18n stops working and seems to fallback to English. My f开发者_StackOverflow社区orm labels are switched back to English instead of Dutch. How can I prevent this from happening?
Relevant lines:
config.i18n.default_locale = :nl
config.i18n.locale = :nl
Example URL:
#/users?param1=abc
localized do
resources :users
end
If you're supplying the locale in the URL (I can not make that up out of the information you supplied) then you can make sure it always gets passed to URL helper by overriding the default URL options with a snippet like this in your application controller:
def default_url_options(options={})
{ :locale => I18n.locale }
end
Via http://guides.rubyonrails.org/i18n.html#setting-the-locale-from-the-url-params
Use as in rails guide:
# config/routes.rb
scope "/:locale" do
resources :books
end
Set locale:
before_filter :set_current_locale
private
def set_current_locale
I18n.locale = params[:locale]
end
精彩评论