Debian Codegraph @

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

raw
1 use alloc::{string::String, vec::Vec};
2
3 #[path = "./windows_sys.rs"]
4 mod windows_sys;
5 use windows_sys::{GetUserPreferredUILanguages, MUI_LANGUAGE_NAME, TRUE};
6
7 #[allow(clippy::as_conversions)]
8 pub(crate) fn get() -> impl Iterator<Item = String> {
9 let mut num_languages: u32 = 0;
10 let mut buffer_length: u32 = 0;
11
12 // Calling this with null buffer will retrieve the required buffer length
13 let success = unsafe {
14 GetUserPreferredUILanguages(
15 MUI_LANGUAGE_NAME,
16 &mut num_languages,
17 core::ptr::null_mut(),
18 &mut buffer_length,
19 )
20 } == TRUE;
21 if !success {
22 return Vec::new().into_iter();
23 }
24
25 let mut buffer = Vec::<u16>::new();
26 buffer.resize(buffer_length as usize, 0);
27
28 // Now that we have an appropriate buffer, we can query the names
29 let mut result = Vec::with_capacity(num_languages as usize);
30 let success = unsafe {
31 GetUserPreferredUILanguages(
32 MUI_LANGUAGE_NAME,
33 &mut num_languages,
34 buffer.as_mut_ptr(),
35 &mut buffer_length,
36 )
37 } == TRUE;
38
39 if success {
40 // The buffer contains names split by null char (0), and ends with two null chars (00)
41 for part in buffer.split(|i| *i == 0).filter(|p| !p.is_empty()) {
42 if let Ok(locale) = String::from_utf16(part) {
43 result.push(locale);
44 }
45 }
46 }
47
48 result.into_iter()
49 }