Skip to content

Commit

Permalink
fix: set system language to webview on windows, closes #442 (#640)
Browse files Browse the repository at this point in the history
* fix: set system language to webview on windows

* Update .changes/i18n-windows.md

Co-authored-by: Amr Bashir <amr.bashir2015@gmail.com>
  • Loading branch information
keiya01 and amrbashir committed Jul 25, 2022
1 parent 4466250 commit e9f04d7
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 3 deletions.
5 changes: 5 additions & 0 deletions .changes/i18n-windows.md
@@ -0,0 +1,5 @@
---
"wry": patch
---

Set webview2 language to match the OS language. This makes i18n functions like `new Date().toLocaleStrin()` behave correctly.
3 changes: 2 additions & 1 deletion Cargo.toml
Expand Up @@ -74,7 +74,8 @@ windows-implement = "0.37.0"
"Win32_System_SystemInformation",
"Win32_System_SystemServices",
"Win32_UI_Shell",
"Win32_UI_WindowsAndMessaging"
"Win32_UI_WindowsAndMessaging",
"Win32_Globalization"
]

[target."cfg(any(target_os = \"ios\", target_os = \"macos\"))".dependencies]
Expand Down
21 changes: 19 additions & 2 deletions src/webview/webview2/mod.rs
Expand Up @@ -19,6 +19,7 @@ use windows::{
core::{Interface, PCWSTR, PWSTR},
Win32::{
Foundation::{BOOL, E_FAIL, E_POINTER, FARPROC, HWND, LPARAM, LRESULT, POINT, RECT, WPARAM},
Globalization::{self, MAX_LOCALE_NAME},
System::{
Com::{IStream, StructuredStorage::CreateStreamOnHGlobal},
LibraryLoader::{GetProcAddress, LoadLibraryA},
Expand Down Expand Up @@ -95,8 +96,24 @@ impl InnerWebView {

CreateCoreWebView2EnvironmentCompletedHandler::wait_for_async_operation(
Box::new(move |environmentcreatedhandler| unsafe {
let options: ICoreWebView2EnvironmentOptions =
CoreWebView2EnvironmentOptions::default().into();
let options = {
let options: ICoreWebView2EnvironmentOptions =
CoreWebView2EnvironmentOptions::default().into();

// Get user's system language
let lcid = Globalization::GetUserDefaultUILanguage();
let mut lang = [0; MAX_LOCALE_NAME as usize];
Globalization::LCIDToLocaleName(
lcid as u32,
&mut lang,
Globalization::LOCALE_ALLOW_NEUTRAL_NAMES,
);

options
.SetLanguage(PCWSTR(lang.as_ptr()))
.map_err(webview2_com::Error::WindowsError)?;
options
};

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

0 comments on commit e9f04d7

Please sign in to comment.