【发布时间】:2014-01-06 11:40:21
【问题描述】:
我正在使用路线翻译 gem 来翻译我的路线,但我无法以我想要的方式工作。
所以基本上我安装了 gem 并将路由插入到
localized do
end
现在我有这样的路线
users_en GET /en/users(.:format) users#index {:locale=>"en"}
users_cs GET /cs/users(.:format) users#index {:locale=>"cs"}
users_de GET /de/users(.:format) users#index {:locale=>"de"}
在我的 haml 视图中,我使用 link_to 导航到新页面。英语是我的默认语言,所以我的默认路由是 user_path,但如果我在 /cs/home 等不同语言的页面上,所有翻译都可以工作(t('word to translate') 中的文本有效,但链接没有被翻译,如果我点击它,我会进入页面 /users 而不是 /cs/users
我该如何解决这个问题?
我尝试将语言环境放在变量 "users_#{locale}_path" 的名称中,然后提取值,但我无法回答如何做到这一点(现在我只有一个字符串,我的链接看起来像 /users_cs_path)。
所以,我的问题是: 1. 我做错了吗? 2.如何在haml中创建一个字符串,如"users_#{locale}path",然后将其用作变量名来获取如下值: "#{users#{locale}_path"}(但是是的,这不起作用)。
在我的 locale.rb 我有
# tell the I18n library where to find your translations
I18n.load_path += Dir[Rails.root.join('lib', 'locale', '*.{rb,yml}')]
# set default locale to something other than :en
I18n.default_locale = :en
在我的 route_translator.rb 中
RouteTranslator.config do |config|
config.force_locale = true
#config.locale_param_key = :locale
config.generate_unlocalized_routes = true
end
谢谢。
【问题讨论】:
-
你是说链接有不正确的、没有本地化的url,还是里面有不正确的文字?
-
链接中的url没有本地化。我在页面localhost:3000/cs 上,关于页面的链接网址应该是localhost:3000/cs/about_translation,但它是localhost:3000/about。因为我有about_path,但是link_to t('about')中应该有about_cs_path,about_path
标签: ruby-on-rails ruby-on-rails-4 haml rails-routing rails-i18n