如何在单个 EXE 中嵌入多语言 *.resx(或 *.resources)文件?

How to embed multilanguage *.resx (or *.resources) files in single EXE?(如何在单个 EXE 中嵌入多语言 *.resx(或 *.resources)文件?)
本文介绍了如何在单个 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)文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

DispatcherQueue null when trying to update Ui property in ViewModel(尝试更新ViewModel中的Ui属性时DispatcherQueue为空)
Drawing over all windows on multiple monitors(在多个监视器上绘制所有窗口)
Programmatically show the desktop(以编程方式显示桌面)
c# Generic Setlt;Tgt; implementation to access objects by type(按类型访问对象的C#泛型集实现)
InvalidOperationException When using Context Injection in ASP.Net Core(在ASP.NET核心中使用上下文注入时发生InvalidOperationException)
LINQ many-to-many relationship, how to write a correct WHERE clause?(LINQ多对多关系,如何写一个正确的WHERE子句?)