Internationalize HelloWorld program .NET

2020-07-27 05:32发布

问题:

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.

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;

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.

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?

回答1:

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

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

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.

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).



回答2:

I think my problem is that I am using a US version of XP with its language set to German as well other culture settings. The CurrentCulture and CurrentUICulture still appear to be still be "en-US"

I guess the best way to test then is to manually set the cultures at the start of the program like:

Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture("de-DE"); Thread.CurrentThread.CurrentUICulture = CultureInfo.CreateSpecificCulture("de-DE");



回答3:

thanks a ton Si.

I finally got my problem figured out.

My Resource files are in the Properties folder so I need to put the following

//using embedded resources
ResourceManager rm = new ResourceManager("International_test.Properties.Resources", Assembly.GetExecutingAssembly());