HOWTO: Create Localized Resource DLLs for MFC Application
ID: Q198846
|
The information in this article applies to:
-
Microsoft Visual Studio 97
SUMMARY
Resource DLLs are an efficient means for using resources in different
languages. Applications can supply a single DLL that has localized
resources through their setup program. Changing to another language is a
simple matter of using the correct DLL.
The information below discusses how to make a localized resource DLL
with Visual C++ 5.0.
MORE INFORMATION
This article describes an easy, general method for
generating an application and resource DLLs capable of supporting
multiple languages. By using this method, you have all of your resources in one RC file, making it fairly easy to
see what resources you've implemented.
- Create a default MFC AppWizard application (called MyApp in this
example), and specify U.S. English for the resources language. Use MFC library as a shared DLL.
- Insert a default MFC AppWizard DLL as a top-level project (ResourceDll in this example), and select MFC Extension DLL
(using shared MFC DLL) for the type of DLL. Because no .lib file is created for the DLL, a top-level project avoids linker problems in
subsequent builds of the executable.
- Remove and delete the RC file, the Resource.h file, the ResourceDll.rc2
file, and the RES directory from the ResourceDll project.
- From the Project menu, select the Add To Project pop-up menu and
choose Files. Insert the MyApp.RC file into the ResourceDll project.
- For each additional language that MyApp will have localized resources for:
- From the Build menu, click Configurations and add a new
Release and Debug configuration. For example, add a French Release
configuration and copy its settings from ResourceDll - Win32
Release and add a French Debug configuration and copy its
settings from ResourceDll - Win32 Debug.
- In the Project Settings dialog box, choose Multiple Configurations, in
the Settings for combo-box. Choose both the release and the debug
project configurations for a particular language in the Select
Project Configurations to Modify dialog box. In the Resources tab,
add the preprocessor definitions AFX_RESOURCE_DLL and AFX_TARG_XXX,
where XXX is the a letter specifier for the language. For
example, FRA for French [France, DEU for German, and ENU for English (USA)].
NOTE: The preprocessor definitions are comma separated with no
spaces between definitions.
- In the Language combo-box, select the language used in the resource.
NOTE: You may want to name your DLLs to help identify them. For example you may want to add a "d"
to the root name to mark it as a debug DLL, and use language-specific file extensions to note the locale. Be aware that
a debug version of the executable will not work with a release
version of the resource dll.
This can be done by changing the output target that the linker
uses for each build of the DLL.
- Select the Link property page from the Project settings
dialog box.
- In the in the Settings For combo-box, highlight the
configuration for which you want to change the DLL output
directory.
- Make sure Category is set to General and specify the
output directory name in the edit box under Output File Name.
For example, for a dll containing localized resources for
French, change Debug/ResourceDLL.dll to French Debug/
ResourceDLLd.FRA for the Win32 Debug configuration and change
change Release/ResourceDLL.dll to French Release/
ResourceDLL.FRA for the Win32 Release configuration.
To use these DLLs, you must copy the desired
DLL into the same directory that MyApp.exe is in, or an
appropriate Windows directory, and rename it to ResourceDll.dll
or ResourceDllD.dll. For example:
copy ResourceDll.FRA\windows\system\ResourceDll.dll
You can also specify the directory where the resource file,
MyApp.res, will be generated for each localized DLL.
- Select the Resources property page from the Project
settings dialog box.
- In the in the Settings For combo-box, highlight the
configuration for which you want to change the resource output
directory.
- Make sure Category is set to General and specify the
directory where the output file for the resources will be
generated in the edit box under Resource file name.
- From the Project menu, select Settings and highlight the
ResourceDLL project. Choose All Configurations from the Settings
For combo box. Click the C/C++ tab followed by its Preprocessor
category. Add the path for the MyApp project in the Additional
include directories field.
- From the Project menu, select Settings and highlight the MyApp
project. Choose All Configurations from the Settings For
combo box. Click the Resources tab, and in the Preprocessor
definitions field, add AFX_RESOURCE_DLL. This definition removes
all of the resources from MyApp.exe.
- In the ResourceView workspace, open each folder and, with the CTRL key
pressed, select all of the resources in the MyApp project. For example,
your selections should include an IDR_MAINFRAME for the Accelerator and
IDD_ABOUTBOX for the Dialog.
For each of the ResourceDLL configurations, on the Insert menu,
click Resource Copy, set the language to the appropriate language, and press OK. You should now have a complete set of resources
for anther language. The resources, however, must be translated.
(If the language for which you want to create localized resources is not in
the list of languages, select another language and manually edit the
text of the resource file. After you have made the appropriate changes,
the brackets next to the resource will say "Unknown Language" and show
the language and sublanguage identifiers.)
NOTE: You can also copy preexisting resources to the resource file at
this time. For example, create a separate MFC AppWizard
application project that uses the French language resources
by using the same selections you used to create the initial MyApp
project. After creating this application, close its workspace
and open the MyApp project workspace and select ResourceView.
Then, on the File menu, click Open, and open the French application's RC file. Now drag/drop all of the resources from the French
application into the MyApp application. These resources are
already in French and need no translation.
- Add an HINSTANCE member variable to the application's CWinApp derived
class. This will hold the DLL instance handle. For example:
HINSTANCE m_hInstResDLL;
Inside the CWinApp::InitInstance definition for your project, add the
following three lines at the top of the function:
#ifdef _DEBUG
// Load the debug version of the localized resources.
m_hInstResDLL = LoadLibrary("ResourceDlld.dll");
#else
// Load the release version of the localized resources.
m_hInstResDLL = LoadLibrary("ResourceDll.dll");
#endif
ASSERT( m_hInstResDLL != NULL );
NOTE: It is not necessary to call AfxSetResourceHandle() at this
point.
- Add a CMyApp::ExitInstance() member function using the Class
Wizard. Add code freeing the library before the application exits.
Modify the function as follows:
int CMyApp::ExitInstance()
{
// In case you load multiple DLL's make sure to free them,
// and avoid calling FreeLibrary with a NULL pointer.
FreeLibrary(m_hInstResDLL);
return CWinApp::ExitInstance();
}
- The application is ready to build. Using the Batch Build dialog box,
select the desired targets. Remember that you must copy
the appropriate DLL into the a suitable location and rename it.
REFERENCES
Tech note 56 (TN056) describes use of localized MFC resources (MFC40LOC.DLL).
Tech note 57 (TN057) describes some of the designs and procedures you
can use to localize your component, be it an application or an OLE
control, or a DLL that uses MFC.
Tech note 23 (TN023) describes the standard resources provided with and
needed by the MFC library.
For additional information about the Microsoft Foundation Classes included with Visual C++ versions 2.2 and earlier, please see the following
article in the Microsoft Knowledge Base:
Q147149 "How to Localize Resources with Foundation Classes"
(c) Microsoft Corporation 1999, All Rights Reserved. Contributions by Isaac Varon, Microsoft Corporation.
Additional query words:
localize localization
Keywords : kbIntl kbMFC kbVC500
Version : WINDOWS:97
Platform : WINDOWS
Issue type : kbhowto
Last Reviewed: February 11, 1999