【发布时间】:2013-12-26 11:49:07
【问题描述】:
我正在使用子域来确定 Rails 4 网站中的语言环境。我完全按照我想要的方式与语言环境切换器一起工作,但现在我需要翻译路线,我不确定最好的方法。
我查看了https://github.com/kwi/i18n_routing i18n 路由 gem,但这不适用于子域,它似乎通过添加 /locale 来更改路径,这不是我需要的。
其他 gem 似乎在 Rails 4 中已经过时了。
编辑
基本上,我希望能够使用相同的视图助手,但更改 url 以使用所选子域反映的任何语言。这只是关于路线翻译。
我有可用的语言特定模板,我可以生成特定语言的导航模板,但我真的不想担心更改 erb 路径和 url erb 标签
结束编辑
来自 routes.rb 的示例
scope module: 'countries', shallow: true do
get 'south_american', to: 'south_america#index', as: :south_america
scope module: 'south_america' do
get 'south-america-weather', to: 'weather#index', as: :south_america_weather
get 'south-america-gps-maps', to: 'gps#index', as: :south_america_gps
get 'south-america-accommodation', to: 'hotels#index', as: :south_america_hotels
get 'south-america-vacations', to: 'vacations#index', as: :south_america_vacations
get 'south-america-facts', to: 'facts#index', as: :south_america_facts
end
以 south_america_hotels_path 为例会生成一个 url
南美洲住宿
这很好,但是我将如何翻译它,以便当我在西班牙子域 south_america_hotels_path 上时会生成以下网址
hoteles-en-sudamerica
更新
此外,这将如何用于 url 而不仅仅是路径,以便 south_america_hotels_url 将生成
en.some_site/south-america-accommodation
当我在西班牙子域上时,我会得到
es.some_site/hoteles-en-sudamerica
对于所涉及的不同语言环境,依此类推。
我很乐意使用 yml 文件来翻译 url 或在 routes.rb 文件中定义额外的路由,这都是一个选项,但我更愿意在 routes.rb 中定义 url 但我无法找到一种方法为基于子域的相同 :as 路径/url 选项提供特定于语言的 url。
针对之前的回复进行更新和进一步澄清。
更改网址不是一种选择,它们需要匹配现有的网址。我只需要知道如何从视图助手的角度翻译它们,而无需更改视图助手。
【问题讨论】:
-
你看过Rails i18n guide吗?它解释了如何使用子域来设置语言环境。翻译路线不是 Rails 开箱即用的事情。看看rails-translate-routes 或route_translator。
-
@janfoeh 我对我处理子域的方式非常满意,只是路由翻译给我带来了麻烦,你指向的宝石不适合我的需要。当然,这可以在不使用 gem 的情况下完成
标签: ruby-on-rails routing ruby-on-rails-4 rails-i18n