Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit e9f04d7

Browse files
keiya01amrbashir
andauthored
fix: set system language to webview on windows, closes #442 (#640)
* fix: set system language to webview on windows * Update .changes/i18n-windows.md Co-authored-by: Amr Bashir <[email protected]>
1 parent 4466250 commit e9f04d7

3 files changed

Lines changed: 26 additions & 3 deletions

File tree

.changes/i18n-windows.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"wry": patch
3+
---
4+
5+
Set webview2 language to match the OS language. This makes i18n functions like `new Date().toLocaleStrin()` behave correctly.

Cargo.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,8 @@ windows-implement = "0.37.0"
7474
"Win32_System_SystemInformation",
7575
"Win32_System_SystemServices",
7676
"Win32_UI_Shell",
77-
"Win32_UI_WindowsAndMessaging"
77+
"Win32_UI_WindowsAndMessaging",
78+
"Win32_Globalization"
7879
]
7980

8081
[target."cfg(any(target_os = \"ios\", target_os = \"macos\"))".dependencies]

src/webview/webview2/mod.rs

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ use windows::{
1919
core::{Interface, PCWSTR, PWSTR},
2020
Win32::{
2121
Foundation::{BOOL, E_FAIL, E_POINTER, FARPROC, HWND, LPARAM, LRESULT, POINT, RECT, WPARAM},
22+
Globalization::{self, MAX_LOCALE_NAME},
2223
System::{
2324
Com::{IStream, StructuredStorage::CreateStreamOnHGlobal},
2425
LibraryLoader::{GetProcAddress, LoadLibraryA},
@@ -95,8 +96,24 @@ impl InnerWebView {
9596

9697
CreateCoreWebView2EnvironmentCompletedHandler::wait_for_async_operation(
9798
Box::new(move |environmentcreatedhandler| unsafe {
98-
let options: ICoreWebView2EnvironmentOptions =
99-
CoreWebView2EnvironmentOptions::default().into();
99+
let options = {
100+
let options: ICoreWebView2EnvironmentOptions =
101+
CoreWebView2EnvironmentOptions::default().into();
102+
103+
// Get user's system language
104+
let lcid = Globalization::GetUserDefaultUILanguage();
105+
let mut lang = [0; MAX_LOCALE_NAME as usize];
106+
Globalization::LCIDToLocaleName(
107+
lcid as u32,
108+
&mut lang,
109+
Globalization::LOCALE_ALLOW_NEUTRAL_NAMES,
110+
);
111+
112+
options
113+
.SetLanguage(PCWSTR(lang.as_ptr()))
114+
.map_err(webview2_com::Error::WindowsError)?;
115+
options
116+
};
100117

101118
// remove "mini menu" - See https://github.com/tauri-apps/wry/issues/535
102119
let _ = options.SetAdditionalBrowserArguments("--disable-features=msWebOOUI,msPdfOOUI");

0 commit comments

Comments
 (0)