I want to be able to switch my locale in an ActiveAdmin app of mine. So far I've followed this guide on switching-locale, which actually mentions the problem I'm having:
我希望能够在我的ActiveAdmin应用程序中切换我的语言环境。到目前为止,我已经遵循了关于切换区域设置的本指南,它实际上提到了我遇到的问题:
You will notice, however, that all links keep the default locale of your app.
但是,您会注意到所有链接都保留了应用的默认语言环境。
So in my case, once I switch the locale, the urls stay
所以在我的情况下,一旦我切换了语言环境,网址就会保留
-
localhost:3000/en/admin/users
instead of localhost:3000/de/admin/users
localhost:3000 / en / admin / users而不是
The guide also proposes a solution:
该指南还提出了一个解决方案:
You can override this default locale by passing the locale to all
_path
methods.您可以通过将语言环境传递给所有_path方法来覆盖此默认语言环境。
But this seems error-prone and quite a lot of work.
但这似乎容易出错并且工作量很大。
So looks like ActiveAdmin uses I18n.locale
once to create all the urls and does not consider changes to I18n.locale
after that.
所以看起来ActiveAdmin使用I18n.locale创建所有URL并且之后不考虑对I18n.locale的更改。
Meaning if you this in your ApplicationController:
如果您在ApplicationController中有这个意思:
def set_locale
I18n.locale = params[:locale] || I18n.default_locale
end
I've tried overriding
我试过压倒一切
ActiveAdmin::Helpers::Routes.default_url_options
in my ApplicationController, which did not help.
在我的ApplicationController中,没有帮助。
Does anyone have an idea how I could resolve this issue?
有谁知道如何解决这个问题?
Edit:
I also set these and tried different variations of the scope
method.
我也设置了这些并尝试了范围方法的不同变体。
Routes
scope '(/:locale)', locale: /en|de/, defaults: { locale: I18n.locale }
ActiveAdmin.routes(self)
end
ApplicationController
def default_url_options(options={})
{ locale: I18n.locale }.merge options
end
2 个解决方案
#1
Ok so I've finally figured out what was going on.
好的,我终于弄清楚发生了什么。
I originally followed the rails guide and set my routes.rb:
我最初遵循rails指南并设置我的routes.rb:
# config/routes.rb
scope "/:locale" do
resources :books
end
which resulted in an error like:
导致如下错误:
No route matches {:action=>"index", :controller=>"admin/users"} missing required keys: [:locale]
没有路由匹配{:action =>“index”,:controller =>“admin / users”}缺少必需的密钥:[:locale]
this was "fixed" by setting
这是通过设置“修复”
scope ':locale', defaults: { locale: I18n.locale } do
ActiveAdmin.routes(self)
end
as suggested in the current version of the Switching locale guide. But this has the side-effect of all subsequent url_helpers using this locale for url generation. BTW at least one other person ran into this.
正如当前版本的“切换区域设置指南”中所建议的那样。但这会产生所有后续url_helpers的副作用,使用此区域设置生成url。顺便说一句,至少有一个人碰到了这个。
My current work-around can be found here:
我目前的解决方法可以在这里找到:
lib/active_admin/helpers/routes/url_helpers.rb
def self.default_url_options
(Rails.application.config.action_mailer.default_url_options.merge({locale: ::I18n.locale})) || {}
end
Now urls are generated as expected.
现在,URL按预期生成。
#2
First of all you need define :locale scope as optional, this can be done with this code:
首先,您需要定义:locale scope as optional,这可以使用以下代码完成:
scope '(:locale)' do
#your routes
end
After in ApplicationController put that code for enable default scoping:
在ApplicationController之后,将该代码放入启用默认范围:
def default_url_options(options={})
options.merge({locale: I18n.locale})
end
#1
Ok so I've finally figured out what was going on.
好的,我终于弄清楚发生了什么。
I originally followed the rails guide and set my routes.rb:
我最初遵循rails指南并设置我的routes.rb:
# config/routes.rb
scope "/:locale" do
resources :books
end
which resulted in an error like:
导致如下错误:
No route matches {:action=>"index", :controller=>"admin/users"} missing required keys: [:locale]
没有路由匹配{:action =>“index”,:controller =>“admin / users”}缺少必需的密钥:[:locale]
this was "fixed" by setting
这是通过设置“修复”
scope ':locale', defaults: { locale: I18n.locale } do
ActiveAdmin.routes(self)
end
as suggested in the current version of the Switching locale guide. But this has the side-effect of all subsequent url_helpers using this locale for url generation. BTW at least one other person ran into this.
正如当前版本的“切换区域设置指南”中所建议的那样。但这会产生所有后续url_helpers的副作用,使用此区域设置生成url。顺便说一句,至少有一个人碰到了这个。
My current work-around can be found here:
我目前的解决方法可以在这里找到:
lib/active_admin/helpers/routes/url_helpers.rb
def self.default_url_options
(Rails.application.config.action_mailer.default_url_options.merge({locale: ::I18n.locale})) || {}
end
Now urls are generated as expected.
现在,URL按预期生成。
#2
First of all you need define :locale scope as optional, this can be done with this code:
首先,您需要定义:locale scope as optional,这可以使用以下代码完成:
scope '(:locale)' do
#your routes
end
After in ApplicationController put that code for enable default scoping:
在ApplicationController之后,将该代码放入启用默认范围:
def default_url_options(options={})
options.merge({locale: I18n.locale})
end