Search code examples
ruby-on-rails-4routesrails-i18n

I18n routes rails


I want to program in english but my output routes must be in another language.

Today I'm using the following approach and I'm not getting the index action and new and edit still missing translation. I've tried to use test: one: other: without success.

resource :activities, path: I18n.t('routes.test')

# config/routes.rb

     activities POST   /teste(.:format)            activities#create
 new_activities GET    /teste/new(.:format)        activities#new
edit_activities GET    /teste/edit(.:format)       activities#edit
                GET    /teste(.:format)            activities#show
                PATCH  /teste(.:format)            activities#update
                PUT    /teste(.:format)            activities#update
                DELETE /teste(.:format)            activities#destroy

# config/locales/routes.yml

fr:
  routes:
    test: "teste"

How can I accomplish that?


Solution

  • You should create a routes.yml file with routes: as main key and the routes you want to translate one level deeper

    # config/locales/routes.yml
    routes:
      activities: "something_else"
    

    and call it like that in routes.rb

    resources :activities, path: I18n.t('activities', locale: :routes)
    

    Then you're independent of the language set in I18n.locale

    Output is something like this:

           Prefix Verb   URI Pattern                        Controller#Action
       activities GET    /something_else(.:format)          activities#index
                  POST   /something_else(.:format)          activities#create
     new_activity GET    /something_else/new(.:format)      activities#new
    edit_activity GET    /something_else/:id/edit(.:format) activities#edit
         activity GET    /something_else/:id(.:format)      activities#show
                  PATCH  /something_else/:id(.:format)      activities#update
                  PUT    /something_else/:id(.:format)      activities#update
                  DELETE /something_else/:id(.:format)      activities#destroy
    

    EDIT: overlooked that you also want to translate new and edit, you can do something like that:

    scope(path_names: { new: I18n.t('new', locale: :routes), edit: I18n.t('edit', locale: :routes) }) do
      resources :activities, path: I18n.t('activities', locale: :routes)
    end
    

    Or you can override it hardcoded in routes.rb like mentioned in http://guides.rubyonrails.org/routing.html#translated-paths