pygtk+glade+localization
Asked Answered
O

3

7

Default locale in my system (Linux) - en_US. When I change locale to russian (LANG=ru_RU ./show_form.py), python string "hello to me" shows correctly (in console), other (glade form) - in english. (LANG=ru_RU.utf-8 ./show_form.py - same result).

What am I doing wrong?

I have simple python code for example (show_form.py):

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

import pygtk, gtk, gtk.glade
import locale, gettext

APP="show_form"
DIR="locale"

locale.setlocale(locale.LC_ALL, '')
gettext.bindtextdomain(APP, DIR)
gettext.textdomain(APP)
lang = gettext.translation(APP, DIR)
_ = lang.gettext
gettext.install(APP, unicode=False, codeset='utf-8', localedir=DIR)
print _("hello to me")

wTree = gtk.glade.XML("localize.glade")
gtk.glade.bindtextdomain(APP, DIR)

window = wTree.get_widget("window1")
window.connect("delete_event", lambda wid, we: gtk.main_quit())
window.show_all()
gtk.main()

and .mo files for some laguages. .po files below:

# Russian translations for PACKAGE package.                                     
# Copyright (C) 2012 THE PACKAGE'S COPYRIGHT HOLDER                             
# This file is distributed under the same license as the PACKAGE package.       
#  <asc@grodno>, 2012.                                                          
#
msgid ""
msgstr ""
"Project-Id-Version: PACKAGE VERSION\n"
"Report-Msgid-Bugs-To: \n"
"POT-Creation-Date: 2012-11-14 13:54+0300\n"
"PO-Revision-Date: 2012-11-14 13:58+0300\n"
"Last-Translator:  <aaa@bbb>\n"
"Language-Team: Russian\n"
"Language: ru\n"
"MIME-Version: 1.0\n"
"Content-Type: text/plain; charset=utf-8\n"
"Content-Transfer-Encoding: 16bit\n"
"Plural-Forms: nplurals=3; plural=(n%10==1 && n%100!=11 ? 0 : n%10>=2 && n"
"%10<=4 && (n%100<10 || n%100>=20) ? 1 : 2);\n"

#: show_form.py:14
msgid "hello to me"
msgstr "привет мне"

#: localize.glade.h:1
msgid "label text"
msgstr "метка"

#: localize.glade.h:2
msgid "button text"
msgstr "кнопка"

#: localize.glade.h:3
msgid "checkbutton text"
msgstr "галочка"

and en_US file (like this with different msgstr). And small glade form with label, button, checkbox.

Upd:

My .mo files creation strings:

intltool-extract --type=gettext/glade localize.glade

xgettext --language=Python --keyword=_ --keyword=N_ --output=show_form.pot show_form.py localize.glade.h

msginit --locale=ru --input=show_form.pot
msginit --locale=en_US --input=show_form.pot
msginit --locale=de_DE --input=show_form.pot

I correct .po files after this.

And at the end:

msgfmt ru.po -o locale/ru/LC_MESSAGES/show_form.mo
msgfmt en_US.po -o locale/en_US/LC_MESSAGES/show_form.mo
msgfmt de.po -o locale/de/LC_MESSAGES/show_form.mo

Upd2: Solution

-wTree = gtk.glade.XML("localize.glade")
 gtk.glade.bindtextdomain(APP, DIR)
+wTree = gtk.glade.XML("localize.glade", "window1", APP)
Ophthalmia answered 15/11, 2012 at 10:20 Comment(0)
A
1

You need to use intltool to translate your Glade files.

Allix answered 15/11, 2012 at 14:50 Comment(4)
Tomorrow I`ll add my .mo files creation commands to section "Upd:"Ophthalmia
As you may see I use intltool but this is not help.Ophthalmia
You also have to intltool-merge the translations back into the glade file, I believe.Allix
intltool-merge isn`t necessary (it crates the same glade form without any changes). wTree = gtk.glade.XML("localize.glade") ---> wTree = gtk.glade.XML("localize.glade", "window1", APP) and gtk.glade.bindtextdomain(APP, DIR) before it, is solving problem.Ophthalmia
H
0

That works nice on gtk2, however if you are using GTK3, things get messy...

Take a look at:

Using locale to bind glade i18n

The actual title is different but it does the trick. I still have problems with stock-buttons, but the rest of my app began to be i18ned

Haught answered 23/5, 2020 at 21:34 Comment(0)
G
0

From this. It seems that your python code get wrong order.

lang = gettext.translation(APP, DIR)
lang.install(APP, unicode=False, codeset='utf-8', localedir=DIR)
_ = lang.gettext
Glisten answered 6/9, 2021 at 14:40 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.