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| **consts** | None | Provides const language ids |
46
47*/
48
49#[cfg(feature = "consts")]
50pub mod consts;
51
52pub mod common;
53
54mod id;
55pub use id::RawID;
56pub use unic_langid::LanguageIdentifier as LangID;
57
58pub mod error {
59 pub type LangidResult<T> = Result<T, LangidError>;
60 pub use tinystr::ParseError as ParseStrError;
61 pub use unic_langid::LanguageIdentifierError as LangidError;
62}
63pub use error::LangidResult as Result;
64
65#[cfg(feature = "map")]
66pub mod maps;
67
68#[cfg(feature = "match")]
69pub mod matches;
70
71/// The sys-locale module is used to get the current system's locale and convert
72/// it into LangID.
73#[cfg(feature = "sys-locale")]
74pub mod sys_locale;
75
76#[cfg(feature = "nostd-sys-locale")]
77pub mod nostd_sys_locale;
78
79#[cfg(feature = "std")]
80#[cfg(test)]
81mod tests {
82 use super::*;
83
84 // #[test]
85 // #[cfg(feature = "map")]
86 // fn minimize_zh_sg() {
87 // let map = crate::maps::min::map();
88 // let sg = map["zh-Hans-SG"];
89 // assert_eq!(sg, "zh-SG");
90 // }
91 #[test]
92 #[cfg(feature = "map")]
93 fn get_const_ids() {
94 let now = std::time::Instant::now();
95 let map = maps::description::map();
96 for (k, v) in &map {
97 println!("{k} {v}")
98 }
99 // let id = match_id(Box::new("en-GB").as_bytes());
100 // let ja = map.get("ja");
101 // println!("{id:?}");
102 // dbg!(id);
103 println!("{:?}", now.elapsed());
104 // dbg!(id);
105 }
106
107 #[test]
108 #[cfg(feature = "consts")]
109 fn test_get_en() {
110 let default_lang = crate::consts::lang_id_en();
111 dbg!(default_lang.language);
112 }
113}