0

Looking to implement exception message translation as shown in Exception messages in English? I'd like unit tests so this requires being able to produce the messages in a different language. That doesn't quite work out though; code like

Thread.CurrentThread.CurrentUICulture = CultureInfo.GetCultureInfo("de-DE");
Console.WriteLine(DirectoryNotFoundException().Message);

Always produces the English version "Attempted to access a path that is not on the disk.".

Figuring I'm missing something and following instructions I installed the German .NET Framework 4.7.2 language pack for testing this in an application with TargetFrameworkVersion=v4.7.2 Visual Studio 2017, no change.

Wanting to try the same in Visual Studio 2022, with the same application but targetting v4.8, I also tried to install the German .NET Framework 4.8 language pack, but that installer doesn't complete. The tranlated message is (almost ironically the installer UI is completely in German) ".NET Framework 4.8 (Deutsch) or a later version is already installed on this computer."

Am I doing something wrong here? I mean I can write applications targetting different framework versions so I assume the prerequisites for language packs are there? Do I need to install something else, or in a different way?

And is there a way to figure out in code or otherwise why the GetCultureInfo call doesn't return a culture, e.g. where does it go looking for resource files, and is there perhaps a way to correct that?

stijn
  • 34,664
  • 13
  • 111
  • 163
  • IIRC you can only have one language pack of .NET framework installed at once (maybe even just the one that matches the OS language + maybe english). The configured culture does not affect messages returned from exceptions. – PMF Mar 25 '22 at 08:57
  • It appears that this is even still the case in .NET5.0+. That seems to indicate that it is not possible to choose the language of error messages from the application itself. – PMF Mar 25 '22 at 09:01
  • @PMF check the link: there are people reporting this example working for different languages. – stijn Mar 25 '22 at 09:21

1 Answers1

0

Looks like to get this working, the OS needs to have the wanted language (thanks @PMS for hinting on that): installing the German language (Start->Add a language to this device) solved it, even without installing a separate language pack or antything.

And is there a way to figure out in code or otherwise why the GetCultureInfo call doesn't return a culture, e.g. where does it go looking for resource files

This remains unasnwered though, but I don't need this anymore now.

stijn
  • 34,664
  • 13
  • 111
  • 163