随着gettext你要么可以使用默认的系统范围内的区域设置目录,或使用指定一个自己bindtextdomain.当编译的.mo转换文件在系统的默认位置不可用时,直接从源运行程序时这很有用.
在Python中你会这样做:
import gettext
from gettext import gettext as _
gettext.bindtextdomain('nautilus-image-manipulator', '/path/to/mo/folder')
gettext.textdomain('nautilus-image-manipulator')
Run Code Online (Sandbox Code Playgroud)
其中/path/to/mo/folder包含熟悉的fr/LC_MESSAGES/nautilus-image-manipulator.mo结构.这样的电话:
print _("Delete this profile")
Run Code Online (Sandbox Code Playgroud)
从本地.mo文件返回正确翻译的字符串,非常感谢.
在GTK + 2/pygtk中,存在gtk.glade.bindtextdomain,但我想知道GTK + 3/PyGObject中是否有任何等价物.
为了给你一个具体的例子,这是Nautilus Image Manipulator的UI是如何从它的Glade文件创建的:
from gi.repository import Gtk
builder = Gtk.Builder()
builder.set_translation_domain('nautilus-image-manipulator')
builder.add_from_file(ui_filename)
return builder
Run Code Online (Sandbox Code Playgroud)
不是从Glade文件构建的UI部分(即从代码中设置)显示正确翻译,但Glade文件中的字符串仍显示为英文.
在我看来,在打电话builder.bind_text_domain('nautilus-image-manipulator', '/path/to/mo/folder')之前我错过了某种呼叫builder.set_translation_domain......任何想法如何执行此操作?