Reputation: 77
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:
<?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()
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 <[email protected]>\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
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. So the problem isn't with the loading of the locale.
Output :
Using locale directory: /home/daniel/Downloads/test/mo
Fájl
Szerkesztés
Keresés
Nézet
Dokumentumok
I'm using ubuntu 20.04 with Gnome desktop, and the system language is the target language (hu_HU)
Upvotes: 0
Views: 1170
Reputation: 142631
Code works for me when I use correct encoding in place of word CHARSET
in en.po
- ie. 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.
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.
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. But I can't set it in code locale.setlocale(locale.LANG, ...)
EDIT:
You confirmed that variable LANGUAGE
was the problem.
If LANGUAGE
is hu:en
or hu
then .mo
file has to be in folder hu
instead of hu_HU
BTW:
I checked I have also folder hu
in /usr/share/locale/
but when I run
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.
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
. But when I remove locale.setlocale()
then I see English words.
To see words from hu_HU
I have to run it in console as
LANGUAGE=hu_HU python test.py
Upvotes: 1