2010-05-31 51 views
6

我在c#windows应用程序中开发了一个示例软件。如何使其成为多语言支持软件。C#中的多语言支持

对于实施例:其中一个消息框的显示“欢迎使用示例应用程序”

i的一个中国佬OS安装的软件,但它仅在英文显示消息。

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

在字符串表,我需要创造英语和中国佬每个信息项。

其及时处理。有没有其他方法可以做到这一点?

+2

好螺纹:http://stackoverflow.com/questions/119568/best-practice-to-make-a-multi-language-application-in-c-winforms – Inisheer 2010-05-31 04:42:57

+0

另外这里:HTTP:// msdn.microsoft.com/en-us/library/1021kkz0%28v=VS。100%29.aspx – Inisheer 2010-05-31 04:45:00

+0

WinForms或WPF? – 2010-05-31 04:56:28

回答

4

Creare为下面提到的每种语言提供支持的语言资源文件。

alt text http://geekswithblogs.net/images/geekswithblogs_net/dotNETPlayground/resx.gif

基于用户的语言/的CurrentCulture,在标签或MessageBog读取来自各语言资源文件,并显示值。以下是一些示例代码。

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> 
+1

谢谢:现在我只用这个样子....别无他法? – 2010-05-31 05:04:06

0

你可以使用资源文件来做到这一点。您需要为每种语言创建资源文件,并在运行应用程序时使用适当的语言。

+0

谢谢拉姆,现在我只使用这种方法....任何其他简单的方法? – 2010-05-31 05:00:15

2

如果你要使用的资源文件,拉姆认为,有一个很好的博客文章有关本地化 here: ASP.NET MVC 2 Localization complete guide。 (我应该提到这是针对Asp.net mvc 2的,它可能有用也可能没有用)您仍然需要花时间为每种语言制作表格。我没有用任何其他方法为在此之前,希望你能找到一些有用的东西

+0

谢谢,这一切都解释得如此简单,它的工作原理。 – 2012-06-18 16:49:55

0

Resharper 5.0可以大大提高您的本地化花费的时间。它具有可轻松移动到资源的功能,并强调(如果选择了这样的话)所有可本地化的字符串,因此很难错过它们。

鉴于它有30天的试用版(完整版),你可以简单地安装它,做你的工作,并卸载,如果你负担不起,但我会建议保留它:-)这真的是值得的价格。

软件本地化和全球化一直是艰难的,有时是开发人员不需要的任务。通过为C#和VB.NET代码以及ASP.NET和XAML标记中的resx文件和资源使用提供全套功能,ReSharper 5极大地简化了资源处理工作。
专用功能包括将字符串移至资源,查找资源和其他导航操作的用法。结合重构支持,检查和修复,您可以获得方便的本地化环境。

这里