jlfenaux
jlfenaux

Reputation: 3291

Translating routes in Rails 3.1 without any gem

In a previous Rails 2.3 project I used the translate_routes gem to perform the translation of the routes. It worked great. In my new Rails 3.1 project, again, I need route translation. Unfortunately, translate_routes doesn't work any longer and Raul its developer announced that he would no longer maintain the gem. I tried to work with one of the project's fork that is supposed to be ok on Rails 3.1, but I couldn't do much of it.

Is there a way to build route translations without a gem ?

Here an example of a working route without translation.

  constraints(:subdomain => 'admin') do
    scope "(:locale)", :locale => /fr|de/ do
           resources :country, :languages
          match '/' => 'home#admin', :as => :admin_home
    end
  end

As you can see, I also want to have a default route without locale that is used for my default locale : en.

Has anyone done that before? Thanks

Upvotes: 1

Views: 1497

Answers (2)

HendrikPetertje
HendrikPetertje

Reputation: 123

Saw your post earlier, but found out another sollution later. I wanted to translate Rails routes and their default resource actions, but I didn't like they way rails-translate-routes added _nl to my default path-names.

I ended up doing this (also works in rails 4.0), which should be a good sollution when you are presenting your app in only 1 or 2 languages.

# config/routes.rb
Testapp::Application.routes.draw do
  # This scope changes resources methods names
  scope(path_names: { new: I18n.t('routename.new'), edit: I18n.t('routename.edit') }) do

    # devise works fine with this technique
    devise_for :users, path: I18n.t('routename.userspath')

    # resource path names can be translated like this
    resources :cars, path: I18n.t('routename.carspath')

    # url prefixes can be translated to
    get "#{I18n.t('routename.carspath')}/export", to: 'cars#export'

  end
end

And

# config/locales/nl.yml
nl:
  routename:
    ## methods
    new: 'nieuw'
    edit: 'aanpassen'
    ## resources, etc.
    userpath: 'gebruikers'
    carspath: 'voertuigen' 

Result in:

  • /voertuigen
    • /voertuigen/nieuw
    • /voertuigen/aanpassen
    • /voertuigen/export

update and destroy are not neccesairy since they link into the root as post actions. Save your work ;)

Upvotes: 1

francesc
francesc

Reputation: 46

Probably a little bit late for you, but it may be helpful for others, try a fork of translate_routes:

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

Upvotes: 1

Related Questions