Ruby on rails I18n#u路由,空路径+;嵌套资源
我有一个使用gemi18n_路由的Rails网站。我有这样一条路径:Ruby on rails I18n#u路由,空路径+;嵌套资源,ruby-on-rails,rails-i18n,Ruby On Rails,Rails I18n,我有一个使用gemi18n_路由的Rails网站。我有这样一条路径: domain.com/<place>/bookings 翻译文件如下所示: domain.com/<place>/bokningar resources: places: 'plats' bookings: 'bokningar' domain.com/<place>/bokningar (:se) domain.com/<place>/booking
domain.com/<place>/bookings
翻译文件如下所示:
domain.com/<place>/bokningar
resources:
places: 'plats'
bookings: 'bokningar'
domain.com/<place>/bokningar (:se)
domain.com/<place>/bookings (:en)
我遇到了一个问题,如果我像上面那样使用它,I18n_routing不承认“预订”需要翻译
如果我将设置更改为将本地化do置于整个资源之外:
localized do
resources :places, :path => '', :except => [:index, :create, :new] do
#resources :flight_pages, :path => 'flyg', :only => [:show, :index]
#resources :hotel_pages, :path => 'hotell', :only => [:show, :index]
resources :bookings, :except => [:edit, :update] do
get :get_method_desc, :on => :collection
get :get_image_path, :on => :collection
end
get :autocomplete_place_name, :on => :collection
end
end
localized do
resources :places, :only => [:index, :create, :new]
end
我将空路径“places”转换为以下路径:
domain.com/plats/<place>/bokningar
i、 e.保留空路径并转换嵌套资源“预订”
编辑以响应Aman Garg:
我使用的路径是
new_place_booking_path(@place) #=> domain.com/<place>/bookings/new
place_bookings_path(@place) #=> domain.com/<place>/bookings
new_place_booking_path(@place)#=>domain.com//bookings/new
place_bookings_path(@place)#=>domain.com//bookings
根据语言提供动态翻译路线有一个gem。它将根据语言环境生成url,就像I18n一样。这一点很容易实现:
遵循github上文档中提到的步骤。您可以在yml文件中定义路由的翻译。e、 g:在config/locales/routes.yml中
en:
routes:
# you can leave empty locales, for example the default one
se:
routes:
places: 'plats'
bookings: 'bokningar'
有一个宝石提供动态翻译路线根据语言。它将根据语言环境生成url,就像I18n一样。这一点很容易实现: 遵循github上文档中提到的步骤。您可以在yml文件中定义路由的翻译。e、 g:在config/locales/routes.yml中
en:
routes:
# you can leave empty locales, for example the default one
se:
routes:
places: 'plats'
bookings: 'bokningar'
你可以粘贴一些例子,比如你如何为上面提到的url创建路径。我是说你现在正在使用的路径。像一些链接路径或静态url?当然,我现在添加了一些示例。希望这就是您所寻找的。您是否在翻译文件中指定了
se:
?当您启动console,设置I18n.locale=:se
并尝试一些路径时,它是否工作?@Christoff:您是否尝试过下面回答中提到的解决方案,我想这正是你所需要的解决方案。你能粘贴一些例子,比如你是如何为上面提到的url创建路径的吗。我是说你现在正在使用的路径。像一些链接路径或静态url?当然,我现在添加了一些示例。希望这就是您所寻找的。您是否在翻译文件中指定了se:
?当您启动console,设置I18n.locale=:se
并尝试一些路径时,它是否工作?@Christoff:您是否尝试过下面回答中提到的解决方案,我想这正是你需要的解决方案。我知道rails的翻译路线,但我从未尝试过。它比I18n_路由更有效,解决了我的问题。谢谢你,阿曼!我知道铁路运输路线,但从未尝试过。它比I18n_路由更有效,解决了我的问题。谢谢你,阿曼!