1

我有一个使用 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

我翻译了空路径“地点”,因此路线变成:

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
4

1 回答 1

1

有一个 gem 可以根据语言提供动态翻译路线。它将像 I18n 一样根据语言环境生成 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'
于 2013-09-03T06:03:26.090 回答