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)文件?
基础教程推荐
- 为什么Flurl.Http DownloadFileAsync/Http客户端GetAsync需要 2022-09-30
- 如何在 IDE 中获取 Xamarin Studio C# 输出? 2022-01-01
- rabbitmq 的 REST API 2022-01-01
- MS Visual Studio .NET 的替代品 2022-01-01
- SSE 浮点算术是否可重现? 2022-01-01
- 将 XML 转换为通用列表 2022-01-01
- 如何激活MC67中的红灯 2022-01-01
- 将 Office 安装到 Windows 容器 (servercore:ltsc2019) 失败,错误代码为 17002 2022-01-01
- 有没有办法忽略 2GB 文件上传的 maxRequestLength 限制? 2022-01-01
- c# Math.Sqrt 实现 2022-01-01