{"id":18513351,"url":"https://github.com/jbox-web/route_translator","last_synced_at":"2026-04-25T21:32:49.945Z","repository":{"id":146101463,"uuid":"322166080","full_name":"jbox-web/route_translator","owner":"jbox-web","description":null,"archived":false,"fork":false,"pushed_at":"2025-01-28T22:54:49.000Z","size":87,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":3,"default_branch":"master","last_synced_at":"2025-01-28T23:29:24.299Z","etag":null,"topics":["rails","routes","ruby","translations"],"latest_commit_sha":null,"homepage":"","language":"Ruby","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/jbox-web.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2020-12-17T03:13:16.000Z","updated_at":"2025-01-28T22:54:53.000Z","dependencies_parsed_at":"2024-03-15T15:33:06.595Z","dependency_job_id":"357ff68e-f117-439a-ba25-8ae8827c1338","html_url":"https://github.com/jbox-web/route_translator","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jbox-web%2Froute_translator","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jbox-web%2Froute_translator/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jbox-web%2Froute_translator/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jbox-web%2Froute_translator/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/jbox-web","download_url":"https://codeload.github.com/jbox-web/route_translator/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":239226436,"owners_count":19603265,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["rails","routes","ruby","translations"],"created_at":"2024-11-06T15:37:46.740Z","updated_at":"2026-04-25T21:32:44.926Z","avatar_url":"https://github.com/jbox-web.png","language":"Ruby","funding_links":[],"categories":[],"sub_categories":[],"readme":"# RouteTranslator\n\n[![GitHub license](https://img.shields.io/github/license/jbox-web/route_translator.svg)](https://github.com/jbox-web/route_translator/blob/master/LICENSE)\n[![GitHub release](https://img.shields.io/github/release/jbox-web/route_translator.svg)](https://github.com/jbox-web/route_translator/releases/latest)\n[![CI](https://github.com/jbox-web/route_translator/workflows/CI/badge.svg)](https://github.com/jbox-web/route_translator/actions)\n\nRouteTranslator is a gem to allow you to manage the translations of your Rails application routes with a simple dictionary format.\n\nIt started as a rewrite of the awesome [route_translator](https://github.com/enriclluelles/route_translator) plugin by Geremia Taglialatela to [keep support of Rails engines](https://github.com/enriclluelles/route_translator/issues/178).\n\nRight now it works with Rails 5.2 and Rails 6.x.\n\n## Installation\n\nPut this in your `Gemfile` :\n\n```ruby\ngit_source(:github){ |repo_name| \"https://github.com/#{repo_name}.git\" }\n\ngem 'route_translator', github: 'jbox-web/route_translator', tag: '1.0.0'\n```\n\nthen run `bundle install`.\n\n\n## Usage\n\n1. First configure RouteTranslator in Rails application\n\n```ruby\nmodule Dummy\n  class Application \u003c Rails::Application\n    # Extend application class to add .add_route_translator_for method\n    extend RouteTranslator::Railtie::Localized\n\n    # Declare our locales as usual\n    config.i18n.available_locales = %i[fr en es pt]\n    config.i18n.default_locale    = :fr\n    config.i18n.fallbacks         = [:fr, { en: %i[en fr], es: %i[es fr], pt: %i[pt es fr] }]\n\n    # Configure RouteTranslator\n    add_route_translator_for(:main_site, {\n      default_locale:    :fr,\n      available_locales: %i[fr es pt en],\n    })\n  end\nend\n```\n\nHere `:main_site` is just an ID to store RouteTranslator config for the current Rails application.\n\nIt's mainly useful when you have Rails engines in your application (see below), but still, you have to give one.\n\n2. Configure your `ApplicationController`\n\n```ruby\nclass ApplicationController \u003c ActionController::Base\n  # Prevent CSRF attacks by raising an exception.\n  # For APIs, you may want to use :null_session instead.\n  protect_from_forgery with: :exception\n\n  # Configure RouteTranslator\n  localized :main_site\nend\n```\n\n3. Localize your routes\n\n```ruby\nRails.application.routes.draw do\n  root 'welcome#index'\n\n  resources :posts\n\n  localized(:main_site) do\n    get 'about', to: 'pages#about', as: :about\n  end\n\nend\n```\n\n4. And add the translations to your locale files, for example :\n\n```yml\nen:\n  routes:\n    about: about\n\nfr:\n  routes:\n    about: a-propos\n```\n\n5. Your routes are now translated! Here's the output of your `bin/rails routes` :\n\nBefore :\n\n```sh\n    Prefix Verb   URI Pattern                 Controller#Action\n      root GET    /                           welcome#index\n     posts GET    /posts(.:format)            posts#index\n           POST   /posts(.:format)            posts#create\n  new_post GET    /posts/new(.:format)        posts#new\n edit_post GET    /posts/:id/edit(.:format)   posts#edit\n      post GET    /posts/:id(.:format)        posts#show\n           PATCH  /posts/:id(.:format)        posts#update\n           PUT    /posts/:id(.:format)        posts#update\n           DELETE /posts/:id(.:format)        posts#destroy\n     about GET    /about(.:format)            pages#about\n```\n\nAfter :\n\n```sh\n    Prefix Verb   URI Pattern                 Controller#Action\n      root GET    /                           welcome#index\n     posts GET    /posts(.:format)            posts#index\n           POST   /posts(.:format)            posts#create\n  new_post GET    /posts/new(.:format)        posts#new\n edit_post GET    /posts/:id/edit(.:format)   posts#edit\n      post GET    /posts/:id(.:format)        posts#show\n           PATCH  /posts/:id(.:format)        posts#update\n           PUT    /posts/:id(.:format)        posts#update\n           DELETE /posts/:id(.:format)        posts#destroy\n  about_fr GET    /a-propos(.:format)         pages#about {:locale=\u003e\"fr\"}\n  about_es GET    /es/sobre(.:format)         pages#about {:locale=\u003e\"es\"}\n  about_pt GET    /pt/quem-e(.:format)        pages#about {:locale=\u003e\"pt\"}\n  about_en GET    /en/about(.:format)         pages#about {:locale=\u003e\"en\"}\n```\n\nNote that only the routes inside a localized block are translated.\n\n\n## Options\n\nAccepted options for `add_route_translator_for` :\n\nOptions              | Default value            | Description\n---------------------|--------------------------|------------\n`:default_locale`    | `I18n.default_locale`    | RouteTranslator adds the locale to all generated route paths, except for the default locale.\n`:available_locales` | `I18n.available_locales` | Limits the locales for which URLs should be generated for. Accepts an array of strings or symbols.\n`:locale_param_key`  | `:locale`                | The param key used to set the locale to the newly generated routes.\n`:disable_fallback`  | `false`                  | Creates routes only for locales that have translations. For example, if we have /examples and a translation is not provided for es, the route helper of examples_es will not be created.\n\n\n## I18n\n\nIf you want to set `I18n.locale` from the url parameter locale, add the following line in your `ApplicationController` :\n\n```ruby\naround_action :set_locale_from_params\n```\n\n**Note :** you might be tempted to use `before_action` instead of `around_action` : just don't. That could lead to [thread-related issues](https://github.com/rails/rails/pull/34356).\n\nIf you use engines don't forget to add it in your engine `ApplicationController` (See: https://github.com/rails/rails/pull/34356#issuecomment-435110862)\n\n\n## With Rails engines\n\nIt's the same procedure that applies for Rails engine :\n\n1. First configure RouteTranslator in your Rails engine\n\n```ruby\nmodule Bar\n  class Engine \u003c ::Rails::Engine\n    isolate_namespace Bar\n    # Extend application class to add .add_route_translator_for method\n    extend RouteTranslator::Railtie::Localized\n\n    # Configure RouteTranslator\n    add_route_translator_for(:bar_engine, {\n      default_locale:    :es,\n      available_locales: %i[es pt en],\n    })\n  end\nend\n```\n\n2. Configure your Rails engine `ApplicationController`\n\n```ruby\nmodule Bar\n  class ApplicationController \u003c ActionController::Base\n    layout 'bar/application'\n\n    # Configure RouteTranslator\n    localized :bar_engine\n  end\nend\n```\n\n3. Localize your routes\n\n```ruby\nBar::Engine.routes.draw do\n  root 'welcome#index'\n\n  localized(:bar_engine) do\n    get 'about',   to: 'pages#about', as: :about\n    get 'unnamed', to: 'pages#unnamed'\n\n    get 'people.:format', to: 'people#index'\n\n    resources :products\n  end\nend\n```\n\n4. Mount your engine in Rails application :\n\n```ruby\nRails.application.routes.draw do\n  root 'welcome#index'\n\n  localized(:main_site) do\n    get 'about', to: 'pages#about', as: :about\n  end\n\n  mount Bar::Engine, at: '/bar', as: 'bar_site'\nend\n```\n\n\n## Namespaces\n\nYou can translate a namespace route by either its `name` or `path` option :\n\n1. Wrap the namespaces that you want to translate inside a `localized` block:\n\n```ruby\nRails.application.routes.draw do\n  localized(:main_site) do\n    namespace :admin do\n      resources :cars, only: :index\n    end\n\n    namespace :sold_cars, path: :sold do\n      resources :cars, only: :index\n    end\n  end\nend\n```\n\n2. And add the translations to your locale files, for example:\n\n```yml\nes:\n  routes:\n    admin: administrador\n    cars: coches\n    new: nuevo\n    sold: vendidos\n\nfr:\n  routes:\n    admin: administrateur\n    cars: voitures\n    new: nouveau\n    sold: vendues\n```\n\n3. Your namespaces are translated! Here's the output of your `bin/rails routes` :\n\n```sh\n             Prefix Verb URI Pattern                           Controller#Action\n      admin_cars_fr GET  /fr/administrateur/voitures(.:format) admin/cars#index {:locale=\u003e\"fr\"}\n      admin_cars_es GET  /es/administrador/coches(.:format)    admin/cars#index {:locale=\u003e\"es\"}\n      admin_cars_en GET  /admin/cars(.:format)                 admin/cars#index {:locale=\u003e\"en\"}\n  sold_cars_cars_fr GET  /fr/vendues/voitures(.:format)        sold_cars/cars#index {:locale=\u003e\"fr\"}\n  sold_cars_cars_es GET  /es/vendidos/coches(.:format)         sold_cars/cars#index {:locale=\u003e\"es\"}\n  sold_cars_cars_en GET  /sold/cars(.:format)                  sold_cars/cars#index {:locale=\u003e\"en\"}\n```\n\n\n## Inflections\n\nAt the moment inflections are not supported, but you can use the following workaround :\n\n```ruby\nlocalized(:main_site) do\n  resources :categories, path_names: { new: 'new_category' }\nend\n```\n\n```yml\nen:\n  routes:\n    category: category\n    new_category: new\n\nes:\n  routes:\n    category: categoria\n    new_category: nueva\n```\n\n```sh\n          Prefix Verb   URI Pattern                       Controller#Action\n   categories_es GET    /es/categorias(.:format)          categories#index {:locale=\u003e\"es\"}\n   categories_en GET    /categories(.:format)             categories#index {:locale=\u003e\"en\"}\n                 POST   /es/categorias(.:format)          categories#create {:locale=\u003e\"es\"}\n                 POST   /categories(.:format)             categories#create {:locale=\u003e\"en\"}\n new_category_es GET    /es/categorias/nueva(.:format)    categories#new {:locale=\u003e\"es\"}\n new_category_en GET    /categories/new(.:format)         categories#new {:locale=\u003e\"en\"}\nedit_category_es GET    /es/categorias/:id/edit(.:format) categories#edit {:locale=\u003e\"es\"}\nedit_category_en GET    /categories/:id/edit(.:format)    categories#edit {:locale=\u003e\"en\"}\n     category_es GET    /es/categorias/:id(.:format)      categories#show {:locale=\u003e\"es\"}\n     category_en GET    /categories/:id(.:format)         categories#show {:locale=\u003e\"en\"}\n                 PATCH  /es/categorias/:id(.:format)      categories#update {:locale=\u003e\"es\"}\n                 PATCH  /categories/:id(.:format)         categories#update {:locale=\u003e\"en\"}\n                 PUT    /es/categorias/:id(.:format)      categories#update {:locale=\u003e\"es\"}\n                 PUT    /categories/:id(.:format)         categories#update {:locale=\u003e\"en\"}\n                 DELETE /es/categorias/:id(.:format)      categories#destroy {:locale=\u003e\"es\"}\n                 DELETE /categories/:id(.:format)         categories#destroy {:locale=\u003e\"en\"}\n```\n\n\n## Notes\n\nWrapping a mount within `localized()` is not supported.\n\nIMHO doing this is a non-sense (unless you explain why I'm wrong with a real life example), so don't do it.\n\n```ruby\nRails.application.routes.draw do\n  root 'welcome#index'\n\n  localized(:main_site) do\n    get 'about', to: 'pages#about', as: :about\n    mount Bar::Engine, at: '/bar', as: 'bar_site'\n  end\nend\n```\n\n\n## Development\n\nThe specs embeds a dummy application with some [real life example](/spec/dummy/config/routes.rb).\n\nTo test it :\n\n```sh\n$ echo \"127.0.0.1 main-domain.local foo-domain.local bar-domain.local\" \u003e\u003e /etc/hosts\n$ bundle install\n$ cd spec/dummy\n$ bin/rails s\n```\n\nOpen your navigator on :\n\n* http://main-domain.local:3000/\n* http://foo-domain.local:3000/\n* http://bar-domain.local:3000/\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjbox-web%2Froute_translator","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fjbox-web%2Froute_translator","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjbox-web%2Froute_translator/lists"}