MAUI Blazor uses WebView2 on Windows, and the MAUI Blazor runtime environment is independent of the application. Even if the system language is set to Chinese, the assembly is set to Chinese, and the local culture is set to Chinese, the CultureInfo
settings are ineffective.
You can press F12 after the application starts, and then execute JavaScript code to check the language of the browser environment:
navigator.language
' en-US '
Alternatively, you can use the API:
// using Windows.Globalization
var langs = ApplicationLanguages.Languages.ToList<string>();
Pitfall ①
First, set Windows.Globalization:
ApplicationLanguages.PrimaryLanguageOverride = "zh-CN";
Then restart the program, and you will find:
However, the browser language environment remains unchanged:
The reason is that the
Preferences
file needs to be regenerated to take effect, which will be discussed later.
Pitfall ②
After the program starts, some WebView2 files will be generated in {Windows application data directory}/{your program ID}/LocalState\EBWebView\Default
, including the Preferences file, which configures the parameters for WebView2.
Find your application data directory:
var path = Windows.Storage.ApplicationData.Current.LocalFolder.Path;
Therefore, you can manually modify the file to allow WebView2 to use the Chinese environment.
var langs = ApplicationLanguages.Languages.ToList<string>();
var cultureInfo = CultureInfo.InstalledUICulture;
var index = langs.FindIndex((lang) => cultureInfo.Equals(CultureInfo.CreateSpecificCulture(lang)));
if (index > 0)
{
ApplicationLanguages.PrimaryLanguageOverride = cultureInfo.Name;
// " ...this is immediately reflected in the ApplicationLanguages.Languages property."
langs = ApplicationLanguages.Languages.ToList<string>();
}
var selectedLangs = string.Join(",", langs);
// Should check if this is the same as before but...
var preferences = Windows.Storage.ApplicationData.Current.LocalFolder.Path + "\\EBWebView\\Default\\Preferences";
if (File.Exists(preferences))
{
var jsonString = File.ReadAllText(preferences);
var jsonObject = JObject.Parse(jsonString); // using Newtonsoft.JSON
//var intl = jsonObject["intl"];
jsonObject["intl"] = JObject.Parse($@"{{""selected_languages"": ""{selectedLangs}"",""accept_languages"": ""{selectedLangs}""}}");
jsonString = JsonConvert.SerializeObject(jsonObject);
File.WriteAllText(preferences, jsonString);
}
Pitfall ③
Finally, I found that the approach in ① was correct, but the reason it didn't work was that the Preferences
file needed to be deleted or recreated. Simply set Chinese when the program starts (before WebView starts).
Check the code:
public static class MauiProgram
{
private static void SetWebViewLanguage()
{
ApplicationLanguages.PrimaryLanguageOverride = "zh-CN";
var basePath = Windows.Storage.ApplicationData.Current.LocalFolder.Path;
var preferencesFile = Path.Combine(basePath, "EBWebView/Default/Preferences"); // Preferences
if (!File.Exists(preferencesFile)) return;
var jsonString = File.ReadAllText(preferencesFile);
var jsonObject = JsonObject.Parse(jsonString).AsObject();
var languages = jsonObject["intl"]["selected_languages"].Deserialize<string>() ?? "";
// "zh-CN,en,en-GB,en-US"
if (!languages.StartsWith("zh-CN"))
{
// File.Delete(preferencesFile);
jsonObject.Remove("intl");
jsonObject.Add("intl", JsonObject.Parse("{\"selected_languages\":\"zh-CN,en,en-GB,en-US\"}"));
jsonString = JsonSerializer.Serialize(jsonObject);
File.WriteAllText(preferencesFile, jsonString);
}
}
}
Use it in public static MauiApp CreateMauiApp()
:
文章评论