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 = "std")]
69#[cfg(test)]
70mod tests {
71  use super::*;
72
73  // #[test]
74  // #[cfg(feature = "map")]
75  // fn minimize_zh_sg() {
76  //   let map = crate::maps::min::map();
77  //   let sg = map["zh-Hans-SG"];
78  //   assert_eq!(sg, "zh-SG");
79  // }
80  #[test]
81  #[cfg(feature = "map")]
82  fn get_const_ids() {
83    let now = std::time::Instant::now();
84    let map = maps::description::map();
85    for (k, v) in &map {
86      println!("{k} {v}")
87    }
88    // let id = match_id(Box::new("en-GB").as_bytes());
89    // let ja = map.get("ja");
90    // println!("{id:?}");
91    // dbg!(id);
92    println!("{:?}", now.elapsed());
93    // dbg!(id);
94  }
95
96  #[test]
97  fn test_get_en() {
98    let default_lang = crate::consts::lang_id_en();
99    dbg!(default_lang.language);
100  }
101}