Expand description
Locale negotiation, hreflang and language switchers for multi-locale sites.
Extracted from ssg’s i18n plugin (#588). Everything here is a pure
function of its arguments — strings, maps and a strategy in, a string
out. Nothing reads a file or knows what a plugin is, which is what
lets it live outside the generator: ssg-search and ssg-a11y are
the same shape, and it is the only shape that avoids a dependency
cycle, since a crate implementing Plugin would have to depend on
the crate that wants to call it.
What stayed behind in ssg is everything that walks or writes the
filesystem: locale detection, page collection, sitemap emission, and
the Plugin implementation that drives them.
use ssg_i18n::{negotiate_locale, parse_accept_language};
let wanted = parse_accept_language("fr-CA,fr;q=0.9,en;q=0.5");
let have = ["en".to_string(), "fr".to_string()];
assert_eq!(negotiate_locale(&wanted, &have, "en"), "fr");Structs§
- I18n
Config - Parsed
[i18n]configuration section.
Enums§
- UrlPrefix
Strategy - Strategy for constructing locale-specific URLs.
Constants§
- HREFLANG_
MARKER - Sentinel substring used for idempotency checks.
- LANG_
SWITCHER_ ATTR - Attribute that marks an element as the language-switcher placeholder. Survives minification, because a minifier may reformat an element but will not delete it.
- LANG_
SWITCHER_ MARKER - Marker comment embedded in templates where the language switcher should be injected. Kept invisible in single-locale sites.
Functions§
- build_
hreflang_ links - Build the hreflang
<link>block for a single page. - build_
url - Construct a full URL for a given locale + relative path.
- find_
lang_ switcher_ element - Finds the placeholder element carrying
LANG_SWITCHER_ATTRand returns its byte range, including the closing tag. - generate_
lang_ switcher_ html - Generates an HTML snippet for a language switcher navigation.
- generate_
lang_ switcher_ html_ with_ self_ lang - Builds a switcher whose entries link to each locale’s own path.
- negotiate_
locale - Given a list of preferred locales (from Accept-Language) and a list of available locales (directories on disk), returns the best match.
- parse_
accept_ language - Parses an Accept-Language header value into a sorted list of locale preferences (highest quality first).
- rewrite_
ap_ lang_ items - Rewrites existing ap-lang-item links in the page to point to the exact localized path
- sidecar_
candidates - Sidecar file names that could carry
site_rel’s front matter, most likely first.