Skip to content

Commit b1c55ec

Browse files
authored
Implement Application::get_locale() for Windows backend (#1874)
* Implement Application::get_locale() for Windows backend * Cleanup Application::get_locale() in Windows backend * Minimize extent of unsafe block * Be less defensive about locales of zero length * Warn on failure to retrieve locale * Formatting in Application::get_locale() for Windows backend
1 parent a08ea03 commit b1c55ec

File tree

2 files changed

+16
-4
lines changed

2 files changed

+16
-4
lines changed

druid-shell/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ version = "0.3.9"
6565
features = ["d2d1_1", "dwrite", "winbase", "libloaderapi", "errhandlingapi", "winuser",
6666
"shellscalingapi", "shobjidl", "combaseapi", "synchapi", "dxgi1_3", "dcomp",
6767
"d3d11", "dwmapi", "wincon", "fileapi", "processenv", "winbase", "handleapi",
68-
"shellapi"]
68+
"shellapi", "winnls"]
6969

7070
[target.'cfg(target_os="macos")'.dependencies]
7171
block = "0.1.6"

druid-shell/src/backend/windows/application.rs

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@ use winapi::shared::windef::{DPI_AWARENESS_CONTEXT_PER_MONITOR_AWARE_V2, HCURSOR
2727
use winapi::shared::winerror::HRESULT_FROM_WIN32;
2828
use winapi::um::errhandlingapi::GetLastError;
2929
use winapi::um::shellscalingapi::PROCESS_PER_MONITOR_DPI_AWARE;
30+
use winapi::um::winnls::GetUserDefaultLocaleName;
31+
use winapi::um::winnt::LOCALE_NAME_MAX_LENGTH;
3032
use winapi::um::winuser::{
3133
DispatchMessageW, GetAncestor, GetMessageW, LoadIconW, PeekMessageW, PostMessageW,
3234
PostQuitMessage, RegisterClassW, TranslateAcceleratorW, TranslateMessage, GA_ROOT,
@@ -40,7 +42,7 @@ use crate::application::AppHandler;
4042
use super::accels;
4143
use super::clipboard::Clipboard;
4244
use super::error::Error;
43-
use super::util::{self, ToWide, CLASS_NAME, OPTIONAL_FUNCTIONS};
45+
use super::util::{self, FromWide, ToWide, CLASS_NAME, OPTIONAL_FUNCTIONS};
4446
use super::window::{self, DS_REQUEST_DESTROY};
4547

4648
#[derive(Clone)]
@@ -194,7 +196,17 @@ impl Application {
194196
}
195197

196198
pub fn get_locale() -> String {
197-
//TODO ahem
198-
"en-US".into()
199+
let mut buf = [0u16; LOCALE_NAME_MAX_LENGTH];
200+
let len_with_null =
201+
unsafe { GetUserDefaultLocaleName(buf.as_mut_ptr(), buf.len() as _) as usize };
202+
let locale = if len_with_null > 0 {
203+
buf.get(..len_with_null - 1).and_then(FromWide::from_wide)
204+
} else {
205+
None
206+
};
207+
locale.unwrap_or_else(|| {
208+
tracing::warn!("Failed to get user locale");
209+
"en-US".into()
210+
})
199211
}
200212
}

0 commit comments

Comments
 (0)