内容目录
By default, WebView2 displays in English, including in places like the console and print interface. However, it can be modified to use the local language.
public static void SetWebviewLanguage(string language = "zh-CN")
{
var cultureInfo = new CultureInfo(language);
// Set the language used by the program; can be ignored.
CultureInfo.CurrentCulture = cultureInfo;
CultureInfo.DefaultThreadCurrentUICulture = cultureInfo;
// Modify the language settings of WebView2
// Not sure where the directory is, it can be modified; refer to https://www.whuanle.cn/archives/21314
var basePath = "weview2 data storage directory";
if (!Directory.Exists(Path.Combine(basePath, "EBWebView")))
{
var webView2 = Directory.GetDirectories(basePath).FirstOrDefault(x => x.EndsWith(".WebView2"));
if (webView2 == null) return;
basePath = webView2;
}
var preferencesFile = Path.Combine(basePath, "EBWebView/Default/Preferences"); // Preferences
if (!File.Exists(preferencesFile)) return;
string jsonString = File.ReadAllText(preferencesFile);
JsonObject jsonObject = JsonNode.Parse(jsonString)!.AsObject();
// var languages = jsonObject["intl"]!["selected_languages"].Deserialize<string>() ?? ""; // Get current language
// "zh-CN,en,en-GB,en-US"
// File.Delete(preferencesFile);
jsonObject.Remove("intl");
jsonObject.Add("intl", JsonNode.Parse($"{{\"selected_languages\":\"{cultureInfo.Name}\"}}"));
jsonString = JsonSerializer.Serialize(jsonObject);
File.WriteAllText(preferencesFile, jsonString);
}
文章评论