c/c
vc 国际化的资源文件处理 -凯发ag旗舰厅登录网址下载
ms windows操作系统是一个世界上广泛使用的操作系统,对于不同语种的国家ms windows有相应语种的版本。在不同语种的windows平台上应该运行相应语种的应用程序。也就是说程序的用户界面(如菜单、对话框、状态条)中的提示文字应该使用和windows操作系统所使用的语种一致。当然英语用户界面的程序可以运行在其它语言平台上,但比较复杂的程序或多或少都有问题,如对话框的尺寸不对,特殊的ascii字符显示为文字,输入字符串可能会导致死机等。如果不一致却还想使用,则需要动态翻译软件。例如,在英文windows平台上运行中文版的ms office就需要像中文之星或四通利方这样的中文动态翻译软件。
当前国内使用的ms windows在语种上划分主要有简体中文版和英文版两种。为了让开发的软件能在这两种平台上使用,提出了三种凯发ag旗舰厅登录网址下载的解决方案:第一种凯发ag旗舰厅登录网址下载的解决方案是仅编写中文界面的程序。在中文windows上程序可以运行(这里的运行是指程序界面不出现乱码),在英文windows上依靠中文之星或四通利方这样的中文动态翻译软件也可以运行。第二种凯发ag旗舰厅登录网址下载的解决方案是针对中文版和英文版各编一个程序,即在第一种方案的基础上增加了英文版,这样做有两点好处:一是英文windows平台上运行英文版的软件比英文windows平台上运行中文版的软件稳定,因为没有像中文之星或四通利方这样的中文动态翻译软件的参与。第三种凯发ag旗舰厅登录网址下载的解决方案是自动根据操作系统的语种选择相应的界面语种。也就是说,同一个软件在中文windows上显示中文界面,在英文windows上显示英文界面。这种方案相对于第二种方案的好处在于:程序不用做英文版和中文版两个版本,只要一个版本就可以了,对于用户来说是很方便的。在这三种方案中,前一种是后一种的基础,后一种实现了,前一种也就已经实现了。下面将从易到难介绍三种方案实现的步骤和要点。
凯发ag旗舰厅登录网址下载的解决方案一、自动生成中文界面框架
visual c 6.0版本中可以使用appwizard自动生成具有基本中文界面的win32应用程序。使用appwizard时,在自动生成的第一个步骤中,将资源类型选择为简体中文就可以了,可参见图1。其余选项都不涉及语种问题,根据应用程序的具体情况进行选择即可。然后在此基础上作进一步的软件开发工作。
如果你在一边读文章一边上机实际操作,可能会发现一个问题:不论你怎样设定visualc 5.0的安装程序的选项,资源类型仅会支持英语、德语、法语、西班牙语和意大利语这五种语言。于是你可能会怀疑你所用的visual c 5.0版本有问题。实际上并不是visual c 5.0版本有问题,而是需要从安装盘上手工拷贝一些文件。所需文件在visual studio97软件包的第三张包含visual c 5.0的光盘上。在目录devstudio/sharedide/bin/ide下有几个以appwz开头的动态链接库(扩展名为dll的文件)。在这些动态链接库中,一个动态链接库提供对一种语言的支持。其中的appwzchs.dll就是支持简体中文的文件。将其拷贝到安装visual c 5.0的硬盘上相同名字的目录下即可。重新开始自动生成步骤,在自动生成的第一个步骤中,在资源类型选择中将出现简体中文支持。
凯发ag旗舰厅登录网址下载的解决方案二、支持多种语言
没有必要从头开发一个英文版。事实上,在现有中文版的基础上生成英文版并不复杂。工作量在于修改资源,程序的代码不用改变。
图2左边的resourceview窗口中,同时存在着中英文两种资源,在中文资源的后面标注有“chinese(p.r.c.)”以示区别。为了保证程序代码不变,相同界面元素的两种语言版本下的标识符必须相同。例如,产品介绍对话框的英文资源的标识符为idd_aboutbox,中文资源的标识符也应该是idd_aboutbox。可以用copy操作对资源进行复制,然后修改语种再修改标识符。 (在资源中用insertcopy copy一份资源)
resourceview提供的管理功能并不全面,还需要直接编辑资源文件resource.rc来完成全部任务。经过对resource.rc文件的研究,发现文件中使用编译控制开关来实现多语言支持。 ......
//
// chinese (p.r.c.) resources
#if !defined(afx_resource_dll) || defined(afx_targ_chs)
#ifdef _win32
language lang_chinese, sublang_chinese_simplified
#pragma code_page(936)
#endif //_win32
//
// dialog
......
/
// menu
......
//
// icon
......
//
// bitmap
......
//
// toolbar
......
#ifdef apstudio_invoked
/
// textinclude
1 textinclude discardable
begin
"resource.h/0"
end
2 textinclude discardable
begin
"#include ""afxres.h""/r/n"
"/0"
end
3 textinclude discardable
begin
"#define _afx_no_splitter_resources/r/n"
"#define _afx_no_property_resources/r/n"
"/r/n"
"#if !defined(afx_resource_dll) || defined(afx_targ_enu)/r/n"
"#ifdef _win32/r/n"
"language lang_chinese, sublang_chinese_simplified/r/n"
"#pragma code_page(936)/r/n"
"#endif/r/n"
"#include ""res//sample.rc2"" // non-microsoft visual c edited resources/r/n"
"#include ""l.chs//afxres.rc"" // standard components/r/n"
"#include ""l.chs//afxprint.rc"" // printing/print preview resources/r/n"
"#include ""l.chs//afxolecl.rc"" // ole container resources/r/n"
"#include ""l.chs//afxolesv.rc"" // ole server resources/r/n"
"#endif/0"
end
#endif // apstudio_invoked
......
#endif // chinese (p.r.c.) resources
/
// english (u.s.) resources
#if !defined(afx_resource_dll) || defined(afx_targ_enu)
#ifdef _win32
language lang_english, sublang_english_us
#pragma code_page(1252)
#endif //_win32
//
// dialog
......
/
// menu
......
//
// icon
......
//
// bitmap
......
//
// toolbar
......
/
// textinclude
1 textinclude discardable
begin
"resource.h/0"
end
2 textinclude discardable
begin
"#include ""afxres.h""/r/n"
"/0"
end
3 textinclude discardable
begin
"#define _afx_no_splitter_resources/r/n"
"#define _afx_no_property_resources/r/n"
"/r/n"
"#if !defined(afx_resource_dll) || defined(afx_targ_enu)/r/n"
"#ifdef _win32/r/n"
"language 9, 1/r/n"
"#pragma code_page(1252)/r/n"
"#endif/r/n"
"#include ""res//sample.rc2"" // non-microsoft visual c edited resources/r/n"
"#include ""afxres.rc"" // standard components/r/n"
"#include ""afxprint.rc"" // printing/print preview resources/r/n"
"#include ""afxolecl.rc"" // ole container resources/r/n"
"#include ""afxolesv.rc"" // ole server resources/r/n"
"#endif/0"
end
#endif // apstudio_invoked
......
#endif // english (u.s.) resources
/
#ifndef apstudio_invoked
/
// generated from the textinclude 3 resource.
#define _afx_no_splitter_resources
#define _afx_no_property_resources
#if !defined(afx_resource_dll) || defined(afx_targ_chs)
#ifdef _win32
language lang_chinese, sublang_chinese_simplified
#pragma code_page(936)
#endif
#include "res/sample.rc2" // non-microsoft visual c edited resources
#include "l.chs/afxres.rc" // standard components
#include "l.chs/afxprint.rc" // printing/print preview resources
#include "l.chs/afxolecl.rc" // ole container resources
#include "l.chs/afxolesv.rc" // ole server resources
#endif
#if !defined(afx_resource_dll) || defined(afx_targ_enu)
#ifdef _win32
language lang_english, sublang_english_us
#pragma code_page(1252)
#endif
#include "res/sample.rc2" // non-microsoft visual c edited resources
#include "afxres.rc" // standard components
#include "afxprint.rc" // printing/print preview resources
#include "afxolecl.rc" // ole container resources
#include "afxolesv.rc" // ole server resources
#endif
/
#endif // not apstudio_invoked
以上代码中在 #if !defined(afx_resource_dll) || defined(afx_targ_chs)宏定义后面为中文资源,在#if !defined(afx_resource_dll)|| defined(afx_targ_enu)宏定义后面为英文资源。应该保证中英文资源中都有textinclude段和#ifndef apstudio_invoked段。 这样mfc(microsoft fundermental class)缺省资源才会被包含。 对于中文资源来讲,这些额外资源的路径应加上l.chs目录名。
在编译生成可执行文件时,如果生成中文版本,则在project settings对话框中应按图3所示进行设置:在preprocessor definitions编辑框中加入afx_targ_chs和afx_resource_dll的定义,并且language选择框中应选择chinese(p.r.c.)。如果生成英文版本,则在project settings对话框中应设置如下:在preprocessor definitions编辑框中加入afx_targ_enu和afx_resource_dll的定义,并且language选择框中应选择english(united states)。
其中afx_targ_chs与936定义了资源文件的简体中文属性。
英文属性中为afx_targ_enu与1252,
日文属性中为afx_targ_jpn与932,
繁体中文属性中为afx_targ_cht与950。
翻译时必须改成相应的语言属性。
注意下面两句也应改动:
#include "l.chs//afxres.rc" // standard components
#include "l.chs//afxprint.rc" // printing/print preview resources
英文为:
#include "afxres.rc" // standard components
#include "afxprint.rc" // printing/print preview resources
日文为:
#include "l.jpn//afxres.rc" // standard components
#include "l.jpn//afxprint.rc" // printing/print preview resources
繁体中文为:
#include "l.cht//afxres.rc" // standard components
#include "l.cht//afxprint.rc" // printing/print preview resources
还有一点需要注意,就是所有在代码中显示的文字串都应存储在资源文件中,而不应该在程序中使用硬编码。例如,以下代码就是硬编码:
......
messagebox(_t("文件没有找到!"),_t("错误")) ;
......
这种提示仅限于windows简体中文版,而在没有像中文之星、四通利方等翻译软件的windows英文版平台上显示的将是一堆乱码。正确的方法应该是:
......
cstring string1, string2 ;
string1.loadstring(ids_file_not_find) ;
string2.loadstring(ids_error) ;
messagebox(string1, string2) ;
......
在中文资源中,ids_file_not_find对应“文件没有找到!”字符串,ids_error对应“错误”字符串;在英文资源中ids_file_not_find对应“file not found! ”字符串,ids_error对应“error”字符串。
三、不同语种平台的自动选择
实现这项功能的思路是使用纯资源dll,把应用程序中的资源都放在纯资源dll中。这种dll仅含有资源而没有可执行代码,也就是源文件中仅包含rc资源文件。应用程序中中文和英文资源可放在不同的dll中。例如,中文资源dll名为chinese.dll,它放置中文资源;英文资源dll名为english.dll,它放置英文资源。程序启动时根据windows平台语种的类型,加载相应的dll。以后资源就会从对应的dll中读取,从而能保证使用正确的资源.。
为了实现这项功能,需要在应用程序类中加入一个变量以存储资源dll的句柄并重载应用程序类中的initinstance和exitinstance函数。
在应用程序类声明中加入代码如下:
......
protected:
hinstance m_hlangdll ;
......
在initinstance函数开始处加入代码如下:
// get the primary language identifier
word wlangpid = primarylangid(::getsystemdefaultlangid()) ;
// load the language resource dll
switch( wlangpid )
{
case lang_english:
m_hlangdll = ::loadlibrary("english.dll") ;
break ;
case lang_chinese:
m_hlangdll = ::loadlibrary("chinese.dll") ;
break ;
}
if( !m_hlangdll)
{
afxmessagebox(_t("unable to load resource dll!")) ;
return false ;
}
// tell the application what module contains our resource
afxsetresourcehandle(m_hlangdll) ;
......
在exitinstance函数开始处加入代码如下:
// free language resource library
if(m_hlangdll)
{
freelibrary(m_hlangdll) ;
}
......
操作系统所使用的语言由win32函数getsystemdefaultlangid取得。宏primarylangid又取出主语言标识符进行判断,选择应该调用的dll。dll的加载由win32函数loadlibrary实现。程序中所使用的资源库由afxsetresourcehandle函数指定。程序退出时用win32函数freelibrary卸载。
还有,在编译资源dll时别忘了在连接时加上/noentry选项以告诉连接器这是一个唯一资源的dll,不包含程序执行的入口点。
与50位技术专家面对面20年技术见证,附赠技术全景图总结
以上是凯发ag旗舰厅登录网址下载为你收集整理的vc 国际化的资源文件处理的全部内容,希望文章能够帮你解决所遇到的问题。
如果觉得凯发ag旗舰厅登录网址下载网站内容还不错,欢迎将凯发ag旗舰厅登录网址下载推荐给好友。
- 上一篇:
- 下一篇: