How to embed multilanguage *.resx (or *.resources) files in single EXE?(如何在单个 EXE 中嵌入多语言 *.resx(或 *.resources)文件?)
问题描述
有很多教程如何创建多语言 RESX 文件以及如何使用 AL.exe 创建附属程序集,但我还没有找到工作示例如何将 RESX/Resources/satellite-DLL 文件嵌入单个 EXE 文件并分发整个多语言应用程序,例如 EXE.
There are plenty of tutorials how to create multilanguage RESX files and how to create satellite assemblies with AL.exe, but I haven't found working example how to embed RESX/Resources/satellite-DLL files in single EXE file and distribute whole multilanguage app as such EXE.
我尝试使用 ilmerge.exe,但它似乎不适用于具有相同名称的多个 DLL(文化卫星 DLL 具有相同的名称,最初位于以文化命名的不同子目录中).
I tried to use ilmerge.exe, but it looks like it doesn't work for multiple DLLs with the same name (culture satellite DLLs have identical names, originally residing in different subdirs named after culture).
我也不知道如何创建 ResourceManager 实例来处理嵌入式资源.
I also don't know how to create ResourceManager instance to work with embedded resources.
我的目标是在封闭的、预定义的语言集之间实现动态切换.我需要获取文化字符串(即de-DE")、资源名称(即CancelText")并根据 embedded resx/resource/dll 返回翻译文本的类/方法.
My goals is to enable dynamical switching between closed, pre-defined set of languages. I need class/method which will get culture string (i.e. "de-DE"), resource name (i.e. "CancelText") and return translated text based on embedded resx/resource/dll.
我使用的是 VS2008,请注意 resx/资源文件属性表中需要什么构建操作"设置.工作代码示例或教程项目的链接将是最好的.
I'm using VS2008, please note what setting for "build action" is needed in resx/resource files properties sheet. Working code sample or link to tutorial project would be the best.
推荐答案
我的解决方案:程序只包含一个默认语言资源文件(resx).所有其他语言都从 .resx 编译为 .resources 并作为资源文件嵌入.重要的!我更改了扩展名,因为.resources"被识别为一种特殊类型的资源,所以我的法语文件被命名为PIAE.LangResources.fr".
My solution: program contains only one default language resource file (resx). All other languages are compiled from .resx to .resources and embedded as resource file. Important! I have changed extension because ".resources" is recognized as a special type of resource, so my French files is named "PIAE.LangResources.fr".
这里是检索翻译字符串的简单代码(应该通过缓存资源中的值来改进):
Here is simple code to retrieve translated string (it should be improved with caching values from resource):
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);
}
这篇关于如何在单个 EXE 中嵌入多语言 *.resx(或 *.resources)文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何在单个 EXE 中嵌入多语言 *.resx(或 *.resources)文件?
- Web Api 中的 Swagger .netcore 3.1,使用 swagger UI 设置日期时间格式 2022-01-01
- C#MongoDB使用Builders查找派生对象 2022-09-04
- 如何用自己压缩一个 IEnumerable 2022-01-01
- 输入按键事件处理程序 2022-01-01
- C# 中多线程网络服务器的模式 2022-01-01
- WebMatrix WebSecurity PasswordSalt 2022-01-01
- 带有服务/守护程序应用程序的 Microsoft Graph CSharp SDK 和 OneDrive for Business - 配额方面返回 null 2022-01-01
- MoreLinq maxBy vs LINQ max + where 2022-01-01
- 良好实践:如何重用 .csproj 和 .sln 文件来为 CI 创建 2022-01-01
- 在哪里可以找到使用中的C#/XML文档注释的好例子? 2022-01-01