如何在GTK3下将文本域绑定到本地文件夹以获取gettext [英] How to bind a text domain to a local folder for gettext under GTK3

查看:71
本文介绍了如何在GTK3下将文本域绑定到本地文件夹以获取gettext的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用gettext,您可以使用默认的系统范围内的语言环境目录,也可以使用bindtextdomain自己指定一个.当编译的.mo转换文件在系统的默认位置不可用时,当直接从源代码运行程序时,这很有用.

With gettext you can either use the default system-wide locale directory, or specify one yourself using bindtextdomain. This is useful when running a program directly from source when the compiled .mo translation files are not available in the system's default location.

在Python中,您可以这样做:

In Python you would do this:

import gettext
from gettext import gettext as _
gettext.bindtextdomain('nautilus-image-manipulator', '/path/to/mo/folder')
gettext.textdomain('nautilus-image-manipulator')

其中/path/to/mo/folder包含熟悉的fr/LC_MESSAGES/nautilus-image-manipulator.mo结构.这样的调用:

where /path/to/mo/folder contains the familiar fr/LC_MESSAGES/nautilus-image-manipulator.mo structure. Calls like this:

print _("Delete this profile")

从本地.mo文件返回正确翻译的字符串 ,非常感谢.

return the properly translated string from the local .mo files, thank you very much.

在GTK + 2/pygtk中,存在 gtk.glade.bindtextdomain ,但我想知道GTK + 3/PyGObject中是否有任何等效项.

In GTK+2/pygtk, there existed gtk.glade.bindtextdomain, but I'm wondering if there is any equivalent in GTK+3/PyGObject.

为您提供一个具体示例,这是

To give you a specific example, this is how Nautilus Image Manipulator;s UI is created from its Glade file:

from gi.repository import Gtk
builder = Gtk.Builder()
builder.set_translation_domain('nautilus-image-manipulator')
builder.add_from_file(ui_filename)
return builder

不是从Glade文件构建的用户界面部分(即通过代码设置)可以正确翻译,但是Glade文件中的字符串仍以英语显示.

Parts of the UI that are not built from the Glade file (i.e. set from code) show up properly translated, but the strings from the Glade file still show up in English.

在我看来,在呼叫builder.set_translation_domain之前我缺少对某种builder.bind_text_domain('nautilus-image-manipulator', '/path/to/mo/folder')的呼叫...知道如何执行此操作吗?

It seems to me that I'm missing a call to some kind of builder.bind_text_domain('nautilus-image-manipulator', '/path/to/mo/folder') before the call to builder.set_translation_domain... Any idea how to perform this?

推荐答案

在PyGtk中,您也可以使用Gtk.Builder.根据PyGtk Gtk.Builder文档:

In PyGtk you can use Gtk.Builder too. Accordingly to the PyGtk Gtk.Builder documentation:

http://developer.gnome.org/pygtk /stable/class-gtkbuilder.html#properties-gtkbuilder

翻译具有以下内容的属性值时使用的翻译域 在界面说明中被标记为可翻译.如果 翻译域为None,GtkBuilder使用gettext(),否则 dgettext().默认值:无

The translation domain used when translating property values that have been marked as translatable in interface descriptions. If the translation domain is None, GtkBuilder uses gettext(), otherwise dgettext(). Default value: None

也就是说,Gtk.Builder使用"C库"中的dgettext().问题是Python的gettext模块(功能 bindtextdomain())由于某种我不知道的原因而没有设置"C库".选项是使用语言环境模块,该模块也公开该接口.从Python语言环境模块文档中:

That is, Gtk.Builder uses dgettext() from "C library". The problem is that Python's gettext module, function bindtextdomain(), for some reason unknown to me, don't set the "C library". The option is to use the locale module that also exposes that interface. From the Python locale module documentation:

http://docs.python.org/library/locale #access-to-message-catalogs

locale模块在系统上公开C库的gettext接口 提供此接口.它由功能gettext()组成, dgettext(),dcgettext(),textdomain(),bindtextdomain()和 bind_textdomain_codeset().这些类似于中的相同功能 gettext模块,但对消息使用C库的二进制格式 目录以及C库用于查找消息的搜索算法 目录.

The locale module exposes the C library’s gettext interface on systems that provide this interface. It consists of the functions gettext(), dgettext(), dcgettext(), textdomain(), bindtextdomain(), and bind_textdomain_codeset(). These are similar to the same functions in the gettext module, but use the C library’s binary format for message catalogs, and the C library’s search algorithms for locating message catalogs.

Python应用程序通常应该不需要调用它们 函数,而应改用gettext.已知的例外情况 规则是与其他C库链接的应用程序 在内部调用gettext()或dcgettext().对于这些应用,它 绑定文本域可能是必需的,以便库可以 正确找到他们的邮件目录.

Python applications should normally find no need to invoke these functions, and should use gettext instead. A known exception to this rule are applications that link with additional C libraries which internally invoke gettext() or dcgettext(). For these applications, it may be necessary to bind the text domain, so that the libraries can properly locate their message catalogs.

这是当前情况.多么可笑:S

Which, is the current case. What a hack :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):
        # Build GUI
        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'))

        # Get objects
        go = self.builder.get_object
        self.window = go('window')

        # Connect signals
        self.builder.connect_signals(self)

        # Everything is ready
        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>
  <!-- interface-requires gtk+ 3.0 -->
  <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:

Remember to create the mo in mo/LANG/LC_MESSAGES/myapp.mo based on .po extracted with:

xgettext --keyword=translatable --sort-output -o en.po test.glade

它是什么样的:

亲切的问候

这篇关于如何在GTK3下将文本域绑定到本地文件夹以获取gettext的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆