ZoonkWeb.Language (Zoonk v0.1.0-dev)

View Source

Plugs and LiveView hooks for setting the application language.

Summary

Functions

Retrieves the browser's preferred language from the accept-language header.

Sets the application language.

Sets the session language based on user preferences or browser settings.

Functions

get_browser_language(conn)

Retrieves the browser's preferred language from the accept-language header.

If the browser's preferred language is supported, it is returned. Otherwise, the default language is returned.

Examples

iex> conn = %Plug.Conn{req_headers: [{"accept-language", "en-US,en;q=0.9"}]}
iex> ZoonkWeb.Language.get_browser_language(conn)
"en"

on_mount(atom, params, session, socket)

Sets the application language.

It works based on the user's preference or the session language, which is set by ZoonkWeb.Plugs.Language.

on_mount arguments

  • :set_app_language - Sets the application language based on the user's preference or the session language.

Examples

Use the live_session of your router to invoke the on_mount callback:

alias ZoonkWeb.Hooks

live_session :my_liveview,
  on_mount: [{Hooks.Language, :set_app_language}] do
  # my code here
end

set_session_language(conn, opts)

Sets the session language based on user preferences or browser settings.

If the user has a preferred language set in their profile, it is used. Otherwise, the language is determined from the accept-language header.

The selected language is then stored in the session.