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