0.0
The project is in a healthy, maintained state
Wrap your routes in `localized` and get one translated route per locale, locale-aware URL helpers and I18n.locale set from the URL.
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
 Dependencies

Runtime

>= 7.1
 Project Readme

polyglot_routes

Gem Version CI Coverage Ruby Rails License: MIT

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: editar

With 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.locale from 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