Coding Tips (JavaScript/CSS/VBA/Win32)

Useful code snippets, tips and some Windows applications


Using String Tables in Win32

This short article will show a simple way to use string tables to localize your Win32 applications.
Contrary to some expectations, it's very easy to use string tables in plain Win32.
First of all, create a resource-definition statement in a resource file. The format should be similar to the following:

STRINGTABLE
{

1000, "English"
1001, "Display"
1002, "Add"
1003, "Remove"

2000, "French"
2001, "Affichage"
2002, "Ajouter"
2003, "Enlevez"
}

It could be contained in your main resource file or a separate file with a .rc extension.

Each string table section deals with a particular language. Each language section increments a string id by 1000.

We'll use a LoadString() function to read a string from the table and load it into a character array. The function has the following syntax:

int LoadString( HINSTANCE hInstance,
UINT uID,
LPTSTR lpBuffer,
int nBufferMax
);

An example:

HINSTANCE hInst;
char str[32];
LoadString(hInst, 1001, str, 32);

Since our string table is structured in such a way that the only difference between corresponding strings in English and French is the thousand digit, we can use the LoadString function in our own function the following way:

BOOL LoadStrings(HWND hwnd, int n){

char str [32];
LoadString(hInst, n+1, str, 32);
SetDlgItemText(hwnd, IDC_TEXT, str);
return TRUE;
}


So, to switch a language display for a certain string, we just need to pass a window handle and a language identifier, 1 or 2.
For English we pass 1 as a parameter, for French we pass 2. If we use any other languages, we'll pass 3, 4, etc. You get the idea.
This advantage of this approach is its simplicity. You don't need to learn the format of the message file nor how to use other language-related Windows functions, such as GetUserDefaultUILanguage().

Based on an article 'Localizing Delphi applications using StringTable resources' at
http://delphi.about.com/library/weekly/aa011805a.htm .