0

I have a rails 4.0.0 app running with friendly_id and globalize (even if freindly_id and globalize might not matter for this question).

Part of my routes.rb is:

scope "(:locale)", locale: /en|de/ do
  resources :mainclasses, :path => :types
end

I now would like to have the ":path => :types" just for the english version (creating a url like www.website.com/types/etc, the german version should be ":path => :typen" (creating www.website.com/de/typen/etc).

Is there a way to change the path depending on the current locale?

user929062
  • 807
  • 4
  • 12
  • 33

1 Answers1

0

I suggest you take a look at route_translator gem. It'll help you translate your routes to any locale, handle the locale with a scope or a subdomain, etc.

From your example, your would have something like this:

MyApp::Application.routes.draw do
  localized do
    resources :mainclasses, path: :types
  end
end

Along with a locale file routes.yml:

en:
  routes:
    types: types
de:
  routes:
    types: typen

Hope it helps.

dgilperez
  • 10,716
  • 8
  • 68
  • 96
  • It basically works. But for the english version I skip the /en/ locale in the url... and this solution adds the /en/ again into the url. How can I just add the /de/ and skip the /en/? – user929062 Feb 09 '15 at 10:48
  • Take a look at the configuration: https://github.com/enriclluelles/route_translator#available-configurations , there is one ``force_locale`` param that you can set to ``false`` for that. – dgilperez Feb 09 '15 at 16:14
  • Yes, I have checked this. But I need to keep the /de/ while I do not use the /en/. I do not use the /en/ since I have already a running website in English and I do not want to change any url for this language version. For the German version I need the /de/ in order to avoid duplicate urls. I now guess that route_translator will not work for me. – user929062 Feb 09 '15 at 16:50
  • Double check the docs. As fas as I understand, the force_locale: false option gives you just that: keeping the english (default) version without ``/en`` prepended, and adding the locale (``/de/``) for the rest of the languages. This is the philosophy behind the original gem (that's a fork). I'm using this sister gem exactly in that way: https://github.com/francesc/rails-translate-routes – dgilperez Feb 09 '15 at 16:53