Add locale parameter to all paths created by gs-route gem in rails

I am using js-routes gem ( https://github.com/railsware/js-routes ) with Rails 4.0.0.

What I want to do is include the current locale string in every route created by js routes in my rails application. And this locale string (for example, "en") will be pulled from the query string for the current query. (Thus, it can change from load of one page to another if the user decides to switch locales.)

In application_controller.rb I have the following to send and receive the locale. This successfully adds the locale parameter to the routes generated by Rails, but not via js routes. (Nothing set in config / routes.rb)

application_controller.rb:

before_filter :set_locale

def set_locale
  I18n.locale = params[:locale] || I18n.default_locale
end

def default_url_options(options={})
  { locale: I18n.locale }
end

javascript Routes.login_path(), /login, /login?locale=en, login_path(). .

, js- 'locale' , , . /login?locale=fr, - .

+4
1

, - , js- (, , i18next)

# config/initializers/js_routes.rb

JsRoutes.setup do |config|
  config.serializer = <<~'JS'
    function(object) {
      object.locale = i18next.language
      return Routes.default_serializer(object)
    }
  JS
end

locale js-:

Routes.some_path() //=> /path/to/page?locale=es-ES
0

Source: https://habr.com/ru/post/1534937/


All Articles