在PyGtk中,您也可以使用Gtk.Builder。根据PyGtk Gtk.Builder文档的说明:
http://developer.gnome.org/pygtk/stable/class-gtkbuilder.html#properties-gtkbuilder
当翻译接口描述中标记为可翻译的属性值时,使用的翻译域。如果翻译域为None,则GtkBuilder使用gettext(),否则使用dgettext()。默认值:None。
换句话说,Gtk.Builder使用来自"C库"的dgettext()。问题在于,Python的gettext模块函数bindtextdomain()出于我不知道的某种原因不设置"C库"。解决方法是使用locale模块,该模块还公开了该接口。从Python locale模块文档中可以看到:
http://docs.python.org/library/locale#access-to-message-catalogs
模块locale在支持的系统上提供了C库的gettext接口。它由函数gettext()、dgettext()、dcgettext()、textdomain()、bindtextdomain()和bind_textdomain_codeset()组成。这些函数与gettext模块中的相同函数类似,但使用C库的消息目录二进制格式和C库的搜索算法来定位消息目录。
Python应用程序通常不需要调用这些函数,而应该使用gettext。这个规则的一个已知例外是链接了其他C库的应用程序,这些库内部调用gettext()或dcgettext()。对于这些应用程序,可能需要绑定文本域,以便库可以正确地定位其消息目录。
这就是当前的情况。好恶心啊:S
这将完成它,文件test.py:
from gi.repository import Gtk
from os.path import abspath, dirname, join, realpath
import gettext
import locale
APP = 'myapp'
WHERE_AM_I = abspath(dirname(realpath(__file__)))
LOCALE_DIR = join(WHERE_AM_I, 'mo')
locale.setlocale(locale.LC_ALL, '')
locale.bindtextdomain(APP, LOCALE_DIR)
gettext.bindtextdomain(APP, LOCALE_DIR)
gettext.textdomain(APP)
_ = gettext.gettext
print('Using locale directory: {}'.format(LOCALE_DIR))
class MyApp(object):
def __init__(self):
self.builder = Gtk.Builder()
self.glade_file = join(WHERE_AM_I, 'test.glade')
self.builder.set_translation_domain(APP)
self.builder.add_from_file(self.glade_file)
print(_('File'))
print(_('Edit'))
print(_('Find'))
print(_('View'))
print(_('Document'))
go = self.builder.get_object
self.window = go('window')
self.builder.connect_signals(self)
self.window.show()
def main_quit(self, widget):
Gtk.main_quit()
if __name__ == '__main__':
gui = MyApp()
Gtk.main()
我的 Glade 文件 test.glade:
<?xml version="1.0" encoding="UTF-8"?>
<interface>
<object class="GtkWindow" id="window">
<property name="can_focus">False</property>
<property name="window_position">center-always</property>
<property name="default_width">400</property>
<signal name="destroy" handler="main_quit" swapped="no"/>
<child>
<object class="GtkBox" id="box1">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="orientation">vertical</property>
<child>
<object class="GtkLabel" id="label1">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="label" translatable="yes">File</property>
</object>
<packing>
<property name="expand">False</property>
<property name="fill">True</property>
<property name="position">0</property>
</packing>
</child>
<child>
<object class="GtkLabel" id="label2">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="label" translatable="yes">Edit</property>
</object>
<packing>
<property name="expand">False</property>
<property name="fill">True</property>
<property name="position">1</property>
</packing>
</child>
<child>
<object class="GtkLabel" id="label3">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="label" translatable="yes">Find</property>
</object>
<packing>
<property name="expand">False</property>
<property name="fill">True</property>
<property name="position">2</property>
</packing>
</child>
<child>
<object class="GtkLabel" id="label4">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="label" translatable="yes">View</property>
</object>
<packing>
<property name="expand">False</property>
<property name="fill">True</property>
<property name="position">3</property>
</packing>
</child>
<child>
<object class="GtkLabel" id="label5">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="label" translatable="yes">Document</property>
</object>
<packing>
<property name="expand">False</property>
<property name="fill">True</property>
<property name="position">4</property>
</packing>
</child>
</object>
</child>
</object>
</interface>
记得根据使用以下命令提取的 .po 文件,在
mo/LANG/LC_MESSAGES/myapp.mo 中创建 mo 文件:
xgettext --keyword=translatable --sort-output -o en.po test.glade
它的外观是什么:
![enter image description here](https://istack.dev59.com/AbhE6.webp)
敬祝好运
gettext.bindtextdomain()
吗? - ptomato