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