MSTest project can't get localized string?

懵懂的女人 提交于 2019-12-12 10:54:30

问题


I ran into a strange problem. In my unit test, I want to check the localized strings. However, I can't seem to get it work. For example, I created two resources: Resource1.resx for English and Resource1.zh-CN.resx for Chinese. The unit test project can only get the (default?) English resource string. This is the code I'm using:

ResourceManager actual = new ResourceManager(typeof(LocaleTest.Properties.Resource1));
string name0 = actual.GetString("Name", new CultureInfo("en-US"));
string name1 = actual.GetString("Name", new CultureInfo("zh-CN"));

I created another regular project (means not a MSTest project) to make sure the localized strings are working. So, it works in a regular project, but not in a MSTest project.

It didn't help even if I put the following code to make 'zh-CN' as the current culture of the unit test:

[TestInitialize()]
public void MyTestInitialize()
{
    Thread.CurrentThread.CurrentCulture = new CultureInfo("zh-CN");
    Thread.CurrentThread.CurrentUICulture = new CultureInfo("zh-CN");
} 

Anybody has seen similar problems? Is there any workaround?


回答1:


Don't you need to use DeploymentItem to ensure that the localisation DLL is in the test folder?

[TestMethod()]
[DeploymentItem(@"bin\Debug\fr\Proj.resources.dll", "fr-CA")]
public void TestDialogLocalization(){
 // blah
}


来源:https://stackoverflow.com/questions/2686815/mstest-project-cant-get-localized-string

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!