Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

better l18n support in webview (navigator.language) #442

Closed
tauri-apps bot opened this issue Oct 30, 2021 · 15 comments · Fixed by #632
Closed

better l18n support in webview (navigator.language) #442

tauri-apps bot opened this issue Oct 30, 2021 · 15 comments · Fixed by #632
Labels
help wanted Help is requested to fix this issue platform: All type: feature request

Comments

@tauri-apps
Copy link

tauri-apps bot commented Oct 30, 2021

This issue has been upstreamed from tauri-apps/tauri#2735

Is your feature request related to a problem? Please describe.
I want to rely on navigator.language or navigator.languages in the webview to determine which language a user prefers and to then translate the app.

On WSL 2 / Ubuntu 20 the language is just "c", on Windows 10 with WebView2 it does not respect language settings on the system or browser (Edge) level, on native Ubuntu 20 it had a language that was actually more accurate then the system setting, but did not respect the system setting as well. I don't have access to a macos device.

Describe the solution you'd like
Ideally navigator.language or navigator.languages contain languages that match the system level user preferences.

Describe alternatives you've considered
One could call system level APIs (different for every platform, i guess) from Rust to determine the preferences and expose those to Rust and finally the webview. Without changing navigator.language or navigator.languages this approach is not ideal, because some libraries rely on those fields. Maybe an approach where those preferences are exposed on the navigator and additionally have exposed to Rust and Webview via functions.

@amrbashir amrbashir changed the title better l18n support in webview better l18n support in webview (navigator.language) Dec 21, 2021
@wusyong wusyong added platform: All help wanted Help is requested to fix this issue type: feature request labels Dec 27, 2021
@keiya01
Copy link
Member

keiya01 commented Apr 25, 2022

I switched the MacOS system language setting from "English" to "Japanese" and it seems to be working as intended.

@lucasfernog
Copy link
Member

lucasfernog commented Apr 25, 2022

navigator.language returns en-US in my Arch Linux; maybe the OP didn't set up the system language properly?

edit: ahh it's WSL2, maybe that's the problem.

@lucasfernog
Copy link
Member

I can't find a way to change it in Windows though :(

@lucasfernog
Copy link
Member

Looks like this is the way to read and set the language in webview2: https://docs.microsoft.com/en-us/dotnet/api/microsoft.web.webview2.core.corewebview2environmentoptions.language?view=webview2-dotnet-1.0.902.49

See MicrosoftEdge/WebView2Feedback#833

@keiya01
Copy link
Member

keiya01 commented Apr 25, 2022

It looks good. I will try to implement it.

@lucasfernog
Copy link
Member

I manage to overwrite navigator.language with this snippet:

let options: ICoreWebView2EnvironmentOptions = CoreWebView2EnvironmentOptions::default().into();
let mut language = String::from("pt-BR\0").encode_utf16().collect::<Vec<u16>>();
let lang = PWSTR(text.as_mut_ptr());
options.SetLanguage(lang);

@lucasfernog
Copy link
Member

And an ugly code to read it:

let mut buffer = Vec::<u16>::with_capacity(6);
let mut lang = PWSTR(buffer.as_mut_ptr());
options.Language(&mut lang);
let buffer = unsafe { std::slice::from_raw_parts(lang.0, 5) };
let lang = String::from_utf16_lossy(buffer);

oh boy it's hard to learn the windows api :|

@keiya01
Copy link
Member

keiya01 commented Apr 26, 2022

I found this issue.
And I tried to set system language to webview but this code is not working.

@lzdyes
Copy link

lzdyes commented Apr 30, 2022

invalid in Windows

@keiya01
Copy link
Member

keiya01 commented Jul 20, 2022

Hey there. If you have a time, could you run this branch and tell me whether this works or not on windows.
I think this should work but this is not working correctly in my ARM Windows11(language is not set in webview2).
And if you know the reason why this is not working, please tell me about it 🙏
Thank you.

@amrbashir
Copy link
Member

@keiya01 it works perfectly on my x86 Windows machine. Don't worry about ARM now. we can fix it later if someone else reports it.

@keiya01
Copy link
Member

keiya01 commented Jul 25, 2022

Wooo! Thank you @amrbashir !!
I will open PR!

@amrbashir
Copy link
Member

on a second test, navigator.langagues still returns ["en-US", "en"] although every setting in my Windows is changed to Arabic. There is nothing we can do about this apparently since the issue exists in Edge chromium and probably all chromium-based browsers.

I'll still merge the PR since it does have the desired effect on some other i18n components like new Date().toLocaleString()

@John0x
Copy link

John0x commented May 13, 2023

Are there any news about this issue? @amrbashir
I've run navigator.languages in the edge devtools and in my tauri app and they return entirely different results.
OS: Mac

@amrbashir
Copy link
Member

@John0x please file a new issue with the results an full tauri info output

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
help wanted Help is requested to fix this issue platform: All type: feature request
Projects
None yet
Development

Successfully merging a pull request may close this issue.

6 participants