C#中的多语言支持

Tho*_*eld 6 c# multilingual localization winforms

我在c#windows Appliation中开发了一个示例软件.如何使其成为多语言支持软件.

例如:其中一个消息框显示"欢迎使用示例应用程序"

我在chinees操作系统中安装了该软件,但它只显示英文信息.

我正在使用"字符串表"(资源文件)来解决这个问题.

在字符串表中,我需要为英语和中文中的每条消息创建条目.

这是一个及时的过程.还有其他办法吗?

thi*_*eek 4

为您想要支持的每种语言创建资源文件,如下所述。

替代文本 http://geekswithblogs.net/images/geekswithblogs_net/dotNETPlayground/resx.gif

根据用户的语言/当前文化,从相应的语言资源文件中读取值并显示在标签或消息框中。这是一些示例代码:

public static class Translate

{

    public static string GetLanguage()

    {

        return HttpContext.Current.Request.UserLanguages[0];

    }



    public static string Message(string key)

    {

        ResourceManager resMan = null;

        if (HttpContext.Current.Cache["resMan" + Global.GetLanguage()] == null)

        {

            resMan = Language.GetResourceManager(Global.GetLanguage());

            if (resMan != null) HttpContext.Current.Cache["resMan" + Global.GetLanguage()] = resMan;

        }

        else

            resMan = (ResourceManager)HttpContext.Current.Cache["resMan" + Global.GetLanguage()];



        if (resMan == null) return key;



        string originalKey = key;

        key = Regex.Replace(key, "[ ./]", "_");



        try

        {

            string value = resMan.GetString(key);

            if (value != null) return value;

            return originalKey;

        }

        catch (MissingManifestResourceException)

        {

            try

            {

                return HttpContext.GetGlobalResourceObject("en_au", key).ToString();

            }

            catch (MissingManifestResourceException mmre)

            {

                throw new System.IO.FileNotFoundException("Could not locate the en_au.resx resource file. This is the default language pack, and needs to exist within the Resources project.", mmre);

            }

            catch (NullReferenceException)

            {

                return originalKey;

            }

        }

        catch (NullReferenceException)

        {

            return originalKey;

        }

    }

}
Run Code Online (Sandbox Code Playgroud)

在 asn asp.net 应用程序中,您可以按如下方式使用它:

<span class="label">User:</span>
Run Code Online (Sandbox Code Playgroud)

您现在可以输入:

<span class="label"><%=Translate.Message("User") %>:</span>
Run Code Online (Sandbox Code Playgroud)