GithubHelp home page GithubHelp logo

szsoppa / set_locale Goto Github PK

View Code? Open in Web Editor NEW

This project forked from smeevil/set_locale

0.0 1.0 0.0 160 KB

elixir phoenix plug that can extract locale from browser headers or paths for redirection and setting of get text locales

License: Do What The F*ck You Want To Public License

Elixir 100.00%

set_locale's Introduction

SetLocale

This phoenix plug will help you with I18n url paths. It can extract the preffered locale from the browsers accept-language header and redirect if an url without locale has been given. It will extract the locale from the url and check if that is valid and supported. If so it will assign it to conn.assigns.locale and set Gettext to that locale as well. If it is not supported it will redirect to the default locale.

You might also be interested in ecto_translate which can help you with returning translated values of your Ecto data attributes.

Examples

Given that you define your default language to be "en" :

When someone uses the url : http://www.example.org they will be redirected to http://www.example.org/en/

When someone uses the url : http://www.example.org/foo/bar/baz they will be redirected to http://www.example.org/en/foo/bar/baz

When someone uses the url : http://www.example.org/en-gb/foo/bar/baz they will be redirected to http://www.example.org/en/foo/bar/baz

When someone uses an unsupported locale in the url they will be redirected to the default one: http://www.example.org/de-de/foo/bar/baz they will be redirected to http://www.example.org/en/foo/bar/baz

When someone uses a url with no locale prefix, and their browser contains an accept-language string that contains a supported locale : http://www.example.org/foo/bar/baz they will be redirected to http://www.example.org/nl-nl/foo/bar/baz

Fallback chain and precedence

The current precedence and fallback chain is now :

  • locale in url (i.e. /nl-nl/)
  • cookie
  • request headers accept-language
  • default locale from config

Setup

Update your router.ex to include the plug and scope your routes with /:locale

defmodule MyApp.Router do
  use MyApp.Web, :router

  pipeline :browser do
    plug :accepts, ["html"]
    ...
    plug SetLocale, gettext: MyApp.Gettext, default_locale: "en", cookie_key: "project_locale" #cookie_key is optional
  end

  ...

  scope "/", MyApp do
    pipe_through :browser
    get "/", PageController, :dummy #you need this entry to support the default root without a locale, it will never be called
  end

  scope "/:locale", MyApp do
    pipe_through :browser
    get "/", PageController, :index
    ...
  end
end

Installation

If available in Hex, the package can be installed as:

  1. Add set_locale to your list of dependencies in mix.exs:
```elixir
def deps do
  [{:set_locale, "~> 0.2.1"}]
end
```
  1. Ensure set_locale is started before your application:
```elixir
def application do
  [applications: [:set_locale]]
end
```

set_locale's People

Contributors

smeevil avatar dirkholzapfel avatar mtarnovan avatar roques avatar scarfacedeb avatar szsoppa avatar narnach avatar

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.