I have a problem with retrieving the right translation file (.resx) based on a CultureInfo object. So I have a translation solution which has four .resx files (the Italian translations of the application, the Dutch, the German, and the English):

enter image description here

In my application I reference the Qit.WindowsStrings dll and then I can use the translations in my application like this:

lblAddress1.Text = Qit.WindowsStrings.WinStrings.Address1;

The problem is though that I always retrieve the default .resx file so the translation are always English.

If I use the following code:

 string language = "Nederlands";
 switch (language)
 {
     case "English GB":
         System.Threading.Thread.CurrentThread.CurrentUICulture = new System.Globalization.CultureInfo("en-GB");
         break;
     case "English US":
         System.Threading.Thread.CurrentThread.CurrentUICulture = new System.Globalization.CultureInfo("en-US");
         break;
     case "Deutsch":
         System.Threading.Thread.CurrentThread.CurrentUICulture = new System.Globalization.CultureInfo("de-DE");
         break;
     case "Italiano":
         System.Threading.Thread.CurrentThread.CurrentUICulture = new System.Globalization.CultureInfo("it-IT");
         break;
     case "Nederlands":
         System.Threading.Thread.CurrentThread.CurrentUICulture = new System.Globalization.CultureInfo("nl-NL");
         break;
 }

 string test = Qit.WindowsStrings.WinStrings.AppointmentConflictNotAllowed;

 string test2 = Qit.WindowsStrings.Utils.GetString("AppointmentConflictNotAllowed", System.Threading.Thread.CurrentThread.CurrentUICulture);

..the CurrentUICulture is Dutch but I still get the English .resx file and the English translations.

Apparently the ResourceManager class always retrieves the English .resx file:

public static string GetString(string code, CultureInfo cultureInfo)
 {
     string result = string.Empty;
     try
     {
         ResourceManager resourceManager = new ResourceManager(typeof(WinStrings));
         result = ((cultureInfo == null) ? resourceManager.GetString(code) : resourceManager.GetString(code, cultureInfo));
     }
     catch
     {
     }

     return result;
 }

So in my app solution in the build debug folder the Italian, Dutch, and German translation dll files are in a separate folder:

enter image description here

.. while the English Qit.WindowsStrings.resources.dll is in the main folder. Maybe this has something to do with this problem? The weird thing is that it always used to work fine. It must be something small that is causing this. Does anybody have an idea what goes wrong here?

1

There are 1 best solutions below

0
r-m85 On

It turned out that the problem is that the Qit.WindowsStrings.resources.dll is built by a runtime newer than the currently loaded runtime and could not be loaded. I get this error message when loading this assembly:

string path = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
string path2 = Path.Combine(path , "nl");

string[] dll = Directory.GetFiles(path2, "*.resources.dll");

Assembly loadedAssembly = Assembly.LoadFile(dll[0]);

This assembly is built by a runtime newer than the currently loaded runtime and cannot be loaded.

This seems to be the cause that the default (English) translation file is always retrieved.

The weird thing though is that the Qit.WindowsStrings project is targeting .NET framework 3.5 and the application is also targeting .NET framework 3.5.

Anybody has an idea why this problem occurs in this case? If they are both targeting the same .NET framework this problem can't occur, right?

But If I set the .NET target framework of the application (that references the Qit.WindowsStrings.resources.dll) to .NET 4.0 the right translation is retrieved. Then it seems to work fine.

But I would like to keep the .NET framework target version of the application at .NET framework 3.5. I tried to lower the .NET target framework of the Qit.WindowsStrings project but this didn't solve this error.

Anybody has an idea on how to best solve this?