Re: Alternative to 'AfxSetResourceHandle'
switch (GetUserDefaultLangID())
{
case MAKELANGID(LANG_ARMENIAN, SUBLANG_DEFAULT):
ASSERT((m_hResLibrary =
LoadLibraryEx(TEXT("MyApphye.dll"), NULL,
LOAD_LIBRARY_AS_DATAFILE)) != NULL);
break;
case MAKELANGID(LANG_RUSSIAN, SUBLANG_DEFAULT):
ASSERT((m_hResLibrary =
LoadLibraryEx(TEXT("MyApprus.dll"), NULL,
LOAD_LIBRARY_AS_DATAFILE)) != NULL);
break;
}
Note 1:
This kind of code does not scale well, because you have to change your code
to add a new language.
Why not something like this:
TCHAR szLocaleName[50];
TCHAR szResFile[_MAX_PATH];
GetLocaleInfo(lcid, LOCALE_SABBREVLANGNAME, szLocName, _countof(szLocName) );
_sntprintf( szResFile, _countof(szResFile), TEXT("MyApp%s.dll"), szLocName );
m_hResLibrary = LoadLibraryEx(szResFile, NULL,LOAD_LIBRARY_AS_DATAFILE);
Note 2:
GetUserDefaultLangID is to be used for number/date/time/currency format, not
for UI preferences.
For UI you have to eigher use GetUserDefaultUILanguage, or, if you want to
allow your application to differ from the OS language, you can allow the user
to change the setting (and store the preference as config data).
Note 3:
If you want to match the system (using GetUserDefaultUILanguage), then you
can just let MFC do the work, there is no extra code that you have to do.
Because MFC does that detection and loads the language DLL by itself.
The naming convention for the language DLLs depends on the MFC version.
Note 4:
_countof is nice, but it was introduced in VS 2005.
If you use an earlier version, you can do:
#define _countof(a) (sizeof(a)/sizeof(a[0]))
Or, more portable:
#ifndef _countof
#define _countof(a) (sizeof(a)/sizeof(a[0]))
#endif // _countof
--
Mihai Nita [Microsoft MVP, Windows - SDK]
http://www.mihai-nita.net
------------------------------------------
Replace _year_ with _ to get the real email