Rails 4 i18n how to translate routes in which a subdomain is used for a locale

I use subdomains to determine the locale on the Rails 4 website. I have work with the language switch exactly the way I want, but now I need to translate the routes and I'm not sure what is the best way to proceed.

I looked at https://github.com/kwi/i18n_routing i18n routing gem, but this does not work with subdomains, it seems to change the path by adding / locale, which is not what I need.

Other gems seem obsolete with Rails 4.

Edit

Basically, I want to be able to use the same view helpers, but change the URLs to use any language that is reflected by the selected subdomain. this is just a translation of the route.

I have language-specific templates that work, and I can create navigation templates for specific languages, but I really would like not to worry about changing the erb and url erb labels.

End editing

Sample of routes. rb

scope module: 'countries', shallow: true do get 'south_american', to: 'south_america#index', as: :south_america scope module: 'south_america' do get 'south-america-weather', to: 'weather#index', as: :south_america_weather get 'south-america-gps-maps', to: 'gps#index', as: :south_america_gps get 'south-america-accommodation', to: 'hotels#index', as: :south_america_hotels get 'south-america-vacations', to: 'vacations#index', as: :south_america_vacations get 'south-america-facts', to: 'facts#index', as: :south_america_facts end 

Using south_america_hotels_path as an example will result in a URL for

south america accommodation

which is great, but how to translate it so that when I am in the Spanish subdomain, south_america_hotels_path will create the following URL

Hoteles en sudamerica

UPDATE

Just as this will work for the url, not just the path, so south_america_hotels_url will generate

en.some_site / south america location

and when on the Spanish subdomain I get

es.some_site / Hoteles-en-Sudamerica

etc. for different involved locales.

I am happy to use yml files for translations for URLs or for defining additional routes in the routes.rb file, or an option, but I would prefer to define a URL in route.rb, but I cannot find a way to provide language specific URLs addresses for the same: as a path / url parameter based on a subdomain.

Update and further clarification in response to previous answers.

Changing URLs is not an option; they must match existing URLs. I just need to know how to translate them from the perspective of a view helper without changing view helpers.

+6
source share
3 answers

This is what worked for me, you can try, on your routes, for example, you can put:

 AppName::Application.routes.draw do get "/#{I18n.t("index")}", :to => "pages#index", :as => "index" get "/#{I18n.t("contact")}", :to => "pages#contact", :as => "contact" end 

this will lead you to the default locale routes at application startup, however at run time, when you change your language, you need to redraw the routes to get routes for the new locale.

 I18n.locale = :es #different locale AppName::Application.reload_routes! 

Assuming your default_locale :en , this example will take the first routes from:

 en: index: "index-in-english" contact: "contact-in-english" 

and when the application reloads the routes, it will accept translations from your new locale, in this example from

 es: index: "index-in-spanish" contact: "contact-in-spanish" 

This way you will display the pages # index and pages # contact, and it will change the route only, you do not need to touch your code. Hope this helps you

EDIT:

This stone is my recommended way to do this. It supports several languages ​​without reloading routes (which was not in my example). You have to change very small but more consistent than the code above. route translator

+14
source

From my experience, the domain should not be the only one defining the locale. It is much easier if you just put the current locale in a cookie so that the user can select at the end!

Now to the transferable part of your question. I think you should change the way you use routing.

If you see countries as resources and actions as different, you will make your life easier. The to_param model to_param can help you with this:

 resource :country do get :weather, on: :member ... end 

This will result in URLs like south-america/weather , but from my point of view, it is even better in terms of readability and search engine optimization. Google often used paths.

For different translations of your paths, you can iterate over your domains and provide a translation for each segment: http://guides.rubyonrails.org/routing.html#translated-paths

+2
source

To translate routes into a Rails 4+ application, look at route_translator . It allows you to translate routes and comes with several configuration options, such as adding a locale section to the generated paths or supporting a subdomain application.

+1
source

All Articles