国际化HelloWorld程序.NET程序、HelloWorld、NET

2023-09-04 06:47:02 作者:玩什么别玩感情。丶

我小的测试应用程序,它有2个资源文件( Resources.resx &安培; Resources.de-DE.resx )具有完全相同的字符串名称,而是一个有转化为德国的字符串。

I have small test app which has 2 resource files (Resources.resx & Resources.de-DE.resx) with the same exact string names, but one has the strings converted to German.

有关我的形式我设置的本地化属性为TURE。

For my form I set the Localize property to ture.

在我的应用程序,我得到的字符串作为这样的:

In my application I am getting the strings as such:

this.Text = Properties.Resources.frmCaption;

在我的版本的文件夹,我收到了去DE 文件夹名为DLL International_t​​est.resources.dll

In my release folder I get a de-DE folder with a dll named International_test.resources.dll.

我尝试分发此一台机器被设置为德国和所有拉弦仍然是英语。

I try to distribute this to a machine which is set to German and all of the strings pulled are still english.

我试图保持 International_t​​est.resources.dll 去DE 文件夹或只是把我的应用目录。

I tried keeping the International_test.resources.dll in the de-DE folder or just put in in my apps directory.

我是什么做错了还是什么,我需要做的就是德国的资源文件中使用?

What am I doing wrong or what do I need to do to get the German resource file to be used?

推荐答案

由于幸运的是,我使用的Hello World工程样机在我们的建设的管道,以测试一大堆东西。

As luck would have it, I use hello world prototype project to test a whole bunch of stuff in our build pipeline.

假设你已经设置你的资源正确的文件,这里的一些例子code,可以帮助。 code文档为了简洁,删除。

Assuming you have setup your resource files correctly, here's some example code that may help. Code documentation removed for brevity.

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");
    }
}

该项目具有的 Resources.resx 的文件的的HelloWorld 的字符串键值项和你好,世界的值,以及相应的 Resources.en-AU.resx 的用的的HelloWorld 的字符串键值项和天儿真好,世界的价值,再加上其他如ZH-CH(我只气垫船装满晒鳝。:)测试的非英文字符显示,因为它被显示在相关的hello world web项目。

This project has Resources.resx file with HelloWorld string key item and "Hello, World" value, along with corresponding Resources.en-AU.resx with HelloWorld string key item and "G'Day, World" value, plus others such as zh-CH (我隻氣墊船裝滿晒鱔.:) to test display of non-English characters, as it gets displayed in the associated hello world web project.

最后,添加一些记录,显示文化正在使用(我把它这个例子简洁的),并检查您的编译器的输出,以确保Al.exe工具被调用链接你的资源集(听起来好像它是确定虽然)。

Finally, Add some logging to show the culture being used (I took it out of this example for brevity), and also check your compiler output to ensure AL.exe is being invoked to link your resource assemblies (sounds like it's OK though).