5

I am currently developing an ASP.NET 4.8 MVC single-page application that is expected to support multiple languages. From various resources, I found that one way to accomplish this is through the use of App_GlobalResources and resource files for each supported language.

I added some navigation links to my page that update the thread's current culture info, which pulls those language resources into my application.

var cultureInfo = CultureInfo.GetCultures(CultureTypes.AllCultures)
                             .FirstOrDefault(x => x.ThreeLetterISOLanguageName == language);

if (cultureInfo == null)
     cultureInfo = CultureInfo.GetCultureInfo("en-US");

Thread.CurrentThread.CurrentCulture = cultureInfo;
Thread.CurrentThread.CurrentUICulture = cultureInfo;

This appears to work fine for languages that have culture support. (IE: "eng" pulls back en-us and finds the Content.en.resx file, "spa" retrieves Content.es.resx, etc.) But I'm running into difficulty where the languages appear in the ISO639-2 list (Hmong, Karen) but don't have CultureInfo associated with them.

I've tried spinning off Cultures for these using CultureAndRegionInfoBuilder, but from my understanding, this will only work on my computer, and not on the web. I'm also not able to 'hack' an existing Culture -- ie, taking Vietnamese and replacing its ThreeLetterISOLanguageName and other properties -- because those properties are readonly.

Is there an explicit way to tell my code to reference a specific content file when CultureInfo is not available? Or am I going about this the entirely wrong way?

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
MadHenchbot
  • 1,306
  • 2
  • 13
  • 27
  • I think I don't understand your exact problem. Your `language` corresponds to a `ThreeLetterISOLanguageName` and you retrieve the `CultureInfo` for it. What's your problem with e.g. `Karen`? (There is a CultureInfo available) – kapsiR Apr 15 '22 at 12:14
  • @kapsiR There is? Might it be dependent on .NET Core or some other external reference? I'm using Visual Studio 2017 and .NET 4.8. When I did CultureInfo searches for "kar" and "hmn", I found 0 references. – MadHenchbot Apr 16 '22 at 23:21
  • 2
    https://stackoverflow.com/questions/70355589/mvc-cultureinfo-language-code-needed-for-hmong – smoore4 Apr 16 '22 at 23:33
  • 1
    Thanks, @smoore4. Saw that one, but was hoping there was a less "hacky" approach than forcing a different culture match onto the language. I'll probably go that direction if there are no other answers forthcoming. – MadHenchbot Apr 17 '22 at 14:20
  • 1
    @MadHenchbot Today I checked it again and saw that I was wrong. In .NET Framework 4.X the CultureInfos come from the OS. [Here is a list of supported languages with the associated OS](https://learn.microsoft.com/en-us/openspecs/windows_protocols/ms-lcid/a9eac961-e77d-41a6-90a5-ce1a8b0cdb9c). I think you have to use the closest available... – kapsiR Apr 19 '22 at 07:06

0 Answers0