I18n_routing,空路径+嵌套资源

时间:2013-08-26 06:55:20

标签: ruby-on-rails rails-i18n

我有一个使用gem I18n_routing的Rails网站。我有一条这样的道路:

domain.com/<place>/bookings

地方可能是“纽约”。我的目标是让路线翻译成这样:

domain.com/<place>/bokningar

我试图通过像I18n_routing这样翻译:

  resources :places, :path => '', :except => [:index, :create, :new] do

    #resources :flight_pages, :path => 'flyg', :only => [:show, :index]
    #resources :hotel_pages, :path => 'hotell', :only => [:show, :index]    
    localized do
      resources :bookings, :except => [:edit, :update] do
        get :get_method_desc, :on => :collection
        get :get_image_path, :on => :collection      
      end
    end  
    get :autocomplete_place_name, :on => :collection     
  end

  localized do
    resources :places, :only => [:index, :create, :new]
  end

使用这样的翻译文件:

  resources: 
    places: 'plats'
    bookings: 'bokningar'

我遇到了一个问题,如果我像上面那样,I18n_routing不会认识到“预订”需要翻译。

如果我将设置更改为将本地化设置放在整个资源之外:

    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

我尝试在翻译文件中设置places: "",但I18n_routing只是跳过它。

我该怎么做才能获得这样的翻译:

domain.com/<place>/bokningar (:se)
domain.com/<place>/bookings (:en)

即。保持空路径并翻译嵌套资源“预订”?

编辑回应Aman Garg:  我用于这些的路径是

new_place_booking_path(@place) #=> domain.com/<place>/bookings/new
place_bookings_path(@place) #=> domain.com/<place>/bookings

1 个答案:

答案 0 :(得分:1)

有一个宝石可以根据语言提供动态翻译路线。它会像I18n一样在locale的基础上生成url。这很容易实现:

https://github.com/francesc/rails-translate-routes

按照github上的文档中提到的步骤进行操作。您可以在yml文件中定义路由的翻译。例如:在config / locales / routes.yml

en:
  routes:
    # you can leave empty locales, for example the default one
se:
  routes:
    places: 'plats'
    bookings: 'bokningar'