Skip to content

Use all languages on multiple hosts

Geremia Taglialatela edited this page Jan 15, 2021 · 1 revision

Before Route Translator 10.0, the host_locales option allowed to use all the languages on multiple hosts. This was causing duplication of routes as described in #171.

Starting from 10.0, this use case is no longer supported out of the box and requires a custom implementation.

This is an example of how the olde behavior could be implemented in 10.0

/config/initializers/route_translator.rb

RouteTranslator.config do |config|
  # Do not set config.host_locales here, but you may want to hide or force locale
end

/config/application.rb

module MyApplication
  class Application < Rails::Application

    # ...

    config.domain_language = {
      'it.lvh.me' => :it,
      'en.lvh.me' => :en,
      'de.lvh.me' => :de
    }

    # ...

  end
end

/app/controllers/application_controller.rb

class ApplicationController < ActionController::Base

  # ...

  private

  # Override Route Translator's method.
  # NOTE: you may want some error management (e.g., fallback on a default language)
  def set_locale_from_url
    I18n.with_locale(Rails.application.config.domain_language[request.host]) do
      yield
    end
  end

  # ...

end

This will allow to use multiple languages for each host