c# - C# 中的多语言支持

标签 c# winforms localization multilingual

我在 c# windows 应用程序中开发了一个示例软件。如何让它成为一个多语言的配套软件。

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

我在中文操作系统上安装了该软件,但它只显示英文消息。

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

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

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

最佳答案

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

alt text 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;

        }

    }

}

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

<span class="label">User:</span>

你现在将:

<span class="label"><%=Translate.Message("User") %>:</span>

关于c# - C# 中的多语言支持,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2941391/

相关文章:

c# - 在组合框中使用资源的 XAML 全局化

c# - C# 中 ToString() 的需要是什么?

c# - 在 .NET 中存储数据的方法

c# - 基于nlog输出的功能测试

c# - 如何在针对模式验证的 XDocument 中查找无效的 XML 节点(XmlSchemaValidationException.SourceObject 为空)

C#:在 RichTextBox 中粘贴 RTF,但保留颜色和格式(即:粗体、下划线等)

.net - 处理 Me.FormClosing 的两种方法;为什么他们按这个特定顺序开火?

c# - 如何从 ComboBox C# Winforms 获取 ValueMember 值?

javascript - 翻译插件,使用 JS - 源自 ENTITY - *.dtd 文件

objective-c - UIWebview加载不同文件切换语言