Translating glade files

Translating the strings in a glade file is really easy: If gettext is available and loaded and the textdomain is set, it automatically looks for translations - you just have to provide the translated .mo file in the right directory.

Here is the example application, but with glade:

and here the glade file:
<?xml version="1.0"?>
<GTK-Interface>
<project>
  <name>example-tutorial-translation</name>
  <program_name>example-tutorial-translation</program_name>
  <directory></directory>
  <source_directory>src</source_directory>
  <pixmaps_directory>pixmaps</pixmaps_directory>
  <language>C</language>
  <gnome_support>False</gnome_support>
  <gettext_support>False</gettext_support>
  <output_translatable_strings>True</output_translatable_strings>
  <translatable_strings_file>example-tutorial-translation.strings</translatable_strings_file>
</project>
<widget>
  <class>GtkWindow</class>
  <name>window</name>
  <title>window title</title>
  <type>GTK_WINDOW_TOPLEVEL</type>
  <position>GTK_WIN_POS_NONE</position>
  <modal>False</modal>
  <default_width>300</default_width>
  <default_height>200</default_height>
  <allow_shrink>False</allow_shrink>
  <allow_grow>True</allow_grow>
  <auto_shrink>False</auto_shrink>

  <widget>
    <class>GtkVBox</class>
    <name>vbox</name>
    <homogeneous>False</homogeneous>
    <spacing>0</spacing>

    <widget>
      <class>GtkLabel</class>
      <name>label</name>
      <label>Translation test</label>
      <justify>GTK_JUSTIFY_CENTER</justify>
      <wrap>False</wrap>
      <xalign>0.5</xalign>
      <yalign>0.5</yalign>
      <xpad>0</xpad>
      <ypad>0</ypad>
      <child>
	<padding>0</padding>
	<expand>True</expand>
	<fill>True</fill>
      </child>
    </widget>

    <widget>
      <class>GtkButton</class>
      <name>button</name>
      <can_focus>True</can_focus>
      <label>Close window</label>
      <relief>GTK_RELIEF_NORMAL</relief>
      <child>
	<padding>0</padding>
	<expand>False</expand>
	<fill>False</fill>
      </child>
    </widget>
  </widget>
</widget>
</GTK-Interface>

Just start the script, and you will see a translated application as the string contents are already in testapp.mo.

It can be quite boring to collect all the strings which shall be translated in a .glade file. Glade has the option "save translatable strings" which creates a file containing all the strings from a glade file - it's easy to convert them to a .po file.

© Copyright 2003-2023 www.php-editors.com. The ultimate PHP Editor and PHP IDE site.