Skip to main content

Crate cbcp

Crate cbcp 

Source
Expand description

Deterministic BCP 47 locale-ID casting and vendor code mapping.

cbcp is the syntax layer below correspondence libraries: it casts locale identifiers between their spellings without interpreting them. No tables of meaning live here — no locale resolution, no country keywords, no variant mapping, no greetings. Those belong to cletter and friends, which compose this crate.

Two spellings, one rule:

  • Storage/wire form is lowercase (de-ch): filesystem paths, R2 keys, bundle variant IDs, URLs, PDF metadata. Produced by normalize_locale_id.
  • Display form is BCP 47 (de-CH): UI labels and vendor adapters that demand their own casing. Produced by to_bcp47.

Vendor adapters (deepl_source, deepl_target, google_language) transliterate canonical codes into vendor-expected codes. They track vendor documentation; a vendor change is a new cbcp minor version.

tests/vectors/*.json is the executable contract every port (Rust, TypeScript, Python, Typst) implements verbatim.

Functions§

base_language
Extract the base ISO 639 language from a locale ID, lowercased. DE-CH → de, en → en. Never fails; may return garbage for garbage.
deepl_source
DeepL source language code: uppercase ISO 639 base. de-CH → DE.
deepl_target
DeepL target language code. Regional defaults (EN-GB, PT-PT, ZH-HANS) are explicit product-wide contracts: every tool renders the same default for a bare language. Anything unmapped falls back to the uppercase base.
google_language
Google language code (Maps/Places/Translate): lowercase ISO 639 base. de-CH → de.
is_well_formed
Structural well-formedness only: ll or lll alpha, then -xx… segments of 2–8 alphanumerics. No registry involved — xx-yy passes, en__US and "" do not.
locale_eq
Case- and separator-insensitive locale equality via normalization. locale_eq("DE_CH", "de-ch") is true.
normalize_locale_id
Normalize an explicit locale ID’s spelling without inference or fallback.
to_bcp47
Project a locale ID into BCP 47 display casing.