Rails 4 i18n, how to translate routes where subdomain is used for the locale

后端 未结 3 1523
自闭症患者
自闭症患者 2021-02-09 16:27

I am using subdomains to determine the locale in a Rails 4 website. I have this working with a locale switcher exactly the way I want it but now I need to translate the routes a

3条回答
  •  深忆病人
    2021-02-09 17:13

    From my experience, the domain should not be the only thing that determines the locale. It's so much easier if you just put the current locale into a cookie, so that the User can choose in the end!

    Now to the translation-part of your question. I think you should change the way you are using the routing.

    If you see the countries as resources, and the actions as a different part, you will make your life easier. The model to_param method can help you with that:

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

    That will result in urls like south-america/weather but from my perspective, that is even better in terms of readability and search engine optimization. google used to like paths a lot.

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

提交回复
热议问题