【问题标题】:Translate GTK UI written in Glade and Python翻译用 Glade 和 Python 编写的 GTK UI
【发布时间】:2020-06-16 07:53:22
【问题描述】:

我花了一些时间四处寻找答案,我有很多答案。从理论上讲,我的代码应该可以工作,但事实并非如此。 首先我会发布最小代码,然后我会描述问题。

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>

test.py:

#!/usr/bin/env python3
# -*- coding: utf-8 -*-

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.getlocale())
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()

zh.po:

# SOME DESCRIPTIVE TITLE.
# Copyright (C) YEAR THE PACKAGE'S COPYRIGHT HOLDER
# This file is distributed under the same license as the PACKAGE package.
# FIRST AUTHOR <EMAIL@ADDRESS>, YEAR.
#
msgid ""
msgstr ""
"Project-Id-Version: PACKAGE VERSION\n"
"Report-Msgid-Bugs-To: \n"
"POT-Creation-Date: 2020-06-15 15:18+0200\n"
"PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n"
"Last-Translator: FULL NAME <EMAIL@ADDRESS>\n"
"Language-Team: LANGUAGE <LL@li.org>\n"
"Language: \n"
"MIME-Version: 1.0\n"
"Content-Type: text/plain; charset=UTF-8\n"
"Content-Transfer-Encoding: 8bit\n"

#: test.glade:66
msgid "Document"
msgstr "Dokumentumok"

#: test.glade:30
msgid "Edit"
msgstr "Szerkesztés"

#: test.glade:18
msgid "File"
msgstr "Fájl"

#: test.glade:42
msgid "Find"
msgstr "Keresés"

#: test.glade:54
msgid "View"
msgstr "Nézet"

myapp.mo 文件位于 mo/hu_HU/LC_MESSAGES/myapp.mo

问题:

python 代码中的字符串翻译得很好(所以当我使用 set_label 时它也可以工作),但是 glade 文件中的字符串将无法工作,即使它们已成功翻译。所以问题不在于语言环境的加载。

输出:

Using locale directory: /home/daniel/Downloads/test/mo
Fájl
Szerkesztés
Keresés
Nézet
Dokumentumok

图形用户界面:

我用的是ubuntu 20.04和Gnome桌面,系统语言是目标语言(hu_HU)

【问题讨论】:

  • 至于我代替CHARSET,您应该输入一些正确的值-即。 UTF-8 - "Content-Type: text/plain; charset=UTF-8\n"。 Word CHARSET 的值不正确。

标签: python translation pygtk glade pygobject


【解决方案1】:

当我在en.po 中使用正确的编码代替单词CHARSET 时,代码对我有用 - 即。 UTF-8

"Content-Type: text/plain; charset=UTF-8\n". 


顺便说一句:

当我在控制台/终端中使用时

msgfmt -c en.po -o myapp.mo

然后它甚至显示警告

en.po: warning: Charset "CHARSET" is not a portable encoding name.
                Message conversion to user's charset might not work.

它确认CHARSET 必须更改。


它还显示其他值的警告 - PACKAGE VERSIONYEAR-MO-DA HO:MI+ZONEFULL NAME &lt;EMAIL@ADDRESS&gt;LANGUAGE - 但它们并不那么重要。

po/en.po:7: warning: header field 'Project-Id-Version' still has the initial default value
po/en.po:7: warning: header field 'PO-Revision-Date' still has the initial default value
po/en.po:7: warning: header field 'Last-Translator' still has the initial default value
po/en.po:7: warning: header field 'Language-Team' still has the initial default value
po/en.po:7: warning: header field 'Language' still has the initial default value

编辑:

似乎代码集 LC_ALL - locale.setlocale(locale.LC_ALL, ...) 但在我的系统中(基于 Ubuntu 18.04 的 Linux Mint 19.2)我也有 LANG=pl_PL.UTF-8LANGUAGE=pl_PL:pl,也许它会有所作为。但我不能在代码locale.setlocale(locale.LANG, ...)中设置它

编辑:

您确认变量LANGUAGE 是问题所在。

如果LANGUAGEhu:enhu,则.mo 文件必须位于文件夹hu 而不是hu_HU


顺便说一句:

我检查了/usr/share/locale/ 中还有文件夹hu,但是当我运行时

locale -a | grep hu

然后它显示给我

hu_HU
hu_HU.UTF-8

locale.setlocale(locale.LC_ALL, 'hu') 给了我错误,但locale.setlocale(locale.LC_ALL, 'hu_HU') 运行没有错误。

我创建了两个带有不同单词 mo/hu_HU/...mo/pl_PL/... 的文件夹,即使我使用 locale.setlocale(locale.LC_ALL, 'hu_HU') 我也会看到来自 pl_PL 的单词。但是当我删除locale.setlocale() 时,我会看到英文单词。

要查看来自hu_HU 的文字,我必须在控制台中运行它

 LANGUAGE=hu_HU python test.py

【讨论】:

  • 谢谢!这对我来说真的是一件愚蠢的事情......现在示例工作(至少它运行)但它产生与实际程序相同的问题:GUI 仍然是英文的。更新了要显示的问题。
  • 检查您在print( locale.getlocale() ) 中的内容。如果你得到en_GB,那么你必须把它放在mo/en_GB/LC_MESSAGES/myapp.mo 我得到pl_PL,我必须把它放在mo/pl_PL/LC_MESSAGES/myapp.mo
  • 我在locale.getlocale() 输出中有('hu_HU', 'UTF-8'),因为这是系统语言。并且打印输出的翻译没有问题。所以这就是我的问题:一切看起来都很完美,但仍然无法正常工作。
  • 似乎代码集 LC_ALL - locale.setlocale(locale.LC_ALL, ...) 但在我的系统中(基于 Ubuntu 18.04 的 Linux Mint 19.2)我也有 LANG=pl_PL.UTF-8LANGUAGE=pl_PL:pl,也许它会有所不同。但我无法在代码中设置它locale.setlocale(locale.LANG, ...)
  • 顺便说一句:我找到了Translating python gtk builder files,并且有信息表明Gtk.Builder() 可能需要在系统中进行一些额外的设置,而gettext.gettext() 不需要。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-05-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-12-26
相关资源
最近更新 更多