TL;DR: .NET 6 is refusing to render MVC pages when the browser's language is set to zh-TW
.
When my ASP.NET Core 6 MVC application renders a page, if the user has the browser language set to Chinese (Traditional)
(zh-TW
), it displays Chinese (Simplified)
(zh-CN
) and the CurrentCulture name is shown as zh
.
I have the following language resource files:
Configure
method contains this code:
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
});
When debugging, the supportedCultures
list includes the CultureInfo
objects with these names:
***added here***
line above:
supportedCultures.Reverse();
How do I get .net to recognise that the browser is requesting the zh-TW
culture?
This is another ICU issue, as zh-TW
is not an ISO standard name, AllCultures
will not contain it, and zh-TW
will not match zh-Hant-TW
.
I can find 2 solutions:
Manually add zh-TW
supportedCultures.Add(new CultureInfo("zh-TW"));
Use NLS instead of ICU, https://stackoverflow.com/a/77192077/6196568