转载地址:http://blog.sina.com.cn/s/blog_54e1b5250100rr61.html
说到国际化,首先得明白这个概念,其实说白了就是支持多种语言。chromium在Windows平台怎么做的呢?简单的将就是将每一种语言打包成一个dll,在程序启动的时候根据操作系统的语言配置、本地程序配置和程序参数指定等因素决定一个语言类型,然后加载这个对应的dll。这些dll有一个统一的接口,那就是每一个字符串有一个唯一的标识(int类型),应用可以根据这个标识来索引对应的字符串。
为了将chromium中的国际化部分剥离,可以参考下列步骤:
- 新建工程,将其类型设置为Util
- 添加需要的grd文件,同时需要拷贝匹配的xtb文件到工程目录下
- 拷贝三个第三方工具(grit、python、cygwin)
- 注意:删除svn相关的文件可以大幅度减小空间占用,这三个工具加起来一百多M
- 修改rulus配置文件(grit\build目录下)
- 注意:只需要修改相关路径即可,因此可以直接修改文件,而不用去vs2008中去修改,修改bat文件类同。
- 修改bat文件,修改的同样也是路径
- 编译工程产生”.h”文件和rc文件
- 添加”.h”文件到工程
- 新建win32 dll工程,添加对应的resource文件。然后将“No Entry Point”属性设置为true
- 注意:为了能够在不同的configuration条件(Debug/Release)下使用不同的”.h”文件,可以手动修改工程文件,将路径改成这种相对路径即可。不过貌似要重载工程才行。例如 RelativePath=”$(OutDir)\grit_derived_sources\generated_resources_en-US.rc”
GRIT – the Google Resource and Internationalization Tool |
Grit工作原理:
<?xml version=”1.0″ encoding=”UTF-8″?> <grit base_dir=”.” latest_public_release=”0″current_release=”1″ source_lang_id=”en” enc_check=”m枚l”> <outputs> <!- TODO add each of your output files. Modify the three below, and add your own for your various languages. See the user’s guide for more details. Note that all output references are relative to the output directory which is specified at build time. -> <outputfilename=”resource.h” /> <output filename=”en_resource.rc” /> <outputfilename=”fr_resource.rc” /> </outputs> <translations> <!- TODO add references to each of the XTB files (from the Translation Console) that contain translations of messages in your project. Each takes a form like <file path=”english.xtb” />. Remember that all file references are relative to this .grd file. -> </translations> <release seq=”1″><includes> <!- TODO add a list of your included resources here, e.g. BMP and GIF resources. -> </includes> <structures> <!- TODO add a list of all your structured resources here, e.g. HTML templates, menus, dialogs etc. Note that for menus, dialogs and version information resources you reference an .rc file containing them.-></structures> <messages> <!- TODO add all of your “string table” messages here. Remember to change nontranslateable parts of the messages into placeholders (using the<ph> element). You can also use the ‘grit add’ tool to help you identify nontranslateable parts and create placeholders for them. -> </messages> </release></grit>
部分段的意义
messages :该段定义若干message段和一些通用的属性
- fallback_to_english:是否使用英语如果待翻译语言不存在(true/false),默认false。如果该flag为false并且对方语言有不存在对应的翻译项,编译将报错。
message :定义一个字符串
- name:唯一标识一个字符串的int变量名(string)
- desc:该变量的介绍(string)
- translateable:是否翻译(true/false),默认true
- use_name_for_id:是否使用name作为翻译的标识符,如果yes,则翻译文件(xtb)使用name来和grd文件中的记录来映射,否则xtb需要使用一个字符串,默认是false。那这个标志主要考虑到不同的需求,如果该标志为true,不管内容如何改,xtb里面的名字不用变(实际上如果是翻译的话,内容不变估计也不行)。而如果该标志为false,则不管name怎么便,只要内容不变,xtb就可以不变。
内部格式处理
<message name=”IDS_YAYA_ADD_BUDDY_RESULT” use_name_for_id=”true”> Find <phname=”BEGIN_LINK_CHROMIUM1″>BEGIN_LINK</ph> records! </message> <translationid=”IDS_YAYA_ADD_BUDDY_RESULT”> 找到<ph name=”BEGIN_LINK_CHROMIUM1″/> 条记录</translation>
“Find BEGIN_LINK records!”
“找到BEGIN_LINK 条记录”
其他段
翻译段
<translations> <file path=”generated_resources_zh-CN.xtb” lang=”zh-CN” /></translations>
输出段
<outputs> <output filename=”grit/generated_resources.h”> <emit emit_type=’prepend’></emit> </output> <output filename=”generated_resources_en-US.rc” lang=”en” /> <outputfilename=”generated_resources_zh-CN.rc” lang=”zh-CN” /> </outputs>
翻译节
<translation id=”IDS_YAYA_LOGIN_WINDOW”> 登陆 </translation>