Debian Codegraph @

third_party/rust/sys-locale/src/lib.rs

raw
1 //! A library to safely and easily obtain the current locale on the system or for an application.
2 //!
3 //! This library currently supports the following platforms:
4 //! - Android
5 //! - iOS
6 //! - macOS
7 //! - Linux, BSD, and other UNIX variations
8 //! - WebAssembly on the web (via the `js` feature)
9 //! - Windows
10 #![cfg_attr(
11 any(
12 not(unix),
13 target_os = "macos",
14 target_os = "ios",
15 target_os = "android"
16 ),
17 no_std
18 )]
19 extern crate alloc;
20 use alloc::string::String;
21
22 #[cfg(target_os = "android")]
23 mod android;
24 #[cfg(target_os = "android")]
25 use android as provider;
26
27 #[cfg(any(target_os = "macos", target_os = "ios"))]
28 mod apple;
29 #[cfg(any(target_os = "macos", target_os = "ios"))]
30 use apple as provider;
31
32 #[cfg(all(
33 unix,
34 not(any(target_os = "macos", target_os = "ios", target_os = "android"))
35 ))]
36 mod unix;
37 #[cfg(all(
38 unix,
39 not(any(target_os = "macos", target_os = "ios", target_os = "android"))
40 ))]
41 use unix as provider;
42
43 #[cfg(all(target_family = "wasm", feature = "js", not(unix)))]
44 mod wasm;
45 #[cfg(all(target_family = "wasm", feature = "js", not(unix)))]
46 use wasm as provider;
47
48 #[cfg(windows)]
49 mod windows;
50 #[cfg(windows)]
51 use windows as provider;
52
53 #[cfg(not(any(unix, all(target_family = "wasm", feature = "js", not(unix)), windows)))]
54 mod provider {
55 pub fn get() -> impl Iterator<Item = alloc::string::String> {
56 core::iter::empty()
57 }
58 }
59
60 /// Returns the active locale for the system or application.
61 ///
62 /// This may be equivalent to `get_locales().next()` (the first entry),
63 /// depending on the platform.
64 ///
65 /// # Returns
66 ///
67 /// Returns `Some(String)` with a BCP-47 language tag inside. If the locale
68 /// couldn't be obtained, `None` is returned instead.
69 ///
70 /// # Example
71 ///
72 /// ```no_run
73 /// use sys_locale::get_locale;
74 ///
75 /// let current_locale = get_locale().unwrap_or_else(|| String::from("en-US"));
76 ///
77 /// println!("The locale is {}", current_locale);
78 /// ```
79 pub fn get_locale() -> Option<String> {
80 get_locales().next()
81 }
82
83 /// Returns the preferred locales for the system or application, in descending order of preference.
84 ///
85 /// # Returns
86 ///
87 /// Returns a `Vec` with any number of BCP-47 language tags inside.
88 /// If no locale preferences could be obtained, the vec will be empty.
89 ///
90 /// # Example
91 ///
92 /// ```no_run
93 /// use sys_locale::get_locales;
94 ///
95 /// let mut locales = get_locales();
96 ///
97 /// println!("The most preferred locale is {}", locales.next().unwrap_or("en-US".to_string()));
98 /// println!("The least preferred locale is {}", locales.last().unwrap_or("en-US".to_string()));
99 /// ```
100 pub fn get_locales() -> impl Iterator<Item = String> {
101 provider::get()
102 }
103
104 #[cfg(test)]
105 mod tests {
106 use super::{get_locale, get_locales};
107 extern crate std;
108
109 #[cfg(all(target_family = "wasm", feature = "js", not(unix)))]
110 use wasm_bindgen_test::wasm_bindgen_test as test;
111 #[cfg(all(target_family = "wasm", feature = "js", not(unix)))]
112 wasm_bindgen_test::wasm_bindgen_test_configure!(run_in_browser);
113
114 #[test]
115 fn can_obtain_locale() {
116 assert!(get_locale().is_some(), "no locales were returned");
117 let locales = get_locales();
118 for (i, locale) in locales.enumerate() {
119 assert!(!locale.is_empty(), "locale string {} was empty", i);
120 assert!(
121 !locale.ends_with('\0'),
122 "locale {} contained trailing NUL",
123 i
124 );
125 }
126 }
127 }