| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
#![cfg_attr(any(not(unix), target_vendor = "apple", target_os = "android"), no_std)] |
| 11 |
extern crate alloc; |
| 12 |
use alloc::string::String; |
| 13 |
|
| 14 |
#[cfg(target_os = "android")] |
| 15 |
mod android; |
| 16 |
#[cfg(target_os = "android")] |
| 17 |
use android as provider; |
| 18 |
|
| 19 |
#[cfg(target_vendor = "apple")] |
| 20 |
mod apple; |
| 21 |
#[cfg(target_vendor = "apple")] |
| 22 |
use apple as provider; |
| 23 |
|
| 24 |
#[cfg(all(unix, not(any(target_vendor = "apple", target_os = "android"))))] |
| 25 |
mod unix; |
| 26 |
#[cfg(all(unix, not(any(target_vendor = "apple", target_os = "android"))))] |
| 27 |
use unix as provider; |
| 28 |
|
| 29 |
#[cfg(all(target_family = "wasm", feature = "js", not(unix)))] |
| 30 |
mod wasm; |
| 31 |
#[cfg(all(target_family = "wasm", feature = "js", not(unix)))] |
| 32 |
use wasm as provider; |
| 33 |
|
| 34 |
#[cfg(windows)] |
| 35 |
mod windows; |
| 36 |
#[cfg(windows)] |
| 37 |
use windows as provider; |
| 38 |
|
| 39 |
#[cfg(not(any(unix, all(target_family = "wasm", feature = "js", not(unix)), windows)))] |
| 40 |
mod provider { |
| 41 |
pub fn get() -> impl Iterator<Item = alloc::string::String> { |
| 42 |
core::iter::empty() |
| 43 |
} |
| 44 |
} |
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
pub fn get_locale() -> Option<String> { |
| 65 |
get_locales().next() |
| 66 |
} |
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
pub fn get_locales() -> impl Iterator<Item = String> { |
| 86 |
provider::get() |
| 87 |
} |
| 88 |
|
| 89 |
#[cfg(test)] |
| 90 |
mod tests { |
| 91 |
use super::{get_locale, get_locales}; |
| 92 |
extern crate std; |
| 93 |
|
| 94 |
#[cfg(all(target_family = "wasm", feature = "js", not(unix)))] |
| 95 |
use wasm_bindgen_test::wasm_bindgen_test as test; |
| 96 |
#[cfg(all(target_family = "wasm", feature = "js", not(unix)))] |
| 97 |
wasm_bindgen_test::wasm_bindgen_test_configure!(run_in_browser); |
| 98 |
|
| 99 |
#[test] |
| 100 |
fn can_obtain_locale() { |
| 101 |
assert!(get_locale().is_some(), "no locales were returned"); |
| 102 |
let locales = get_locales(); |
| 103 |
for (i, locale) in locales.enumerate() { |
| 104 |
assert!(!locale.is_empty(), "locale string {} was empty", i); |
| 105 |
assert!( |
| 106 |
!locale.ends_with('\0'), |
| 107 |
"locale {} contained trailing NUL", |
| 108 |
i |
| 109 |
); |
| 110 |
} |
| 111 |
} |
| 112 |
} |