Multilingual support of implementing software with VC : Bian far Published: 2001/04/25 Article Summary: This article describes how to prepare their own bilingual or multilingual software. First, create a DLL containing all English resources. When using this resource DLL, the application will appear in English, that is, the software is English; then create a Chinese DLL, run the StateDemo program again, the program will appear in Chinese (Even if the application is not recompiled). Key words VC , app, English resources, Chinese resources
-------------------------------------------------- ------------------------------ Text:
Use VC to implement software multilingual support
Foreword
Use only one source code to easily support multiple texts and multiple regions, then this software can be easily translated into a local version, which is called localization. How can I not modify any source code to make it dynamically conversion to different geographical resources? That is one of the programs characteristics of Windows - resources. Maintaining the visible resources used in the software in a resource DLL (Dynamic Link Library Dynamic Link Library), it is easy to implement geographicalization because it puts the specific text components individually in a file, So, a executable can load several different languages, and select the subroutine to load the appropriate text DLL. Create an instance of a CString object, and use the resource identifier of the string (String ID) to avoid cumbersome string coding. In most cases, the resource is included in the unit of the application. If you call the AFXSetResource Handle, you can point to another different unit, and the StateDemo program given below is handled. Call the AFXSetResource Handle, collecting the resources required from the DLL resource, can use a completely different resource (such as String string, Dialogue dialog, BMP bitmap, Menu menu, etc.) by replacing DLL resources in different languages ). In the initial case, StateDemo as the main actuator does not contain any resources. First, create a DLL containing all English resources. When using this resource DLL, the application will appear in English, that is, the software is English; then create a Chinese DLL, run the StateDemo program again, the program will appear in Chinese (Even if the application is not recompiled).
I. Creation and compilation of applications
(1) Creating a StateDemo.exe file (1) Creating a StateDemo workspace In the Microsoft Visual C 5.0 development environment, click the File | New option, select MFC AppWizard (Exe) in the Create Type Selection box New, ProjectName: StateDemo, create The new project StateDemo workspace, the application type Select Multiple Documents, click Finish to complete the workspace. (2) Adding a string resource This step is actually adding a string resource for English resource DLL, because the English resource StateDemo.rc will be removed from the project, and copy it into English resources, not Just established StateDemo.exe file project, the executive EXE file will reference this resource, and the string is in the resource DLL. Select String Table in the StateDemo project resource, right click in StringTable, select New String, add the following code, then select the File | Save all option in the menu to complete. ID: IDS_MINE = 300 // Main Framework IDS_MINE Defined to 300, the value of ID is 300 caption (Title): hi! It's my demo vc5 program of english version. (3) Add a CSTATEDEMO class data member This program is dynamically Loading the DLL containing all resources, so you must save the DLL's handle (handle), will release the Handle later and add private data members in the CSTATEDEMOAPP class (the code is as follows). Private: hinstance m_hdemodll; // Resource DLL Handle handle You can select the ClassView tab in the write of MS-VC5, in the left Workspace window, click the CStatedemoApp class, click the mouse button to select Add Member Variable, then enter Type and Declarition, and Choose a private type. (4) Modifying the cStatedemoApp :: InitInstance function application The application that contains all resources, in this case, English resource DLL is named resource.dll. Add the following code to the top of the initialization function initInstance.
BOOL CStateDemoApp :: InitInstance () {AfxEnableControlContainer (); // Standard initialization // If you are not using these features and wish to reduce the size // of your final executable, you should remove from the following // the specific initialization routines You do not need. // Turn StateDemo Resource DLL. m_hdemodll = AFXLOADLIBRARY (_t ("resource.dll")); if (! m_hdemodll) // If the resource.dll file handle does not exist {AFXMessageBox (_t ("i can 'T load the resource dll! ")); // Did not find the resource.dll file, pop-up message box issued a warning: i can't load the resource dll!; // Warning Message box, Return False shown in Figure 2 } // Tell the application to set the resource handle M_HDemodll; AFXSetResourceHandle (M_HDemodll);} (5) Using WizardBar Handling CStateDemoApp's ExitInstance function You need to release the DLL loaded in front of AFXLOADLIBRARY, add the following code to function EXITINSTANCE Reach this. Select Menu View | Class Wizard, select CStatedemoApp in the ClassName Edit window of Message Map, then find EXITInsTance under Messages, select Edit Code and add the following code. int CStateDemoApp :: ExitInstance () {// TODO: Add your specialized code here and / or call the base class if (m_hDemoDLL) AfxFreeLibrary (m_hDemoDLL); // release resource DLL; retrun CWinApp :: ExitInstance ();} (6 Modifying CStatedemoview :: OnDraw displays a variety of resources such as different language strings and ICON in the multiple-document type, add the following code in the function.
Void cStatedemoview :: Ondraw (CDC * PDC) {cStatedemodoc * pdoc = getDocument (); assert_valid (pdoc); PDC-> Drawicon (10, 10, AFXGetApp () -> loadicon (iDR_mainframe); // From Resource DLL Turn into the icon (ID is IDR_MAINFRAME) and draw in the window 10, 10 position; cstring strmessage; // Define StrMessage is a cstring string type; // Display strings from Resource DLL; strmessage.loadstring (IDS_DEMO); PDC-> TextOut (60, 65, strmessage);} (7) Shot StateDemo.RC from StateDemo Project STATEDEMO.RC does not contain any resources in the program, only in Resource.dll, so you want to remove all in this item. Resources and put them in resource.dll, but not delete, this is the flexibility of using MFC. The specific operation is as follows: Select FileView under Workspace, click StateDemo.rc, press the delete key on the keyboard to delete, at which time all resources are not in the project. (8) Compiling the StateDemo project to start compiling the project, click the menu building | Execute StateDemo.exe option, because there is no resource in the project, therefore popping up the error message box "I can't load the resource dll!", As shown in Figure 2 As shown, click OK and do not pay attention. Now, the STATEDEMO project's executive StateDemo.exe is built, which does not contain any resources, but rely on dynamic link library DLL to provide all resources. Below to create, English resource DLL, after this DLL is built, copy the resource DLL to the debug or release directory of this project, you can run the app. Ii. Create English Resource.dll
(1) Creating an English New Project Area Selecting the MFC AppWizard (DLL) in the Creation Type Selection box New, create an English new project area, select the directory for the workspace under location ... / StateDemo / ie ... / StateDemo / English as the project area directory, click OK, select Regular DLL Using MFC DLL in Step1, click OK. (2) Deleting the DLL that all the files just created in the project contains some resources, but its various files do not need, so all files are selected in the Workspace fileview, press DELETE to delete all files. (3) Copying the English resource file from the StateDemo project Now uses the English resource that has just been removed from StateDemo, and a resource string has been added to the StateDemo.rc file, where the resource string works. Copy STATEDEMO.RC and Resource.h under the ... / statEMo directory to ... / statedemo / english directory. The purpose is to transfer all resources under ... / statEdemo / RES to ... / statedemo / english / res directory. The following list is listed below to copy all files from the project in English and their comments. Required copy of the file Note StateDemo.rc resource file resource.h resource header file RES / StateDemo.ico main program icon RES / StateDemo.rc2 User Resource Description RES / STATEDOC.ICO WIR Icon Res / Toolbar.BMP Tools Figure (4) Add the resource file to the English project, make the icon to English resource style to add StateDemo.rc to the English project, you can choose "ENGLISH in the File View tab of the Workspace window of VC5 Develop Studio ". Click Right-click to select the Add Project to Workspace option to join the file. Here, you can see from the Resource View tab that the string IDs_demo ("IT's My Demo ..." added in the first StateDemo project is now moved to the English project and will become resource. One member of the DLL. Since this English project is an English resource DLL, it is named resource.dll (which is the same name established in the back, so that the EXE execute program to access bilingual or multilingual DLL), so modify the icon to demonstrate StateDemo.exe It is an English version when being transferred. Modify the IDR_MAINFRAME icon to English cultural characteristics, or in the icon column of the Resource View tab, click Right click to select Import to import the icon you want, it can be 16 * 16, or 32 * 32, this demo program It is the use of the British flag as IDR_mainframe to show the English version. You can arbitrarily modify other resources in the Resource View as any of your needs and styles. (5) Modifying the settings in the link target program to establish an English resource DLL has basically completed, but also modify the settings before the DLL program in Link. Select the Workspace tab, select the Project | Settings option, select the Link tab in the window, change some link settings, change the output file name to resource.dll, which is the English resource DLL of the Chinese and English resources, in the link command Project Add / Nontry in Options, its function is to tell the linker This is a DLL containing a unique resource that does not include entry points.
Figure 3 shows the modified Link tab dialog, pay attention to the / noentry option to be added to the public item compile control. At this point, the English resource DLL creates success. When running it, press the hotkey F7 link (there is a window pop-up, let the user choose the exe file to be related, can save), and resource.dll is in the ... / statedemo / english / debug directory. If you need to run your English demo, copy it to the StateDemo / Debug directory, then run the StateDemo.exe file, now the English resource works, it shows English and British flags (shown in Figure 4). Iii. Create Chinese Resources RESOURCE.DLL
(1) Create a Chinese new project area and remove all files in the Chinese project Select the MFC AppWizard (DLL) in the Create Type Selection box New, create a Chinese new project area, and select the directory for the workspace under location ... / StateDemo / That is, ... / statEdemo / Chinese as the project area directory, click OK. Other options are described above with the English project area above. Just established DLL has some resources, but its various files are not needed here. So, in Workspace fileview, all files are selected, press DELETE to delete all files. (2) Copying resource files still use the English resources removed from StateDemo, and copy the directory ... StateDemo / under StateDemo.rc and Resource.h to the original Chinese resource project directory under Chinese, will ... / StateDemo.rc and resource.h copy below the StateDemo directory to ... / statedemo / chinese directory. The purpose is to transfer all resources under ... / statEdemo / Reside to ... / StateDemo / CHINESE / RES directory, all files and comments required to copy to the project StateDemo to the project Chinese, as shown in Table 1 above. (3) Add the resource file to the Chinese project to make the icon to Chinese resource style to add StateDemo.rc to the Chinese project, you can choose "Chinese" in the File View tab of the VC5 Develop Studio Workspace window, single Right click to select the Add Project to Workspace option to join the file to the project. Here, the string IDS_DEMO in the StateDemo project has been moved to the Chinese project and will become a member of Chinese Resource.dll. The current resource.dll is the Chinese resource DLL (with the same name established in front), the IDS_DEMO is changed to the Chinese string, the code is as follows: IDS_DEMO = 300, CAPTION: "Hello! This is MS VC5 Chinese Demonstration! "After the strings are complete, you need to process the menu to change the menu, that is, change the menu item to Chinese. Open the Resource View label, double-click the MENU item, translate English into Chinese in the MFC standard menu displayed on the right workspace; you can also add a menu item to fully personalize it by software. This demo has used China National Flag to make IDR_MAINFRAME to indicate a Chinese version. You can also modify other resources in Resource View by any of your needs and styles. (4) Modifying the Chinese DLL Link Target Program and Settings Chinese Resources DLL in the Resource Components You also need to modify the settings of the DLL program in front of Link, the method is the same as in English: Select Workspace tab, Project | Settings menu, Link tab To change some link settings, change the output file name to resource.dll, add / nontry in the link command Project Options (see Figure 3). Modify the settings in the resource component, in the Resource tab in the Project | Settings menu, change the default text to Chinese (if it is Chinese Windows, it is already Chinese), then delete the pre-processing definition _ AFXDLL, Finally, enter the path to the Visual C 5.0 installation directory in Directory. This directory is put into Chinese specific resources.
There are other resources such as /1.fra French, / 1.ita Italian, / 1.Kor Korean, etc., you can choose to use when needed. Deleting the pre-processing definition _AFXDLL and enter the path to the resource is to include the universal resource. If there is no do, the application collects universal resources from the MFC installed in the system. Application specific resources will be unwanted, universal MFC resources (such as cursor, print preview, and universal properties page). The modified settings are shown in Figure 6. (5) Link and run Chinese resource DLL Press the hotkey F7 to link, Chinese resource resource.dll is in the ... / statedemo / Chinese / debug directory. To read Chinese, copy it into the StateDemo / Debug directory, then run the StateDemo.exe file, then the display is the Chinese version. All resources are cultural, such as China Flag, Chinese menu, etc., the role is the just established Chinese resource.dll, and the operation results are shown in Figure 7. It should be noted that due to the Chinese resource DLL contains all the universal MFC resources, it is more than English. At this point, the DLL of the two resources is established. In this presentation program, the resources are all removed from the executable and are put in a DLL. When any resource is referenced in the MFC class (such as CString :: LoadString), the current resource handle can be used. Typically, the current resource handle and the application's resource handle are the same, but this demo has changed this by AFXSetResourceHandle, which allows the MFC to extract all resources by passing the handle of the AfxSetResourceHandle. When the English DLL is replaced by Chinese DLL, the resource ID is accessed to position the resources located in Chinese DLL. Since the DLL is translated from the initial DLL copy, all resources should be complete, but it is translated into Chinese. The resource of the Windows application is generally maintained on the execution program and is loaded when needed. When your application needs some kind of resource, you need to provide two things to Windows: an instance handle of the application and an identifier of the resource. Application Multilingual Installation Standard Software Installation Tool Install SHIELD 5 is provided in Microsoft Visual C 5.0 Enterprise Edition. In all the tools for making installation software, it is the most professional and most powerful. Making a general practical installer with install shield 5 is simple, just add code in generating icons, etc.. Here is only discussed before installing the software to identify Windows versions and languages, and automatically select your language or multilingual software to install in accordance with the operating system based on the operating system. Under the Windows operating system, Microsoft's Win32 resources provide very complete features. In order to obtain the language version of the current operating system, you can call the Win32 function getoemcp (), which is to get the OEM (Original Equipment Manufacturer source device vendor) code page of the current operating system, which is defined as follows: Unit getOemcp (void) / / Getoemcp (void) does not have any parameters; if the function call is successful, return the value of the current system OEM code page; if the current system does not have an OEM code page, return the user-defined default value. The meaning of several defined values is listed below.
Delegation Meaning 936 Chinese (PRC, SINGAPORE) People's Republic of China, Singapore 950 Chinese (TAIWAN, HK) China Taiwan, Hong Kong 437 United States USA 949 Korean South Korea 932 Japan Japan 855 Russian Russia installed in Windows 95 or Windows NT platform Before the software, first select the dynamic link library DLL corresponding to the language version according to the language version of the current system, generate software that meets the operating system version. For example, you can use the following code to determine if the current simplified Chinese: IF (getoemcp () == 936) {.....} Note
In addition, there is an approach to adopting Autorun technology. If the application is used as a carrier, you can work hard on file autorun. Autorun is a file placed in a disc directory. When the optical disk is placed in the optical drive, Windows 95 will automatically run it. According to the different resource DLL files required for different language version software, use install sheld 5 to make two sets of installations, separately in their respective directories, and the SPLASH copyright cover picture setup.bmp can also be different depending on the region. For example, putting the Chinese mainland national code installer in the / gbsetup directory, the Chinese BIG5 code installer is placed in the / big5setup directory. You can add the following code as above, add the following code in Autorun.exe. IF (getOemcp () == 936) {:: Winexec (_t ("gbsetup / setup.exe"), sw_show); // Call SDK function WINEXEC execution command line program setup.exe} if (getoemcp () == 950 {:: Winexec (_T ("BIG5SETUP / SETUP.EXE"), sw_show);} You can automatically identify the Windows operating system language version and install the respective corresponding language versions of the software. In some cases, the user needs a greater freedom, you can pop up the window before installing, ask which language version you want to install. In the Autorun.exe executable, the installation files in their respective directories are invoked according to the user's selection. At this point, you can use the contents of this article to prepare your own bilingual or multilingual software. After the software test is completed and completed the necessary work such as help files, make a personality installation file, so that your bilingual or multilingual software is born.
Author member name: Mighill