Chinaunix首页 | 论坛 | 博客
  • 博客访问: 540680
  • 博文数量: 83
  • 博客积分: 6010
  • 博客等级: 准将
  • 技术积分: 1169
  • 用 户 组: 普通用户
  • 注册时间: 2007-04-29 22:34
文章分类

全部博文(83)

文章存档

2011年(3)

2010年(29)

2009年(30)

2008年(21)

我的朋友

分类: 系统运维

2009-09-10 00:09:19

很多FF插件都有本地化版本,这要很大一部分要归功于FF提供这样一种很方便的接口。
本文主要是http://www.softwareishard.com/blog/?p=10的学习心得。

1,添加本地化文件

helloworld@janodvarko.cz/
chrome/
content/
helloworld/
helloWorld.xul
helloWorld.js
locale/
en-US/
helloWorld.dtd
helloWorld.properties
zh-CN/
helloWorld.dtd
helloWorld.properties
defaults/
preferences/
prefs.js
chrome.manifest
install.rdf
上面添加了2个目录,一个是用于en-US语言的,一个则是用于中文显示的zh-CN,Extending Firebug, localization (part IV.)这篇文件主要列举了en-US为例,很显然,人家是e语国家的人。本文以添加中文化为例。

2,修改chrome.manifest文件

加入这么两句:
locale    helloworld    en-US        chrome/locale/en-US/
locale    helloworld    zh-CN        chrome/locale/zh-CN/

3,XUL文件的本地化

首先,本地化的字符串都放在文件helloWorld.dtd

<!ENTITY helloworld.mybutton.label "按钮标签">
<!ENTITY helloworld.mybutton.tooltip "按钮提示">

其次,修改文件helloworld.xul,加入:


...
这句话作用大概就是引进了文件helloWorld.dtd的内容。 关于DTD引进的详细信息,可以看这里。
接着,把原来的XUL文件中的字符串改用本地化字符串:

                <toolbarbutton id="hwMyButton"
                    label="&helloworld.mybutton.label;"
                    class="toolbar-text-button"
                    tooltiptext="&helloworld.mybutton.tooltip;"
                    command="cmd_hwMyButton"/>

4,Javascript文件的本地化

首先,JS文件的本地化字符串放在helloWorld.properties中:

helloworld.option1=选项1
helloworld.option2=选项2
helloworld.paneltitle=面板标签
helloworld.message=消息

其次再在helloWorld.xul文件中引进helloWorld.properties:

<overlay xmlns="">
    <script src="chrome://helloworld/content/helloworld.js" type="application/x-javascript" />
    
    <stringbundleset id="stringbundleset">
        <stringbundle id="strings_helloworld" src="chrome://helloworld/locale/helloworld.properties"/>
    </stringbundleset>

见上面stringbundleset和stringbundle的用法,引进helloWorld.properties还有另外一种方法,这里就不介绍了。
接着再修改helloworld.js文件,先是添加2个获取字符串的函数,方便字符串的处理:

function $HW_STR(name)
{
    return document.getElementById("strings_helloWorld").getString(name);
}

function $HW_STRF(name, args)
{
    return document.getElementById("strings_helloWorld")
        .getFormattedString(name, args);
}

最后一步是把原来的字符串改为本地化字符串:

HelloWorldPanel.prototype = extend(Firebug.Panel,
{
    name:    panelName,
    title:    $HW_STR("helloworld.paneltitle"),
    initialize:    function() {
        Firebug.Panel.initialize.apply(this, arguments);
    },
    getOptionsMenuItems: function(context) {
        return [
            this.optionMenu($HW_STR("helloworld.option1"), "helloworld.option1"),
            "-",
            this.optionMenu($HW_STR("helloworld.option2"), "helloworld.option2")
        ];
    },

...

5,效果

源码下载
阅读(838) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~