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:
- strings.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
(I have checked that the correct translations are in those files.)
In order to define which resource files should be used when performing language selection, my
Configuremethod 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:
- 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();
How do I get .net to recognise that the browser is requesting the zh-TW culture?
This is another ICU issue, as
zh-TWis not an ISO standard name,AllCultureswill not contain it, andzh-TWwill not matchzh-Hant-TW.I can find 2 solutions:
Manually add
zh-TWUse NLS instead of ICU, https://stackoverflow.com/a/77192077/6196568