pub struct LanguageRegistry { /* private fields */ }Expand description
The language registry: every known tag, with the name and alias tables used to resolve one.
Cloning is cheap: the tables sit behind an Arc.
Implementations§
Source§impl LanguageRegistry
impl LanguageRegistry
Sourcepub fn bundled() -> Result<Self>
pub fn bundled() -> Result<Self>
The registry compiled into this crate. Never touches the network.
let reg = ngano::LanguageRegistry::bundled()?;
assert_eq!(reg.resolve("isiZulu", false), vec!["zul".to_string()]);Sourcepub fn from_json(raw: &str) -> Result<Self>
pub fn from_json(raw: &str) -> Result<Self>
Parse a registry from the JSON shape of language_codes.json.
Sourcepub fn codes(&self) -> &[LanguageCode]
pub fn codes(&self) -> &[LanguageCode]
Every registry entry, in registry order.
Every canonical tag, in registry order.
Sourcepub fn get(&self, tag: &str) -> Option<&LanguageCode>
pub fn get(&self, tag: &str) -> Option<&LanguageCode>
The entry for an exact tag, matched case-insensitively.
let reg = ngano::LanguageRegistry::bundled()?;
assert_eq!(reg.get("ENG-ng").map(|c| c.tag.as_str()), Some("eng-NG"));
assert!(reg.get("Shona").is_none()); // a name, not a tagEvery tag sharing a bare ISO 639-3 code, in registry order, so eng
gives eng, eng-NG, eng-ZA and the rest.
Sourcepub fn resolve(&self, value: &str, include_varieties: bool) -> Vec<String>
pub fn resolve(&self, value: &str, include_varieties: bool) -> Vec<String>
Resolve one caller-supplied language to canonical tags, first match wins:
- an exact tag, case-insensitively, so
sna,SNA,eng-NGandeng-ngall land on the same entry; - a bare ISO 639-3 code that exists only as regional varieties, which resolves to those varieties, because there is nothing else it could mean;
- a name from the registry aliases, case-insensitively, in its plain or its slugified spelling, which is how a free-text name still works.
A tag always beats a name. The one collision in the catalogue is tem,
which is the tag for Timne and is also the name of kdh: the tag wins,
so kdh is reached by its own tag rather than by that name.
A bare code never widens to its regional varieties unless
include_varieties is true, in which case the bare tag comes first and
every <code>-* variety follows in registry order.
Nothing that matches gives an empty vector. Input is never guessed at.
let reg = ngano::LanguageRegistry::bundled()?;
assert_eq!(reg.resolve("SNA", false), vec!["sna".to_string()]);
assert_eq!(reg.resolve("tem", false), vec!["tem".to_string()]); // Timne's tag
assert_eq!(reg.resolve("isiZulu", false), vec!["zul".to_string()]); // a name
assert_eq!(reg.resolve("eng", false), vec!["eng".to_string()]);
assert!(reg.resolve("eng", true).len() > 1);
assert!(reg.resolve("not a language", false).is_empty());Sourcepub fn resolve_all<I, S>(
&self,
values: I,
include_varieties: bool,
) -> Vec<String>
pub fn resolve_all<I, S>( &self, values: I, include_varieties: bool, ) -> Vec<String>
Resolve several values at once, deduplicated, order preserved.