locked
Localization stays English when used in class library RRS feed

  • Question

  • User1289595697 posted

    Hello,

    I have a bit of a weird problem. I'm using a class library to store my different resource files, which I then use in a web application. There are 2 resource files; resources.resx and resources.nl.resx

    The language is stored in a database, in a table named 'user'.

    Code example:

    Localization locrm = new Localization(user.Language);
    lblTest.Text = locrm.GetString("test");

    This works perfectly fine, when I change the language in the database table, the website is translated properly.

    However, when I try to do the same within the class library (for example to send automated news messages in the user's own language), the language stays English even though the localization is set to Dutch. When debugging, I can see in the quickwatch of the locrm variable that the culture is correctly set to "nl", but the GetString method returns only English text.

    The code to set the localization variable is exactly the same as on the website.

    Anyone has an idea what could be the cause?

    Thanks!! :)

    Sunday, August 14, 2011 11:57 AM

Answers

  • User1289595697 posted

    I apparently solved it like this:

    System.Threading.Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture(language);
    System.Threading.Thread.CurrentThread.CurrentUICulture = new CultureInfo(language);
    Localization locrm = new Localization(language);
    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, September 15, 2011 6:37 AM

All replies

  • User1289595697 posted

    I think it has to do with the location of the resource files. All the files are under the folder 'Properties'. But I think that, for some reason, the Dutch file can't be found so the default language is used instead. I don't understand, however, how it works for a website (external application) but not within the class library itself.

    I must be missing something... or maybe it has to do with initializing the localization variable? Do I have to set the culture as well, besides the language?

    Any help or tips would be appreciated Smile

    Monday, August 15, 2011 5:56 AM
  • User-276842610 posted

    Hi Boddah,

    The resource files should located in App_LocalResources folder, if your change the current culture, the application will automatically find the according region.resx file .

    Please refer the MSDN article: http://msdn.microsoft.com/en-us/library/c6zyy3s9.aspx

    Hope this helps, thanks.

    Tuesday, August 16, 2011 2:46 AM
  • User1289595697 posted

    Hi Yanping,

    Thanks for replying.

    The problem is not on the web application level. The translations work fine here. I have embedded the resource files in the class library and my web application is finding the correct language.

    My problem is when using the resources files within the class library itself. For example, when I want to send a weekly e-mail notification.

    I created an 'Update' class with a method that loops through all users that exist in the database. For each user, I want to send the e-mail in their own language.

    But this doesn't work for some reason. In the method I set the Localization variable to the users' language, but it keeps showing English texts. I don't think the folder matters, as the English resource file is in the same folder as the other files.

    Sunday, August 21, 2011 8:59 AM
  • User1345853836 posted

    Hi Boddah,

    Did you find a solution for this problem, as I have the same thing.

    I have several unit tests which test this behavior. This unit test project contains a folder named Resources, which contains the resources in English, Dutch and French.

    However, when I specify the Dutch or French culture, I always get the default English value back.

    Kind regards

    Monday, September 12, 2011 5:45 AM
  • User1345853836 posted

    Boddah,

    I solved my problem. Seems that my unit tests did not copy the sattelite assemblies containing the resources for the different languages.

    So I used the DeploymentItem attribute and specified the relative location to my sattelite assembly dll's.

    Note that the relative location has to be relative to the solution folder. (So the folder containing your solution's .sln file. By default your project's source code and project file are in the same location, but they could be different)

    Maybe this sheds some light on your problem?

    Kind regards,

    Monday, September 12, 2011 7:52 AM
  • User1289595697 posted

    I apparently solved it like this:

    System.Threading.Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture(language);
    System.Threading.Thread.CurrentThread.CurrentUICulture = new CultureInfo(language);
    Localization locrm = new Localization(language);
    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, September 15, 2011 6:37 AM