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