Python-Ref > GUI programming with PyGTK > Localization using gettext > Localizing with GtkBuilder XML files
 
 

<-^^
Klíčová slova
Moduly
Knihovní funkce

Localizing with GtkBuilder XML files

The standard tool for extraction of translatable messages from source files - xgettext does not seem to be fully prepared for the GtkBuilder format. Even though it extracts some messages, many are omitted. Also the loading of a GtkBuilder file does not seem to honour current gettext localization properly.
To get around both these problems, there is a little free library that was developed as part of a free program that can help with this. The program can be found here, but I will copy the library here to make it simple to access.
To use the library for message translation, you simply run it from command line. It will print out all translatable messages in a python format. Redirecting this output to a file gives you a temporary python file from which you can the extract the messages in a normal way. The following code shows how:
python xml_translator.py example.ui > tmp.py
xgettext tmp.py -d my_example
mkdir -p cs/LC_MESSAGES
mv my_example.mo cs/LC_MESSAGES
To load the GtkBuilder file into your application fully localized, you use the following method.
Expand/Shrink
# 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: 2010-04-16 09:14+0200\n"
"PO-Revision-Date: 2010-04-16 09:17+0200\n"
"Last-Translator: Beda Kosata <EMAIL@ADDRESS>\n"
"Language-Team: LANGUAGE <LL@li.org>\n"
"MIME-Version: 1.0\n"
"Content-Type: text/plain; charset=utf-8\n"
"Content-Transfer-Encoding: 8bit\n"

#: _tmp.py:1
msgid "Hello Glade"
msgstr "Ahoj Glade"

#: _tmp.py:2
msgid "Forbidden button"
msgstr "Zakázaný čudlík"

#: _tmp.py:3
msgid "Press me"
msgstr "Zmáčkni mě"
Argumenty příkazové řádky:
LC_ALL=cs python localization5-1.py 
  1   import gettext
  2   from xml_translator import translate_xml
  3   
  4   # last argument is True - we need gettext to return unicode
  5   gettext.install( "hello_builder", "./", True)
  6   # we obtain a new XML localized using current language
  7   new_xml = translate_xml("infiles/hello_builder.ui", _)
  8   # we than use the XML string to load it to GtkBuilder
  9   #; builder.add_from_string(new_xml)
 10   # here we just print it out to show that it is localized
 11   print new_xml
stdout:
<interface>
  <requires version="2.16" lib="gtk+"></requires>
  
  <object class="GtkWindow" id="window1">
    <property name="title" translatable="yes">Ahoj Glade</property>
    <child>
      <object class="GtkVBox" id="vbox1">
        <property name="visible">True</property>
        <property name="orientation">vertical</property>
        <child>
          <object class="GtkLabel" id="label1">
            <property name="visible">True</property>
            <property name="xpad">10</property>
            <property name="ypad">10</property>
            <property name="label" translatable="yes">Ahoj Glade</property>
          </object>
          <packing>
            <property name="position">0</property>
          </packing>
        </child>
        <child>
          <object class="GtkHBox" id="hbox1">
            <property name="visible">True</property>
            <property name="homogeneous">True</property>
            <child>
              <object class="GtkButton" id="button1">
                <property name="label" translatable="yes">Zmáčkni mě</property>
                <property name="visible">True</property>
                <property name="can_focus">True</property>
                <property name="receives_default">True</property>
                <signal handler="on_button1_clicked" name="clicked"></signal>
              </object>
              <packing>
                <property name="position">0</property>
              </packing>
            </child>
            <child>
              <object class="GtkButton" id="button2">
                <property name="label" translatable="yes">Zakázaný čudlík</property>
                <property name="visible">True</property>
                <property name="can_focus">True</property>
                <property name="receives_default">True</property>
                <signal handler="on_button2_clicked" name="clicked"></signal>
              </object>
              <packing>
                <property name="position">1</property>
              </packing>
            </child>
          </object>
          <packing>
            <property name="expand">False</property>
            <property name="position">1</property>
          </packing>
        </child>
      </object>
    </child>
  </object>
</interface>
Doba běhu: 55.4 ms