MFC:使用cstring的loadstring [英] MFC: loadstring using cstring

查看:536
本文介绍了MFC:使用cstring的loadstring的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Visual Studio 6在VC ++中编写基于对话框的应用程序。这是一个文本很少的简单应用程序,但现在需要支持多种语言。由于文本不多,计划是将所有不同的语言添加到一个具有唯一标识符的字符串表中。所以现在一个示例字符串表看起来像这样,

I'm having a dialog based app written in VC++ using Visual Studio 6. It's a simple app with very few texts, but now those needs to support multiple languages. As it's not many texts, the plan is to add all the different languages into just one string table with a unique identifier. So now a sample string table looks like this,

STRINGTABLE
BEGIN
    IDS_STRING_OK_BUTTON_ENG "OK"    //English text
    IDS_STRING_EXIT_BUTTON_ENG "Exit"   //English text
    IDS_STRING_OK_BUTTON_FRA "D'accord"   //French text
    IDS_STRING_EXIT_BUTTON_FRA "Sortie"   //French text
END





现在我有一个函数将根据操作系统语言设置返回一个字符串。





Now i have a function that will return a string based on the OS language setting.

CString strLang = "";
    //Retrieves the system default locale identifier
    LCID lcid = GetSystemDefaultLCID();

    //Determine the language identifier from the locale identifier
    LANGID langid = LANGIDFROMLCID(lcid);

    //Does many processing here........
    //.................................
    //.................................
    // So if English is the OS language then this function will return "_ENG".





我尝试过:



现在代码的另一部分,这个独特的语言ID被连接起来另一个字符串,用于查找特定语言的文本。





What I have tried:

Now in another part of the code, this unique language ID is concatenated with another string to find the language specific text.

CString okButton = "IDS_STRING_OK_BUTTON" + m_strLanguageIndex; //Here m_strLanguageIndex for example will be "_ENG"





最后的字符串是



The final string will be

IDS_STRING_OK_BUTTON_ENG





所以这样,我可以只有一个包含所有不同语言的字符串表,然后使用上面的方法创建一个唯一的资源ID。



但现在的挑战是资源ID在resource.h文件中是整数。所以上面的CString没有用找到相应的文字。



所以我不确定这是否会起作用。我只是把它抛出来看看是否有人有更好的想法或有任何建议使上述方法有效。



我不想为每个人创建多个DLL语言,因为这是一个简单的基于对话框的应用程序。



So this way, i can have just one string table with all different languages and then use the above method to create a unique resource ID.

But now the challenge is, the resource IDs in resource.h file are integers. So the above CString is of no use to find the corresponding text.

So am not sure whether this is going to work. Am just throwing it out to see whether anyone has better ideas or have any suggestion to make the above method work.

I don't want to create multiple DLLs for every language as this is a simple dialog based app.

推荐答案

资源DLL是建议的方法,但如果你有理由不想使用它们,构建代码中的资源ID是不可能的。



resource.h中的ID是整数,并将它们称为例如IDS_STRING_OK_BUTTON_ENG只能在编译时完成。但是你可以这样做。



resource.h:

Resource DLLs is the recommended way to do this, but if you have reasons to not want to use those, constructing the resource IDs in code is not impossible.

The ID's in resource.h are integers, and refering to them as e.g. IDS_STRING_OK_BUTTON_ENG can only be done at compile time. However you could do something like this.

resource.h:
#define IDS_STRING_OK_BUTTON_ENG	100
#define IDS_STRING_EXIT_BUTTON_ENG 	101
#define IDS_STRING_OK_BUTTON_FRA	200
#define IDS_STRING_EXIT_BUTTON_FRA 	201





.rc文件:



.rc file:

STRINGTABLE
BEGIN
    IDS_STRING_OK_BUTTON_ENG "OK"    //English text
    IDS_STRING_EXIT_BUTTON_ENG "Exit"   //English text
    IDS_STRING_OK_BUTTON_FRA "D'accord"   //French text
    IDS_STRING_EXIT_BUTTON_FRA "Sortie"   //French text
END



然后将ID作为英文ID +语言引用依赖偏移量。在这个例子中,French的偏移量为100。


And then refer to the ID's as the English ID + a language dependent offset. In this example the offset for French would be 100.


当Windows已经提供了拥有多语言资源的能力时,为什么要尝试重新发明轮子?请参阅多语言资源 [ ^ ]。
Why try to re-invent the wheel, when Windows already provides the ability to have multi-language resources? See Multiple-Language Resources[^].


这篇关于MFC:使用cstring的loadstring的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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