View Source ClaperWeb.Plugs.Locale (Claper v1.7.0)

Plug to set the locale based on the Accept-Language header.

## Usage

Add the plug to your pipeline in router.ex:

  pipeline :browser do
    ...
    plug ClaperWeb.Plugs.Locale
  end

## Configuration

The plug will use the :default_locale configuration value as the default locale. If the :default_locale is not set, it will default to :en.

## Accept-Language header

The plug will parse the Accept-Language header and set the locale to the first language in the list that is known to the application. If no language is known, the locale will not be changed.

The Accept-Language header is a comma-separated list of language tags with optional quality values. The quality value is a number between 0 and 1, where 1 is the highest quality. The quality value is optional and defaults to 1.

Examples:

  Accept-Language: en-US,en;q=0.8,da;q=0.6

The above example will set the locale to :en if it is known to the application. If :en is not known, it will set the locale to :da if it is known to the application. If neither :en nor :da is known, the locale will not be changed.

  Accept-Language: en-US,en;q=0.8

The above example will set the locale to :en if it is known to the application. If :en is not known, the locale will not be changed.

  Accept-Language: en-US

The above example will set the locale to :en if it is known to the application. If :en is not known, the locale will not be changed.

## Known locales

The plug will only set the locale if it is known to the application. The known locales are determined by the :gettext configuration. The :gettext configuration is set in config/config.exs:

  config :claper, ClaperWeb.Gettext,
    default_locale: "en",
    default_domain: "claper",
    available_locales: ~w(en fr)

The :available_locales option is

Link to this section Summary

Link to this section Functions