https://github.com/remi/plug_locale_root_redirect
PlugLocaleRootRedirect uses PlugBest to map '/' route to a path based on the Accept-Language HTTP header.
https://github.com/remi/plug_locale_root_redirect
elixir http localization plug
Last synced: 4 months ago
JSON representation
PlugLocaleRootRedirect uses PlugBest to map '/' route to a path based on the Accept-Language HTTP header.
- Host: GitHub
- URL: https://github.com/remi/plug_locale_root_redirect
- Owner: remi
- License: mit
- Created: 2017-02-04T13:59:08.000Z (about 9 years ago)
- Default Branch: master
- Last Pushed: 2019-03-15T14:15:30.000Z (about 7 years ago)
- Last Synced: 2024-10-19T10:06:08.052Z (over 1 year ago)
- Topics: elixir, http, localization, plug
- Language: Elixir
- Homepage: https://hex.pm/packages/plug_locale_root_redirect
- Size: 44.9 KB
- Stars: 0
- Watchers: 3
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE.md
Awesome Lists containing this project
README
PlugLocaleRootRedirect
=================
[](https://travis-ci.org/remiprev/plug_locale_root_redirect)
[](https://hex.pm/packages/plug_locale_root_redirect)
`PlugLocaleRootRedirect` ensures that all requests to the root path (`/`) are
redirected to the best locale path (eg. `/fr`, `/en`, etc.), from a list of supported
locales.
Installation
------------
Add `plug_locale_root_redirect` to the `deps` function in your project's `mix.exs` file:
```elixir
defp deps do
[
…,
{:plug_locale_root_redirect, "~> 0.3"}
]
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.
```elixir
defmodule Endpoint do
plug PlugLocaleRootRedirect, locales: ~w(en fr)
end
```
The result:
```bash
$ 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
$ curl -sI "http://localhost:4000" -H "Accept-Language: es;q=1, ru;q=0.5" | grep "302\|Location"
HTTP/1.1 302 Found
Location: /en
$ curl -sI "http://localhost:4000" | 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-2019 [Rémi Prévost](http://exomel.com) and may be
freely distributed under the [MIT license](https://github.com/remiprev/plug_locale_root_redirect/blob/master/LICENSE.md). See the
`LICENSE.md` file for more information.