marzapower
marzapower

Reputation: 5611

Is it possible to localize static pages URLs in Rails?

I recently updated my routes.rb file for a Rails 4.0.4 application to be able to properly handle multiple localizations. It looks something like this:

scope ':locale', locale: /#{I18n.available_locales.join("|")}/ do
  # All the routing happens here
  # ...

  root "home#index", as: :locale_root
  match '*path', to: "locale#not_found", via: :all
end

# handles /
root to: redirect("/#{I18n.default_locale}")
match '*path', to: redirect(status: 307) {|params| "/#{I18n.default_locale}/#{params[:path]}"}, via: :all
match '', to: redirect("/#{I18n.default_locale}"), via: :all

I would like to route static pages being able to change the URL depending on the current URL. Let's say I have a "How It Works" page addressed via http://mydomain/how_it_works. Using multiple localizations I would like to get:

http://mydomain/en/how_it_works  # English
http://mydomain/it/come_funziona # Italian

The router should link to the same controller#action, changing the displayed URL. I've not been able to find a gem or a suitable solution to achieve this, so I'm wondering if Rails is capable of localizing this kind of static URLs.

Moreover, I would like to be able to address these URLs using the same *_path helper in the views, "ignoring" the current locale. Es. if I use how_it_works_path and the locale is :en I get http://mydomain/en/how_it_works, otherwise if the locale is :it I get http://mydomain/it/come_funziona.

Upvotes: 2

Views: 242

Answers (1)

dasbabs
dasbabs

Reputation: 45

I know this was asked 2 years ago but maybe this helps people like me who are facing this issue now.

My problem was quite similar and I ended up using the following gem for a Rails 5 app: https://github.com/enriclluelles/route_translator

Upvotes: 1

Related Questions