Source Rally PHP Community Scripts .. Sign up .. Login
This tutorial suppose you have php + apache installed from packages on an Ubuntu system!

Gettext is a sweet library that makes implementing a new language version in your PHP application real sweet – no need to mess about with hundres and thousands of definitions. Basicly everytime you have a text string that need to be translated you as following in the code:

[CODE]
<?
    echo _('Text to be translated');
?>
[/CODE]

1. Installation of gettext package
Open a console window and execute following command:
$ sudo apt-get install php-gettext

2. Install locales
To make a good translation you need to be able to use functions like strftime() to get dates displayed in the needed languages (in this cases Spanish and English), gettext needs it as well.

Go to your console window again:
Install Spanish locale: $ sudo locale-gen es_ES
Install English locale: $
sudo locale-gen en_BR

On Ubuntu you can see "all" locales in the file /usr/share/i18n/SUPPORTED


3. Setting up a simple PHP application

Now start your favourite PHP application and create a new file called helloword.php and add following code.

[CODE]
<?php
    bindtextdomain('messages', './');
    textdomain('messages');
    switch ($_GET['l']) {
        case 'es':
        setlocale(LC_ALL,'es_ES');
        break;
        case 'en':
        default:
        setlocale(LC_ALL,'en_GB');
        break;
    }
    echo _('Hello World');
?>
[/CODE]

And save the file of course.

4. Extract strings and translate
Following you need to extract the strings that need to be translated and translate them.
Go to your console window and execute following command:
$ xgettext example.php

A file called message.po will be created.
Now you need to create some folders where gettext will be looking for the language files.
$ mkdir -p en/LC_MESSAGES
$ mkdir -p es/LC_MESSAGES

You also need to move the translation file to the right destination.
$ mv messages.po en/LC_MESSAGES
$ cp en/LC_MESSAGES/messages.po es/LC_MESSAGES/messages.po

Now open es/LC_MESSAGES/message.po in your text editor, is should look like this:

[TEXT]
# 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.
#
#, fuzzy
msgid ""
msgstr ""
"Project-Id-Version: PACKAGE VERSION\n"
"Report-Msgid-Bugs-To: \n"
"POT-Creation-Date: 2007-10-26 13:40+0200\n"
"PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n"
"Last-Translator: FULL NAME <EMAIL@ADDRESS>\n"
"Language-Team: LANGUAGE <LL@li.org>\n"
"MIME-Version: 1.0\n"
"Content-Type: text/plain; charset=CHARSET\n"
"Content-Transfer-Encoding: 8bit\n"

#: example.php:15
msgid "Hello World"
msgstr ""
[/TEXT]

We need to tell which charset we are using, change the line:
"Content-Type: text/plain; charset=CHARSET\n"
to
"Content-Type: text/plain; charset=UTF-8\n"

And now for the translation, change following
[TEXT]
msgid "Hello World"
msgstr ""
[/TEXT]

to

[TEXT]
msgid "Hello World"
msgstr "Hola mundo!"
[/TEXT]

Now save the file.

Now finally you need to compile the translation, go to console again and execute following command:
$ msgfmt es/LC_MESSAGES/messages.po -o es/LC_MESSAGES/messages.mo

Now we are basicly done, all we only need to open the file in our favourite browser, open the urls:
http://localhost/{directory}/helloworld.php?l=es for the Spanish version and http://localhost/{directory}/helloworld.php?l=en for the English version.


 


Following presention was used as inspiration:
http://talks.php.net/show/i18nl10nphp/5


Sign up to add your own comment here!

All user contributed content is available under the LGPL unless specified otherwise.
Remaining copyrights Regin Gaarsmand © 2006-2008
About SourceRally.net
Programador PHP