lang_id/lib.rs
1#![cfg_attr(__unstable_doc, feature(doc_auto_cfg, doc_notable_trait))]
2#![cfg_attr(not(feature = "std"), no_std)]
3
4/*!
5This library provides a series of const lang-ids (language identifiers) which can be found in the
6`consts` module. Additionally, it provides some handy maps.
7
8## Examples
9
10Using the result of a const fn as a value:
11
12```
13use lang_id::LangID;
14
15// Compile-time verified language ID
16const DEFAULT_LANG: LangID = lang_id::consts::lang_id_en();
17```
18
19Description-data Lookup (requires map feature)
20
21```
22# #[cfg(feature = "map")]
23# {
24let map = lang_id::maps::description::map();
25let gsw_fr = map.get("gsw-FR");
26assert_eq!(gsw_fr, Some(&"Schwiizertüütsch, Latiinisch, Frankriich"));
27
28let zh = map.get("zh");
29assert_eq!(zh, Some(&"简体中文, 中国"));
30
31let ja = map.get("ja");
32assert_eq!(ja, Some(&"日本語, 日本語の文字, 日本"));
33# }
34```
35
36## Features
37
38| Feature | Dependencies | Description |
39| -------------- | --------------------- | -------------------------------------------------------------------------------------------------------------------- |
40| **std** | None | Enables stdlib integrations |
41| **map** | `phf`, `tinystr` | Adds precomputed static maps:<br>- Perfect hash maps for O(1) lookups<br>- Compact string storage with TinyStr |
42| **sys-locale** | `sys-locale`, **std** | System locale detection:<br>- Cross-platform locale querying<br>- Integration with OS settings |
43| **match** | None | Match the value or function name in consts using bytes, for example, b"en-001" => lang_id_en_001(). |
44| **serde** | `unic-langid/serde` | Serialization/deserialization support |
45
46*/
47
48pub mod consts;
49mod id;
50pub use unic_langid::LanguageIdentifier as LangID;
51
52pub mod error {
53 pub use unic_langid::LanguageIdentifierError as LangidError;
54}
55
56#[cfg(feature = "map")]
57pub mod maps;
58
59#[cfg(feature = "match")]
60pub mod matches;
61
62/// The sys-locale module is used to get the current system's locale and convert
63/// it into LangID.
64#[cfg(feature = "sys-locale")]
65pub mod sys_locale;
66
67#[cfg(feature = "std")]
68#[cfg(test)]
69mod tests {
70 use super::*;
71
72 // #[test]
73 // #[cfg(feature = "map")]
74 // fn minimize_zh_sg() {
75 // let map = crate::maps::min::map();
76 // let sg = map["zh-Hans-SG"];
77 // assert_eq!(sg, "zh-SG");
78 // }
79 #[test]
80 #[cfg(feature = "map")]
81 fn get_const_ids() {
82 let now = std::time::Instant::now();
83 let map = maps::description::map();
84 for (k, v) in &map {
85 println!("{k} {v}")
86 }
87 // let id = match_id(Box::new("en-GB").as_bytes());
88 // let ja = map.get("ja");
89 // println!("{id:?}");
90 // dbg!(id);
91 println!("{:?}", now.elapsed());
92 // dbg!(id);
93 }
94
95 #[test]
96 fn test_get_en() {
97 let default_lang = crate::consts::lang_id_en();
98 dbg!(default_lang.language);
99 }
100}