chromium之国际化

转载地址:http://blog.sina.com.cn/s/blog_54e1b5250100rr61.html

说到国际化,首先得明白这个概念,其实说白了就是支持多种语言。chromium在Windows平台怎么做的呢?简单的将就是将每一种语言打包成一个dll,在程序启动的时候根据操作系统的语言配置、本地程序配置和程序参数指定等因素决定一个语言类型,然后加载这个对应的dll。这些dll有一个统一的接口,那就是每一个字符串有一个唯一的标识(int类型),应用可以根据这个标识来索引对应的字符串。

        在windows上打包字符串一般都会想到使用资源里面的string table,而chromium如果想做到跨平台就显然不能直接这么用,而且直接用修改和管理都是相当麻烦的事情。于是chromium开发者弄了个python工具叫作grit(网上找不到任何文档,真是杯具), chromium就是自己定义一套字符串存储规范(grd、xtb文件,都是用xml表示),然后通过grit将其转化为特定平台的字符串存储。在windows下显然就是生成一堆rc(resource)文件。

为了将chromium中的国际化部分剥离,可以参考下列步骤:

  1. 新建工程,将其类型设置为Util
  2. 添加需要的grd文件,同时需要拷贝匹配的xtb文件到工程目录下
  3. 拷贝三个第三方工具(grit、python、cygwin)
    1. 注意:删除svn相关的文件可以大幅度减小空间占用,这三个工具加起来一百多M
  4. 修改rulus配置文件(grit\build目录下)
    1. 注意:只需要修改相关路径即可,因此可以直接修改文件,而不用去vs2008中去修改,修改bat文件类同。
  5. 修改bat文件,修改的同样也是路径
  6. 编译工程产生”.h”文件和rc文件
  7. 添加”.h”文件到工程
  8. 新建win32 dll工程,添加对应的resource文件。然后将“No Entry Point”属性设置为true
    1. 注意:为了能够在不同的configuration条件(Debug/Release)下使用不同的”.h”文件,可以手动修改工程文件,将路径改成这种相对路径即可。不过貌似要重载工程才行。例如 RelativePath=”$(OutDir)\grit_derived_sources\generated_resources_en-US.rc”

       关于grit,因为官方没有发布什么文档,所以也只好看看源码、代码里的用法和程序中的帮助来了解一二啦,运行程序打出来的帮助内容如下:

GRIT – the Google Resource and Internationalization Tool 
Copyright (c) Google Inc. 2010 
Usage: grit [GLOBALOPTIONS] TOOL [args to tool] 
Global options: 
-i INPUT  Specifies the INPUT file to use (a .grd file).  If this is not 
specified, GRIT will look for the environment variable GRIT_INPUT. 
If it is not present either, GRIT will try to find an input file 
named ‘resource.grd’ in the current working directory. 
-v        Print more verbose runtime information. 
-x        Print extremely verbose runtime information.  Implies -v 
-p FNAME  Specifies that GRIT should profile its execution and output the 
results to the file FNAME. 
Tools: 
TOOL can be one of the following: 
build        A tool that builds RC files for compilation. 
newgrd       Create a new empty .grd file. 
rc2grd       A tool for converting .rc source files to .grd files. 
transl2tc    Import existing translations in RC format into the TC 
sdiff        View differences without regard for translateable portions. 
resize       Generate a file where you can resize a given dialog. 
unit         Use this tool to run all the unit tests for GRIT. 
count        Exports all translateable messages into an XMB file. 
For more information on how to use a particular tool, and the specific 
arguments you can send to that tool, execute ‘grit help TOOL’

 

Grit工作原理:

         定义一个grd文件,在这个文件中定义输出的文件(例如.h文件和不同语言的.rc文件),指定若干翻译文件(xtb文件),同时定义一堆字符串,每一个字符串有一个name和content,当然还有其他一些属性。这个name必须是唯一可标识一个字符串,而content则表示实际在程序中显示的内容。

         为各种语言分别定义一个xtb文件,这个xtb文件中可以全部或者有选择性地针对grd文件中定义的字符串进行本地语言的翻译。为了对这些字符串进行精确的匹配,grid使用name或者根据content生成的一个字符串来关联文件中的字符串。Grd典型的格式如下:

<?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>

        其中<outputs>段进行输出定义<translations>段进行翻译定义,而<release>段则进行输入定义,其中<release>段中的<messages>段定义字符串,而其中的<includes>段是定义二进制文件,所以这里可以支持主题(theme),里面全部包含图片即可。

部分段的意义
messages :该段定义若干message段和一些通用的属性
  1. fallback_to_english:是否使用英语如果待翻译语言不存在(true/false),默认false。如果该flag为false并且对方语言有不存在对应的翻译项,编译将报错。
message :定义一个字符串
  1. name:唯一标识一个字符串的int变量名(string)
  2. desc:该变量的介绍(string)
  3. translateable:是否翻译(true/false),默认true
  4. use_name_for_id:是否使用name作为翻译的标识符,如果yes,则翻译文件(xtb)使用name来和grd文件中的记录来映射,否则xtb需要使用一个字符串,默认是false。那这个标志主要考虑到不同的需求,如果该标志为true,不管内容如何改,xtb里面的名字不用变(实际上如果是翻译的话,内容不变估计也不行)。而如果该标志为false,则不管name怎么便,只要内容不变,xtb就可以不变。
内部格式处理

         这里主要考虑到某些分段字符串,例如要定义“x时y分z秒”的字符串,如果定义三个显然不美观也没必要,但是定义一个就需要在中间插入特殊的标记,以便用户能够分割,下面的范例解释如何设置

<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 条记录”

       除了<ph>tag意外,还有<ex>tag。不过看源码里面是空的,估计这个tag目前也没啥用

其他段

 

翻译段

<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>
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值