c# – 如何在單個EXE中嵌入多語言* .resx(或* .resources)檔案?
有很多教程如何建立多語言RESX檔案以及如何使用AL.exe建立衛星程式集,但我還沒有找到工作示例如何在單個EXE檔案中嵌入RESX / Resources / satellite-DLL檔案並分發整個多語言應用程式像這樣的EXE.
我試圖使用ilmerge.exe,但它看起來像對於具有相同名稱的多個DLL(文化衛星DLL具有相同的名稱,最初駐留在以文化名稱命名的不同子目錄中)不起作用.
我也不知道如何建立ResourceManager例項來處理嵌入式資源.
我的目標是在封閉的,預定義的語言集之間進行動態切換.我需要獲得文化字串(即“de-DE”),資源名稱(即“CancelText”)的類/方法,並且基於嵌入的resx / resource / dll返回翻譯的文字.
我使用VS2008,請注意在resx /資原始檔屬性表中需要“build action”的設定.工作程式碼示例或連結到教程專案將是最好的.
我的解決方案:程式只包含一個預設語言資原始檔(resx).所有其他語言都從.resx編譯為.resources並作為資原始檔嵌入.重要!我更改了副檔名,因為“.resources”被識別為一種特殊型別的資源,所以我的法語檔案被命名為“PIAE.LangResources.fr”.
以下是檢索已翻譯字串的簡單程式碼(應使用資源快取值來改進):
internal static string GetString(string str, string lang) { if (string.IsNullOrEmpty(str)) throw new ArgumentNullException("empty language query string"); if (string.IsNullOrEmpty(lang)) throw new ArgumentNullException("no language resource given"); // culture-specific file, i.e. "LangResources.fr" Stream stream = Assembly.GetExecutingAssembly().GetManifestResourceStream("PIAE.LangResources."+lang); // resource not found, revert to default resource if (null == stream) { stream = Assembly.GetExecutingAssembly().GetManifestResourceStream("PIAE.Properties.LangResources.resources"); } ResourceReader reader = new ResourceReader(stream); IDictionaryEnumerator en= reader.GetEnumerator(); while (en.MoveNext()) { if (en.Key.Equals(str)) { return en.Value.ToString(); } } // string not translated, revert to default resource return LangResources.ResourceManager.GetString(str); }
http://stackoverflow.com/questions/1793256/how-to-embed-multilanguage-resx-or-resources-files-in-single-exe