Skip to main content

Crate ssg_i18n

Crate ssg_i18n 

Source
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§

I18nConfig
Parsed [i18n] configuration section.

Enums§

UrlPrefixStrategy
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_ATTR and 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.