Our sample application is the following:
例 3.4. Basic sample application
<?php
if( !extension_loaded('gtk')) {
dl( 'php_gtk.' . PHP_SHLIB_SUFFIX);
}
$window = &new GtkWindow();
$window->set_default_size( 300, 200);
$window->set_title( 'window title');
$vbox = &new GtkVBox();
$label = &new GtkLabel( 'Translation test');
$button = &new GtkButton( 'Close window');
$vbox->pack_start( $label);
$vbox->pack_start( $button);
$window->connect_object('destroy', array('gtk', 'main_quit'));
$button->connect_object( 'clicked', array( 'gtk', 'main_quit'));
$window->add( $vbox);
$window->show_all();
gtk::main();
?>
|
We would like to have the "window title", "Translation test"
and "Close window".
The first thing you need to do is loading the gettext extension,
the same way you load php-gtk. This can be done by doing a
if( !extension_loaded( 'gettext')) {
if( !@dl( 'php_gettext.' . PHP_SHLIB_SUFFIX)) {
die( 'gettext is not available');
}
}
|
The next step is to set the location of the translation file;
this can be done with
bindtextdomain( 'testapp', './locale');
|
which would assume that the .mo file would be in
{CURRENT_DIR}/locale/{LANGUAGE}/LC_MESSAGES/testapp.mo.
Language could be de_DE, depending on your
system setting - this can be overridden with
putenv( "LANG=" . 'de_DE'); //required on windows, because setlocale doesn't really work
setlocale( LC_ALL, 'de_DE');
|
. You should always use the language identifier
together with the country code as there have been problems
on some systems when using only de or
DE.
The {CURRENT_DIR} assumes that the application
is started with the working directory in the application directory;
but this can't always be expected. It would be better to do a
bindtextdomain( 'testapp', dirname( __FILE __) . '/locale');
|
which uses the real location of the current file as base.
After setting the file location, you have to tell gettext
which "domain" (part of the translation files) you want to
use, in our case it would be "testapp":
This allows you to use multiple domains in the same program, and
you can switch between them everytime.
The translation of strings if an ease: There is the function
gettext which takes a string as parameter
and returns the translated version. To make the code more readable,
there is an alias of the function: _ (underscore).
Now we change our code and get the following:
例 3.5. Basic example with translation
<?php
if( !extension_loaded('gtk')) {
dl( 'php_gtk.' . PHP_SHLIB_SUFFIX);
}
putenv( "LANG=" . 'de_DE'); //required on windows, because setlocale doesn't really work
setlocale( LC_ALL, 'de_DE');
if( !function_exists( 'gettext')) {
//gettext not automatically loaded
if( !@dl( 'php_gettext.' . PHP_SHLIB_SUFFIX)) {
die( 'gettext extension is not available!');
}
}
bindtextdomain( 'testapp', dirname( __FILE__) . '/locale');
textdomain( 'testapp');
$window = &new GtkWindow();
$window->set_default_size( 300, 200);
$window->set_title( _('window title'));
$vbox = &new GtkVBox();
$label = &new GtkLabel( _('Translation test'));
$button = &new GtkButton( gettext('Close window'));
$vbox->pack_start( $label);
$vbox->pack_start( $button);
$window->connect_object('destroy', array('gtk', 'main_quit'));
$button->connect_object( 'clicked', array( 'gtk', 'main_quit'));
$window->add( $vbox);
$window->show_all();
gtk::main();
?>
|
When you start your application, you will see nothing translated:
We forgot the translation file.
Just copy the following into
locale/de/LC_MESSAGES/testapp.po:
# PHP-Gtk translation tutorial
# Copyright (C) 2004 PHP-Gtk documentation team
# Christian Weiske <cweiske@php.net>, 2004.
#
#, fuzzy
msgid ""
msgstr ""
"Project-Id-Version: 0.1N\n"
"POT-Creation-Date: 2004-09-15 07:12+0100\n"
"PO-Revision-Date: 2004-09-15 07:12+0100\n"
"Last-Translator: Christian Weiske <cweiske@php.net>\n"
"Language-Team: PHP-Gtk doc team germany <germany@example.com>\n"
"MIME-Version: 1.0\n"
"Content-Type: text/plain; charset=iso-8859-1\n"
"Content-Transfer-Encoding: 8bit\n"
#: gettext_example.php:20
msgid "window title"
msgstr "Fenstertitel"
msgid "Translation test"
msgstr "UEbersetzungstest"
msgid "Close window"
msgstr "Fenster schliessen"
|
After doing this, compile it with
msgfmt testapp.po and
move the created messages.mo
to testapp.mo.
Start the application again - you will see
the app in the german language.