1. 程式人生 > >chromium之國際化

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>