GamendWeb.Plugs.LocalePath (gamend_web v1.0.1216)

Copy Markdown View Source

Handles the optional locale prefix in the URL path (e.g. /es/about).

Two behaviours, chosen by what the rest of the path points at:

  • Content pages — the allowlist in :localized_paths — are served at the prefixed URL with a 200. The prefix is stripped from path_info before routing, so the router still only ever sees clean paths, but the URL stays /es/about. That is what makes the Spanish version of a page separately indexable: with a redirect there is exactly one indexable URL per page no matter how many locales the site is translated into. These pages are all controller rendered, so there is no LiveView socket that could reconnect at a URL the router does not know.

  • Everything else — the LiveView app pages — keeps the original behaviour: store the locale in the session and redirect to the unprefixed path. A LiveView reconnecting at /es/learning would hit an unmatched route, so those prefixes must not survive routing.

The default locale is never served under a prefix: /en/about redirects to /about so the two do not compete as duplicates.

Assigns :seo_path (the clean, locale-free path) for the root layout to build rel="canonical" and the hreflang alternates from.

Known locales are derived from Gettext.known_locales/1 at compile time.

Summary

Functions

The default locale, served without a prefix.

The locales advertised as hreflang alternates.

Whether clean_path is served under locale prefixes — i.e. whether it is worth advertising hreflang alternates for.

The query parameter a language switcher appends to say the reader chose this language deliberately. Consumed and dropped by this plug — see @switch_param.

Text direction for the dir attribute. Right-to-left scripts must mirror the page layout (WCAG/i18n); everything else is ltr.

Gettext locales use _ (pt_BR); BCP-47, which hreflang requires, uses - (pt-BR). URLs emit the BCP-47 form.

Functions

call(conn, opts)

default_locale()

@spec default_locale() :: String.t()

The default locale, served without a prefix.

hreflang_locales()

@spec hreflang_locales() :: [String.t()]

The locales advertised as hreflang alternates.

Defaults to every known gettext locale; a host narrows it with config :gamend_web, :hreflang_locales, [...] when some translations are too thin to be worth pointing search engines at.

init(opts)

localized_path?(clean_path)

@spec localized_path?(String.t()) :: boolean()

Whether clean_path is served under locale prefixes — i.e. whether it is worth advertising hreflang alternates for.

switch_param()

@spec switch_param() :: String.t()

The query parameter a language switcher appends to say the reader chose this language deliberately. Consumed and dropped by this plug — see @switch_param.

text_direction(locale)

@spec text_direction(String.t()) :: String.t()

Text direction for the dir attribute. Right-to-left scripts must mirror the page layout (WCAG/i18n); everything else is ltr.

url_locale(locale)

@spec url_locale(String.t()) :: String.t()

Gettext locales use _ (pt_BR); BCP-47, which hreflang requires, uses - (pt-BR). URLs emit the BCP-47 form.