当浏览器的语言设置为ZH-TW
时,TL;DR:.NET 6拒绝呈现MVC页面.
他说:
当我的ASP.NET Core 6 MVC应用程序呈现一个页面时,如果用户将浏览器语言设置为Chinese (Traditional)
(ZH-TW
),它将显示Chinese (Simplified)
(ZH-CN
),并且CurrentCulture名称显示为ZH
.
他说:
我有以下语言资源文件:
他说:
- 字符串.ZH-CHS.resx
- Strings.ZH-CHT.resx
- Strings.ZH-cn.resx
- Strings.ZH-Hans.resx
- Strings.ZH-Hant.resx
- Strings.ZH-TW.resx
- Strings.ZH.resx
他说:
(我已判断这些文件中是否有正确的翻译.)
他说:
为了定义在执行语言 Select 时应该使用哪些资源文件,我的
Configure
方法包含以下代码: 他说:
CultureInfo[] allCultures = CultureInfo.GetCultures(CultureTypes.AllCultures);
string location = Path.GetDirectoryName(System.Reflection.Assembly.GetEntryAssembly().Location);
var supportedCultures = allCultures.Where(c => (Directory.Exists(Path.Combine(location, c.Name)) || c.Name.StartsWith("ZH-")) && c.LCID != 127).ToList();
// ***added here***
app.UseRequestLocalization(new RequestLocalizationOptions
{
DefaultRequestCulture = new RequestCulture("en-US"),
SupportedCultures = supportedCultures,
SupportedUICultures = supportedCultures
});
他说:
调试时,supportedCultures
列表包括具有以下名称的CultureInfo
个对象:
他说:
- ZH
- ZH-Hans
- ZH-Hant
- ZH-Hant-TW
In an effort to avoid the selection defaulting to the first matching language in the list, I have added this code to the
***added here***
line above:
supportedCultures.Reverse();
他说:
如何让.Net识别浏览器正在请求ZH-TW
区域性?