c#中的多语言支持程序编码方法

时间:2014-09-30 07:03:43

标签: c# .net config multilingual resx

我想在C#中创建一个支持多语言的程序。但我需要程序应该使用文本文件(或.resx文件,配置文件)来获取语言的内容。我需要当我从文本文件中更改一个单词的含义时,我必须在没有编译.exe文件的情况下看到程序中的更改。 我怎样才能做到这一点? 谢谢。

1 个答案:

答案 0 :(得分:0)

您想要的是[Internationalization (I18N)][1]

您应该查看此documentation

此处还有一个来自this帖子的例子:

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