chromium 10 添加字符串资源

01 查找字符串资源

我们可以从原来的

src\chrome\app\chromium_strings.grd
src\chrome\app\generated_resources.grd

中查找字符串资源。
然后,用翻译工具计算处ID,再到翻译文件中找到对应的字符串。
比如:我们想找 Chromium这个字符串资源。
我们可以在 src\chrome\app\chromium_strings.grd 中找到两个"Chromium"字符串。
这里写图片描述

      <message name="IDS_PRODUCT_NAME" desc="The Chrome application name">
        Chromium
      </message>
      <message name="IDS_SHORT_PRODUCT_NAME" desc="The Chrome application short name.">
        Chromium
      </message>

这两个"Chromium"字符串在c++代码中分别用IDS_PRODUCT_NAME和IDS_SHORT_PRODUCT_NAME来引用。
而这两个字符串在不同语言环境中分别被表示成什么内容,需要用到翻译文件。
翻译文件一般是.xtb文件。

src\chrome\app\resourcessrc\components\strings 下面有大量的翻译文件。

*.grd 中的字符串和 *.xtb 中的对应是通过ID来的。
比如:
src\chrome\app\resources 中的 Chromium 被换成 ZdxBrowser

<translation id="7337881442233988129">ZdxBrowser</translation>

ID查找:

D:\git\chromium\src\tools\grit>python
Python 2.7.6 (default, Nov 10 2013, 19:24:18) [MSC v.1500 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> from grit.extern import tclib
>>> tclib.GenerateMessageId("Chromium")
'7337881442233988129'

需要在 用 grit 工具。src\tools\grit,运行如下命令:

python
from grit.extern import tclib
tclib.GenerateMessageId("Chromium")

最后得到ID:'7337881442233988129'
这里写图片描述

02 添加新字符串

新字符串一般添加到src\chrome\app\generated_resources.grd即可。
比如在src\chrome\app\generated_resources.grd中加入 “Mining setting”字符串。

      <message name="IDS_ZDX_MINER_TITLE" desc="A happy message saying miner mining!">
          Mining setting
      </message>

用grid工具生成ID:'5522263371877361241'

D:\git\chromium\src\tools\grit>python
Python 2.7.6 (default, Nov 10 2013, 19:24:18) [MSC v.1500 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> from grit.extern import tclib
>>> tclib.GenerateMessageId("Mining setting");
'5522263371877361241'

在翻译文件中的中文版本中加入对应中文字符串
src\chrome\app\resources\generated_resources_zh-CN.xtb
···
挖矿设置
···
在其他对应的翻译文件(*.xtb)中加入该字符串的翻译,就完成国际化了。
这样,在c++中引用IDS_ZDX_MINER_TITLE,在中文环境下就显示:挖矿设置
实际上是被grit工具解析成这样:
src\out\Debug\gen\chrome\grit\generated_resources.h(905):

#define IDS_ZDX_MINER_TITLE 4135

为了方便,可以在 src\tools\grit 下面写一个python脚本,比如src\tools\grit\gen_msg_id.py 。内容如下:

#!/usr/bin/python
# -*- coding: UTF-8 -*-

#chrome\app\resources\generated_resources_en-GB.xtb
#<translation id="8195576314346698005">&amp;Miner</translation>
# 带&amp;的字符串是 &,  [&amp;Miner ==> &Miner]
#msgs = [
#'&Miner'
#]


from grit.extern import tclib

msgs = [
'Test',
'&Miner',
]


for msg in msgs:
  print("%s  %s" %(tclib.GenerateMessageId(msg), msg))

src 目录下运行:python tools\grit\gen_msg_id.py

运行结果:

::D:\git\chromium\src>python tools\grit\gen_msg_id.py
917720651393141712  Test
8195576314346698005  &Miner

03 怎么计算带 html 标记的 字符串id值

计算如下,字符串中含有html标记的id值:

<translation id="1022489261739821355">Showing passwords from your <ph name="BEGIN_LINK" />Google Account<ph name="END_LINK" /></translation>

只要把标记部分用name值的字符串替换即可。即计算

Showing passwords from your BEGIN_LINKGoogle AccountEND_LINK
tclib.GenerateMessageId("Showing passwords from your BEGIN_LINKGoogle AccountEND_LINK");
'1022489261739821355'

04 英文重音符号 `

当字符串中带有英文重音符号时,使用 \ 处理。

<translation id="1042174272890264476">Your computer also comes with <ph name="SHORT_PRODUCT_NAME" />'s RLZ library built in. RLZ assigns a non-unique, non-personally identifiable tag to measure the searches and <ph name="SHORT_PRODUCT_NAME" /> usage driven by a particular promotional campaign. These labels sometimes appear in Google Search queries in <ph name="PRODUCT_NAME" />.</translation>
tclib.GenerateMessageId("Your computer also comes with SHORT_PRODUCT_NAME\'s RLZ library built in. RLZ assigns a non-unique, non-personally identifiable tag to measure the searches and SHORT_PRODUCT_NAME usage driven by a particular promotional campaign. These labels sometimes appear in Google Search queries in PRODUCT_NAME.");
'1042174272890264476'

05 字符串中含有换行符

<translation id="1070377999570795893">Another program on your computer added an extension that may change the way Chrome works.

<ph name="EXTENSION_NAME" /></translation>

这里再 works. 后面有两个换行符。在计算字符串 id 时,需要使用 \n 替换换行符。
如下:

Another program on your computer added an extension that may change the way Chrome works.\n\nEXTENSION_NAME

运行结果:

tclib.GenerateMessageId("Another program on your computer added an extension that may change the way Chrome works.\n\nEXTENSION_NAME");
'1070377999570795893'

06 遇到不清楚的格式怎么处理

修改自定义浏览器时,发现有些带 {} 的不知道怎么处理,猜测很久也没搞定。后来发现 Release 版本编译的时候,会有检查输出。哪些没有定义 translation id 的字符串,会被输出到标准输出。windows上面的 cmd 窗口。然后,找到对应修改的字符串,就是 tclib.GenerateMessageId() 需要的字符串。
类似于:

...
Sync will start once you leave sync settings
Manage your Google Account
Activity and interactions
Uses content on sites you visit, plus browser activity and interactions, for personalization
Manage synced data on Google Dashboard
Encrypt synced data with your own BEGIN_LINKsync passphraseEND_LINK. This doesn't include payment methods and addresses from Google Pay.
Only someone with your passphrase can read your encrypted data. The passphrase is not sent to or stored by Google. If you forget your passphrase or want to change this setting, you'll need to BEGIN_LINKreset syncEND_LINK.
To change this setting, BEGIN_LINKreset syncEND_LINK to remove your sync passphrase
To turn this on, BEGIN_LINKreset syncEND_LINK to remove your sync passphrase
If you forgot your passphrase or want to change this setting, BEGIN_LINKreset syncEND_LINK.
Control how your browsing history is used to personalize Search, ads, and more
Import bookmarks and settings
From:
Select items to import:
Loading...
...
# 这个问题找了很久,原来是空格、回车等导致不一致
{COUNT, plural, =1 {text} other {# texts}}
<translation id="1235924639474699896">{COUNT, plural, =1 {text} other {# texts}}</translation>
# 比如:这个从xtb里面拿出来的字符串,空格和编译检查时输出的字符串不一致,导致id不同
<translation id="2757139842204346305">{COUNT,plural, =1{text}other{# texts}}</translation>
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值