donguy76
donguy76

Reputation: 640

MFC: LoadString using CString

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".

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"

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 the 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.

Upvotes: 2

Views: 2029

Answers (1)

Anders
Anders

Reputation: 101559

Win32 resources are stored in a hierarchy; type\name\language and the resource functions will look for a resource matching the threads language if it exists. This will allow you to use an id like IDS_STRING_OK_BUTTON in your code and your resources will contain two different string tables, one for each language.

In a .RC file you can use the LANGUAGE statement to create translated copies of different resources:

Defines the language for all resources up to the next LANGUAGE statement or to the end of the file.

When the LANGUAGE statement appears before the beginning of the body of an ACCELERATORS, DIALOGEX, MENU, RCDATA, or STRINGTABLE resource definition, the specified language applies only to that resource.

Another common way to provide translation support is to create a resource .DLL for each language and just load the desired .DLL and use its HMODULE handle when loading resources. Windows can do the work for you if you create MUI resources.

Upvotes: 2

Related Questions