我有一个小型测试应用程序,它有 2 个资源文件(Resources.resx
& Resources.de-DE.resx
)具有完全相同的字符串名称,但将字符串转换为德语。
对于我的表单,我将 Localize 属性设置为 true。
在我的应用程序中,我得到的字符串如下:
this.Text = Properties.Resources.frmCaption;
在我的发布文件夹中我得到一个de-DE
包含名为 dll 的文件夹International_test.resources.dll
.
我尝试将其分发到设置为德语的机器上,并且所有拉动的字符串仍然是英语。
我尝试保留International_test.resources.dll
in the de-DE
文件夹或只是将其放入我的apps
目录。
我做错了什么或者我需要做什么才能使用德语资源文件?
幸运的是,我使用 hello world 原型项目来测试构建管道中的一大堆东西。
假设您已正确设置资源文件,这里有一些示例代码可能会有所帮助。为简洁起见,删除了代码文档。
public class HelloWorld
{
public CultureInfo CultureInfo { get; private set; }
public HelloWorld()
{
CultureInfo = CultureInfo.CurrentCulture;
}
public HelloWorld(string culture)
{
CultureInfo = CultureInfo.GetCultureInfo(culture);
}
public string SayHelloWorld()
{
return Resources.ResourceManager.GetString("HelloWorld", CultureInfo);
}
}
[TestFixture]
public class HelloWorldFixture
{
HelloWorld helloWorld;
[Test]
public void Ctor_SetsCultureInfo_ToCurrentCultureForParameterlessCtor()
{
helloWorld = new HelloWorld();
Assert.AreEqual(helloWorld.CultureInfo, CultureInfo.CurrentCulture,
"Expected CultureInfo to be set as CurrentCulture");
}
[Test]
public void Ctor_SetsCultureInfo_ToAustralianCulture()
{
helloWorld = new HelloWorld("en-AU");
Assert.AreEqual(helloWorld.CultureInfo.Name, "en-AU",
"Expected CultureInfo to be set to Australian culture");
}
[Test]
[ExpectedException(typeof(ArgumentException))]
public void Ctor_ThrowsException_InvalidCultureName()
{
helloWorld = new HelloWorld("Bogus");
}
[Test]
public void SayHelloWorld_ReturnsFallbackResource_OnUndefinedResource()
{
helloWorld = new HelloWorld("en-JM");
string result = helloWorld.SayHelloWorld();
Assert.AreEqual("Hello, World.", result, "Expected fallback resource string to be used");
}
[Test]
public void SayHelloWorld_ReturnsAustralianResource_OnAustralianResource()
{
helloWorld = new HelloWorld("en-AU");
string result = helloWorld.SayHelloWorld();
Assert.AreEqual("G'Day, World.", result, "Expected australian resource string to be used");
}
}
该项目有资源.resx文件与你好世界字符串键项和“你好世界”值,以及相应的资源.en-AU.resx with 你好世界字符串键项和“你好,世界”值,加上其他诸如 zh-CH (我只气垫船装满晒鳝.:) 来测试非英文字符的显示,因为它显示在关联的 hello world web 项目中。
最后,添加一些日志记录以显示正在使用的区域性(为了简洁起见,我将其从本示例中取出),并检查编译器输出以确保调用 AL.exe 来链接资源程序集(听起来不错)。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)