在不使用资源 DLL 的情况下向 MFC 项目添加第二种语言 [英] Add a second language to a MFC project without using resource DLLs

查看:83
本文介绍了在不使用资源 DLL 的情况下向 MFC 项目添加第二种语言的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个用 MFC 制作的带有德语菜单的非常小的应用程序.现在我也被要求制作英文版.但目标是不使用其他语言 DLL.最后应该只有两个 .exe 文件(德语和英语).您是否知道如何为英语对话添加第二个资源文件(如果需要)以及如何在我的 C++ 代码中使用它们的分步手册?我的想法是使用预处理器指令:

#ifdef APPLANG=ENm_wndRibbonBar.LoadFromResource(IDR_RIBBON_EN);#否则如果m_wndRibbonBar.LoadFromResource(IDR_RIBBON);#万一

这样做是个好主意吗?简单复制现有资源文件并将其修改为英文的正确步骤是什么?

我尝试了多种方法,例如制作新资源文件并将原始德语菜单复制到新文件中.但是我收到了几个错误,例如输入合法资源 ID".

解决方案

您可以在单个 .exe 文件中嵌入多种语言资源,而无需使用资源 DLL.Visual Studio 资源编辑器不支持此用例,因此必须使用文本编辑器手动编辑资源脚本.

要定义资源,请使用 Visual Studio 资源编辑器不会尝试修改的 .rc2 文件.确保使用 Unicode (UTF-16 LE) 编码存储 .rc2 文件,并始终以换行符结尾,否则资源编译器将失败.

步骤

  1. 通过 MFC 应用程序向导创建的项目已经包含一个空的 .rc2 文件,我们可以将其用作主".rc2 文件.否则,请遵循

在代码中你可以像往常一样加载资源,Windows 会自动加载与当前用户区域设置最匹配的语言资源.如果没有找到匹配项,它会回退到英语.

如果要显式加载给定语言的资源,可以使用具有语言参数的资源函数来实现,例如.G.FindResourceEx().

包括标准 MFC 资源

事不宜迟,标准 MFC 资源 将仅包含在项目资源属性中配置的主要"语言中.

在我使用这种方法的项目中,我不需要标准的 MFC 资源.以下只是一个未经测试的想法.

在每个不是主要"语言的特定于语言的 .rc2 文件中,在 LANGUAGE 行之后为 MFC 标准资源添加 #includes,例如.:

LANGUAGE LANG_GERMAN, SUBLANG_NEUTRAL#ifdef __AFXRES_RC__#undef __AFXRES_RC__//能够包含多个语言版本的 afxres.rc#万一#include "l.deu\afxres.rc"//标准 MFC 资源字符串表开始IDS_STRING1 "Stapelüberlauf"IDS_STRING2 "Stack Overflow (englisch für Stapelüberlauf) ist eine Internetplattform, auf der angemeldete Benutzer Fragen zum Thema Softwareentwicklung stellen können."结尾

#undef 用于规避标准 MFC 资源文件的包含保护,这通常会阻止多个包含.在我们的例子中,这是可以的,因为资源最终会出现在可执行文件的不同语言资源部分中.

I have a really little application made with MFC with german menus. Now I was asked to make an english version too. But the goal is to not use additional language DLLs. There should only be two .exe files (german and english) at the end. Do you know any step by step manual how to add a second resource file for the english dialogues (if needed) and how to use them in my C++ code? My idea is to use a preprocessor directive:

#ifdef APPLANG=EN
    m_wndRibbonBar.LoadFromResource(IDR_RIBBON_EN);
#else if
    m_wndRibbonBar.LoadFromResource(IDR_RIBBON);
#endif

Is it a good idea to do it this way? And what are the right steps to simply copy the existing resource files and modify them to English?

I have tried several things like making a new resource file and copy the original german menu to the new file. But than I get several errors like "Enter a legal resource ID".

解决方案

You can embed multiple language resources in a single .exe file without having to use resource DLLs. There is no support for this use case by the Visual Studio resource editor, so resource scripts must be edited manually, using a text editor.

To define the resources, use .rc2 files which the Visual Studio resource editor won't try to modify. Make sure to store the .rc2 files with Unicode (UTF-16 LE) encoding and always end them with a line break, otherwise the resource compiler will fail.

Steps

  1. A project created through the MFC application wizard already includes an empty .rc2 file, which we can use as our "main" .rc2 file. Otherwise follow the documentation or create a new MFC application using the wizard to dissect how the .rc2 file is included.
  2. In the main .rc2 file add an #include for each language-specific .rc2 file:

    #include "lang_en.rc2"
    #include "lang_de.rc2"
    
    // Restore default language for resources included after current file
    LANGUAGE LANG_ENGLISH, SUBLANG_NEUTRAL
    

  3. Create the language-specific .rc2 files. Each file must start with LANGUAGE <LANGID>, <SUBLANGID> to indicate the language of the following resources:

    lang_en.rc2

    LANGUAGE LANG_ENGLISH, SUBLANG_NEUTRAL
    STRINGTABLE
    BEGIN
        IDS_STRING1 "Stack Overflow"
        IDS_STRING2 "Stack Overflow is a privately held website, the flagship site of the Stack Exchange Network, created in 2008 by Jeff Atwood and Joel Spolsky."
    END
    

    lang_de.rc2

    LANGUAGE LANG_GERMAN, SUBLANG_NEUTRAL
    STRINGTABLE
    BEGIN
        IDS_STRING1 "Stapelüberlauf"
        IDS_STRING2 "Stack Overflow (englisch für Stapelüberlauf) ist eine Internetplattform, auf der angemeldete Benutzer Fragen zum Thema Softwareentwicklung stellen können."
    END
    

  4. Build the executable and examine it in a resource editor to see if the resources are actually included for multiple languages. Instead of using a resource editor, you can also open the .exe file in Visual Studio to examine its resources. For instance, the demo .exe I build for this answer, looks like this, when opened in Visual Studio:

In the code you can load resources as usual, Windows will automatically load the language resource that best matches the current user locale. If it doesn't find a match, it falls back to English.

If you want to explicitly load a resource for a given language, you can do this by using the resource functions that have a language parameter, e. g. FindResourceEx().

Including standard MFC resources

Without further ado, standard MFC resources will be included only in the "main" language that is configured in the resource properties of the project.

In the projects where I have used this approach, I had no need for the standard MFC resources. The following is just an untested idea how it could possibly be done.

In each language-specific .rc2 file that is not the "main" language, add #includes for the MFC standard resources after the LANGUAGE line, e. g.:

LANGUAGE LANG_GERMAN, SUBLANG_NEUTRAL

#ifdef __AFXRES_RC__
    #undef __AFXRES_RC__    // To be able to include multiple language versions of afxres.rc
#endif
#include "l.deu\afxres.rc"  // Standard MFC resources

STRINGTABLE
BEGIN
    IDS_STRING1 "Stapelüberlauf"
    IDS_STRING2 "Stack Overflow (englisch für Stapelüberlauf) ist eine Internetplattform, auf der angemeldete Benutzer Fragen zum Thema Softwareentwicklung stellen können."
END

The #undef is there to circumvent the include guard of the standard MFC resource file, which normally prevents multiple includes. In our case this is OK, because the resources will end up in different language resource sections of the executable.

这篇关于在不使用资源 DLL 的情况下向 MFC 项目添加第二种语言的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆