polyglot_routes
Translated Rails routes for every locale. No setup: you only write routes.
Install
gem "polyglot_routes"Use
# config/routes.rb
Rails.application.routes.draw do
localized do
root "pages#home"
resources :products
end
end# config/locales/es.yml
es:
routes:
products: productos
new: nuevo
edit: editarWith I18n.available_locales = [:en, :es] and :en as the default locale:
| Path | Controller | I18n.locale |
|---|---|---|
/products/new |
products#new |
en |
/es/productos/nuevo |
products#new |
es |
/es |
pages#home |
es |
- The default locale has no prefix. Other locales get
/<locale>. - A segment without a translation stays the same.
- Each request sets
I18n.localefrom the URL.
Helpers
products_path # follows I18n.locale
products_path(locale: :es) # "/es/productos"
products_es_path # one helper per locale
switch_locale_path(:en) # current page in another locale (views and controllers)Only some locales: localized(locales: %i[en es]) { ... }.
Devise
Use Devise 4.9.4 or newer. Devise then keeps I18n.locale when authentication fails, so its
flash messages use the locale of the URL
(heartcombo/devise#5567).
Older versions show these messages in the default locale.
Why not route_translator?
route_translator is the mature gem for
this job, and it uses the same localized block. polyglot_routes is a smaller option:
| polyglot_routes | route_translator | |
|---|---|---|
Set I18n.locale from the URL |
Automatic, in every controller | Add around_action :set_locale_from_url
|
| Link to the current page in another locale |
switch_locale_path(:es), keeps the query string |
url_for(locale: "es"), the locale must be a string |
| Configuration | None |
force_locale, hide_locale, host-based locales, unlocalized routes and more |
| Size | About 200 lines | Larger, with years of edge cases fixed |
Use polyglot_routes when the default behaviour is all you need. Use route_translator when you need one of its options.
License
MIT