4

I have a project that needs to support French and English. I put all strings into resource files. When I debug the program in Visual Studio, I can successfully view the program in French. However, when I run the tests for the program, the program reverts to English. I added the French resource dll file to the deployment. This allows me to debug individual unit tests in French, but when I click Test -> Debug -> All Tests in Solution, the program runs in English.

I tried to add [DeploymentItem(@"..\bin\x86\Release\fr", "fr-FR")] to one of the tests also, but this didn't help. Does anyone have any suggestions on how to fix this? Thanks!

Robert Harvey
  • 178,213
  • 47
  • 333
  • 501
user1438430
  • 41
  • 1
  • 2

2 Answers2

6

I wrap language tests in a using statement like this:

[Test]
public void Loads_correct_labels_from_language()
{
    using(new LanguageSwitchContext("fr"))
    {
       var okayString = MyResources.Okay_Button;
       Assert.Equals("your localized string here", okayString);
    }
}

public class LanguageSwitchContext : IDisposable
{
    public CultureInfo PreviousLanguage { get; private set; }

    public LanguageSwitchContext(CultureInfo culture) 
    {
        PreviousLanguage = System.Threading.Thread.CurrentThread.CurrentCulture;
        System.Threading.Thread.CurrentThread.CurrentCulture = culture;
    }

    public LanguageSwitchContext(string language) 
    {
        //create culture from language
    }

    public void Dispose() 
    {
        System.Threading.CurrentThread.CurrentCulture = PreviousCulture;
    }
}
Hadi Eskandari
  • 25,575
  • 8
  • 51
  • 65
  • 1
    I'm not exactly trying to make language tests. The project has tests that run through the code to make sure it works properly. I was hoping there's an easy way to test the code in multiple languages, just to make sure localization didn't break the program. – user1438430 Jul 10 '12 at 18:23
  • What kind of tests do you have? If you are using NUnit you can use SetCultureAttribute to run your tests using a specific culture, this will ensure the code under test is executed using that culture. See here: http://www.nunit.org/index.php?p=setCulture&r=2.4.8 – Hadi Eskandari Jul 10 '12 at 23:00
  • @Hadi, you are using `System.Threading.CurrentThread.CurrentCulture` but this does not affect localization (it affects culture-dependent formatting and parsing). You should also set `System.Threading.CurrentThread.CurrentUICulture` – Clafou Jul 11 '12 at 20:17
6

There are two potential issues here:

  1. When your tests are run, CurrentUICulture may not be set to the language you wish to test
  2. The satellite assemblies (only French in your example) may not be deployed in the unit test environment. When ResourceManager doesn't find a matching satellite assembly for a UI culture, it defaults to the base language embedded in the project's DLL itself (and so you get English, assuming that's your base language).

For 1. you can make sure to set System.Threading.CurrentThread.CurrentUICulture = new CultureInfo("fr-FR"); in your test or at your test class's initialization.

For 2. the use of a DeploymentItemAttribute like you mentioned should work. Do verify that the satellite assembly deployed when your successfully run your app in French does get copied with the same name and in the same subfolder in your test's deployment. It's probably something more like [DeploymentItem(@"bin\fr-FR", "fr-FR")]

Clafou
  • 15,250
  • 7
  • 58
  • 89