Gettext使用--cinnamon applet图标提示汉化

来源:互联网 发布:hse矩阵培训 编辑:程序博客网 时间:2024/05/05 09:58

网上关于Gjs的内容少,对Gjs程序的本地化就更少了,最后还是得跟官方文档和帮助死磕QAQ

最初右下角图标提示为view print task,测试组报了bug需要汉化,一开始想当然,直接将代码this.set_applet_tooltip(("view print task"));改成了this.set_applet_tooltip(("显示打印任务"));Alt+F2重启cinnamon,结果乱码了QAQ,后来在这里看到了这么一段话:

We use gettext to translate GNOME applications into all the supported languages. The gettext function should be aliased globally as _.

const _ = imports.gettext.gettext;

Use ‘single quotes’ for programming strings that should not be translated and “double quotes” for strings that the user may see. This allows us to quickly find untranslated or mistranslated strings by grepping through the sources for double quotes without a gettext call around them. ”

意识到它使用了gettext包来翻译程序源码中使用的英文,我找到了这两篇教程,它们都是对C程序使用的:

1. 使用gettext本地化编程
2. 在Linux下开发多语言软件(gettext解决方案)

这两篇教程给了我很大的帮助,使我对gettext相关的配置过程和使用方法有了进一步的认识,虽然右下角applet程序使用的语言是Gjs,但还是抱着死马当活马医的态度依葫芦画瓢尝试了一下,结果–果然不行,还是英文的QAQ
最后看了通过man dgettext 和man bindtextdomain,还下载了gettext源码包,查看了其中的javascript测试程序,了解了更多函数调用和一些设置的含义,最后终于显示出来了TAT。现总结如下:
1.创建.po文件

//test.po(e.g)#files/usr/share/cinnamon/applets/menu@cinnamon.org/applet.js:1156msgid "Run"msgstr "运行"

2.生成.mo文件(指定路径)

msgfmt test.po -o /xxx/xxx/xxx/test.mo

(这里的mo文件的路径很重要)
3.添加代码

const Gettext = imports.gettext;function _(str) {    Gettext.textdomain ("printassistant@cdos");    Gettext.bindtextdomain ("printassistant@cdos", "/usr/share/cinnamon/locale");    return Gettext.gettext(str);}this.set_applet_tooltip(_("Show print tasks"));

4.Alt+F2查看效果

注:
在这里我使用了bindtextdomain方法,在翻译前重新设置了mo文件的名称和路径,这里的路径和生成mo文件时指定的路径一致。关于bindtextmain方法可通过man bindtextman命令查看具体内容(gettext同)。

=======2016/01/11更新
按照以上方法做了以后,有一个极大的bug就是系统其他使用gettext的程序(比如托盘中的其他程序,状态栏右键菜单等)汉化全部失效了,原因可能是它设置了所有翻译程序的mo文件路径(?),这里第3步需要修改如下:

function _(str) {    return Gettext.dgettext("printassistant@cdos", str);}

没错只要调用这一个函数dgettext,然后把生成的printassistant@cdos.mo文件copy到/usr/share/locale/zh_CN/LC_MESSAGES/目录(mo文件默认存放路径)下,Alt+F2重启,就可以啦~

0 0