2010-05-31 99 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極大地簡化了資源處理工作。
專用功能包括將字符串移至資源,查找資源和其他導航操作的用法。結合重構支持,檢查和修復,您可以獲得方便的本地化環境。

這裏