Debian Codegraph @

src/windows.rs

raw symbols SCIP
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>::with_capacity(buffer_length as usize);
26
27 // Now that we have an appropriate buffer, we can query the names
28 let mut result = Vec::with_capacity(num_languages as usize);
29 let success = unsafe {
30 GetUserPreferredUILanguages(
31 MUI_LANGUAGE_NAME,
32 &mut num_languages,
33 buffer.as_mut_ptr(),
34 &mut buffer_length,
35 )
36 } == TRUE;
37
38 if success {
39 // SAFETY: Windows wrote the required length worth of UTF-16 into our buffer, which initialized it.
40 unsafe { buffer.set_len(buffer_length as usize) };
41 // The buffer contains names split by null char (0), and ends with two null chars (00)
42 for part in buffer.split(|i| *i == 0).filter(|p| !p.is_empty()) {
43 if let Ok(locale) = String::from_utf16(part) {
44 result.push(locale);
45 }
46 }
47 }
48
49 result.into_iter()
50 }