简体   繁体   English

翻译用 Glade 和 Python 编写的 GTK UI

[英]Translate GTK UI written in Glade and Python

I've spent some time looking around for answers, and I've got a ton of them.我花了一些时间四处寻找答案,我有很多答案。 And theoretically my code should work, however it doesn't.从理论上讲,我的代码应该可以工作,但事实并非如此。 At first I'll post the minimal code, after that I'll describe the problems.首先我会发布最小的代码,然后我会描述问题。

test.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>

test.py:测试.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()

en.po: en.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"

And the myapp.mo file is in mo/hu_HU/LC_MESSAGES/myapp.mo myapp.mo 文件位于mo/hu_HU/LC_MESSAGES/myapp.mo

Problem:问题:

The strings in the python code are translated perfectly (so when I use set_label it works also), but the strings in the glade file won't work, even though they're translated successfully. python 代码中的字符串被完美地翻译(所以当我使用 set_label 时它也可以工作),但是 glade 文件中的字符串不起作用,即使它们被成功翻译。 So the problem isn't with the loading of the locale.所以问题不在于语言环境的加载。

Output: Output:

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

GUI:图形用户界面: 在此处输入图像描述

I'm using ubuntu 20.04 with Gnome desktop, and the system language is the target language (hu_HU)我用的是 ubuntu 20.04 和 Gnome 桌面,系统语言是目标语言(hu_HU)

Code works for me when I use correct encoding in place of word CHARSET in en.po - ie.当我在en.po中使用正确的编码代替单词CHARSET时,代码对我有用 - 即。 UTF-8

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

在此处输入图像描述


BTW:顺便提一句:

When I use in console/terminal当我在控制台/终端中使用时

msgfmt -c en.po -o myapp.mo

then it even shows warning然后它甚至显示警告

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

and it confirms that CHARSET has to be changed.它确认CHARSET必须更改。


It shows also warnings for other values - PACKAGE VERSION , YEAR-MO-DA HO:MI+ZONE , FULL NAME <EMAIL@ADDRESS> , LANGUAGE - but they are not so important.它还显示其他值的警告 - PACKAGE VERSION , YEAR-MO-DA HO:MI+ZONE , FULL NAME <EMAIL@ADDRESS> , 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

EDIT:编辑:

it seems code sets LC_ALL - locale.setlocale(locale.LC_ALL, ...) but in my system (Linux Mint 19.2 based on Ubuntu 18.04) I have also LANG=pl_PL.UTF-8 , LANGUAGE=pl_PL:pl and maybe it makes difference.似乎代码集LC_ALL - locale.setlocale(locale.LC_ALL, ...)但在我的系统中(基于 Ubuntu 18.04 的 Linux Mint 19.2)我也有LANG=pl_PL.UTF-8并且可能有所不同, LANGUAGE=pl_PL:pl . But I can't set it in code locale.setlocale(locale.LANG, ...)但我不能在代码中设置它locale.setlocale(locale.LANG, ...)

EDIT:编辑:

You confirmed that variable LANGUAGE was the problem.您确认变量LANGUAGE是问题所在。

If LANGUAGE is hu:en or hu then .mo file has to be in folder hu instead of hu_HU如果LANGUAGEhu:enhu.mo文件必须在文件夹hu而不是hu_HU


BTW:顺便提一句:

I checked I have also folder hu in /usr/share/locale/ but when I run我检查了我在/usr/share/locale/中也有文件夹hu但是当我运行时

locale -a | grep hu

then it shows me然后它告诉我

hu_HU
hu_HU.UTF-8

And locale.setlocale(locale.LC_ALL, 'hu') gives me error but locale.setlocale(locale.LC_ALL, 'hu_HU') runs without error.并且locale.setlocale(locale.LC_ALL, 'hu')给了我错误,但是locale.setlocale(locale.LC_ALL, 'hu_HU')运行没有错误。

I created two folders with different words mo/hu_HU/... and mo/pl_PL/... and even if I use locale.setlocale(locale.LC_ALL, 'hu_HU') I see words from pl_PL .我创建了两个具有不同单词mo/hu_HU/...mo/pl_PL/...的文件夹,即使我使用locale.setlocale(locale.LC_ALL, 'hu_HU')我也会看到来自pl_PL的单词。 But when I remove locale.setlocale() then I see English words.但是当我删除locale.setlocale()时,我会看到英文单词。

To see words from hu_HU I have to run it in console as要查看来自hu_HU的话,我必须在控制台中运行它

 LANGUAGE=hu_HU python test.py

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM