为了使我的应用程序国际化,我想根据当前的I18n.locale将'hello_path'等命名路由自动翻译为'/ en / hello'或'/ fr / bonjour。但我遇到了问题(名字冲突?):
我的路线是:
LostInTranslation::Application.routes.draw do
root :to => 'home#index'
scope '/:locale' do
constraints :locale => 'fr' do
get 'bonjour' => 'home#hello', :as => 'hello'
end
constraints :locale => 'en' do
get 'hello' => 'home#hello', :as => 'hello'
end
end
end
在ApplicationController中,我设置了语言环境并将其传递给默认的url选项:
class ApplicationController < ActionController::Base
protect_from_forgery
before_filter :set_locale
private
def default_url_options(options={})
options.merge({ :locale => I18n.locale })
end
def set_locale
if params[:locale] and I18n.available_locales.include?(params[:locale].to_sym)
I18n.locale = params[:locale]
else
I18n.locale = I18n.default_locale
end
end
end
但是在视图中我使用&lt;%= link_to t('hello'),hello_path%&gt;我收到这个错误:
No route matches {:controller=>"home", :locale=>:fr, :action=>"hello"}
然而rake路线表示声明:
root / {:action=>"index", :controller=>"home"}
hello GET /:locale/bonjour(.:format) {:locale=>"fr", :action=>"hello", :controller=>"home"}
hello GET /:locale/hello(.:format) {:locale=>"en", :action=>"hello", :controller=>"home"}
您是否有任何关于它为什么不起作用或以其他方式实现这一目标的线索?我知道i18n_routing gem,但它通过声明具有不同名称的本地化路由(在此示例中为en_hello和fr_hello)来工作。
谢谢!
答案 0 :(得分:0)
我没有I18n_routing宝石的经验,但可以想象它接受非本地化路线并为您翻译它;例如,你提供hello_path,它完成其余的工作。
你应该有一个非命名空间的默认语言
google.com # english default locale
google.com/es # spanish
而不是
google.com/en # english
google.com/es # spanish
google.com # nothing here
您应该查看具有默认语言环境的translate_routes gem以及我使用过的没有任何问题的gem。 https://github.com/raul/translate_routes