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) #2735

Closed
eike-hass opened this issue Oct 12, 2021 · 5 comments
Closed

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

eike-hass opened this issue Oct 12, 2021 · 5 comments
Labels
status: upstream This issue is blocked by upstream dependencies and we need to wait or contribute upstream fixes type: feature request

Comments

@eike-hass
Copy link

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
Copy link
Member

/upstream tauri-apps/wry

@tauri-apps
Copy link

tauri-apps bot commented Oct 30, 2021

I have created an upstream issue at tauri-apps/wry#442, I will notify you once it is resolved.

@tauri-apps tauri-apps bot added the status: upstream This issue is blocked by upstream dependencies and we need to wait or contribute upstream fixes label Oct 30, 2021
@amrbashir amrbashir changed the title better l18n support in webview better l18n support in webview (navigator. language)a Dec 21, 2021
@amrbashir amrbashir changed the title better l18n support in webview (navigator. language)a better l18n support in webview (navigator. language) Dec 21, 2021
@amrbashir
Copy link
Member

A fix has landed upstream but 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 also exists in Edge chromium and all chromium-based browsers so a fix should land in chromium first.

The upstream fix does have a desired effect on some other i18n components like new Date().toLocaleString()

I am gonna close this issue as we've done everything we can. We probably can provide a separate function to retrieve OS languages in our JS API. Feel free to open another issue for that.

@John0x
Copy link

John0x commented Dec 28, 2022

A fix has landed upstream but 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 also exists in Edge chromium and all chromium-based browsers so a fix should land in chromium first.

The upstream fix does have a desired effect on some other i18n components like new Date().toLocaleString()

I am gonna close this issue as we've done everything we can. We probably can provide a separate function to retrieve OS languages in our JS API. Feel free to open another issue for that.

Is there currently a way to get the preferred language? Can't find anything the JS API.

@Miniontoby
Copy link
Contributor

Why don't we add code to do set the navigator.language?

In Cargo.toml:

(...)

[dependencies]
current_locale = "*"
(...)

In rust:

use current_locale::*;

(...)

#[tauri::command]
fn get_user_language() -> String {
  let current_locale = current_locale::current_locale();
  if (current_locale.is_ok()){
    return current_locale.ok().unwrap();
  }
  return "en-US".to_string();
}

In the code for the frontend functions:

import { invoke } from '@tauri-apps/api/tauri';

(...)

// Add in some async function
navigator.language = await invoke('get_user_language');

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
status: upstream This issue is blocked by upstream dependencies and we need to wait or contribute upstream fixes type: feature request
Projects
None yet
Development

No branches or pull requests

4 participants