PlugLocaleRootRedirect

Travis Hex.pm

PlugLocaleRootRedirect ensures that all requests to the root path (/) are redirected to the best locale path, from a list of supported locales

Installation

Add plug_locale_root_redirect to the deps function in your project’s mix.exs file:

defp deps do
  [
    …,
    {:plug_locale_root_redirect, "~> 0.1"}
  ]
end

Then run mix do deps.get, deps.compile inside your project’s directory.

Usage

PlugLocaleRootRedirect can be used just as any other plugs. Add PlugLocaleRootRedirect before all of the other plugs you want to happen after successful redirection to your locale path.

defmodule Endpoint do
  plug PlugLocaleRootRedirect, locales: %w(en fr)
end

The result:

$ curl -sI "http://localhost:4000" -H "Accept-Language: fr;q=1, en;q=0.8" | grep "302\|Location"
HTTP/1.1 302 Found
Location: /fr

$ curl -sI "http://localhost:4000" -H "Accept-Language: fr;q=0.4, en;q=0.8" | grep "302\|Location"
HTTP/1.1 302 Found
Location: /en

If a request is made to /, it will be redirected to /en or /fr, depending on the request locale preference. If the request has no preference between supported locales, it will be redirected to the first locale, /en in this case.

License

PlugLocaleRootRedirect is © 2017 Rémi Prévost and may be freely distributed under the MIT license. See the LICENSE.md file for more information.