search for in the  
<dngettextngettext>
Last updated: Thu, 19 May 2005

gettext

(PHP 3 >= 3.0.7, PHP 4, PHP 5)

gettext -- Lookup a message in the current domain

Description

string gettext ( string message )

This function returns a translated string if one is found in the translation table, or the submitted message if not found. You may use the underscore character '_' as an alias to this function.

Example 1. gettext()-check

<?php
// Set language to German
setlocale(LC_ALL, 'de_DE');

// Specify location of translation tables
bindtextdomain("myPHPApp", "./locale");

// Choose domain
textdomain("myPHPApp");

// Translation is looking for in ./locale/de_DE/LC_MESSAGES/myPHPApp.mo now

// Print a test message
echo gettext("Welcome to My PHP Application");

// Or use the alias _() for gettext()
echo _("Have a nice day");
?>

See also setlocale().



User Contributed Notes
gettext
niels at monarch dot de
16-Mar-2005 07:17
mike-php at emerge2 dot com:

If you don't restart the webserver when messing with the .mo-files can cause in-traceable errors and can let the webserver become very unstable.

You alway want to restart your apache if you copy, move, create or modify your .mo-files! This is very important.
mike-php at emerge2 dot com
28-Feb-2005 03:45
Re: adino at adino dot sk's note about re-starting Apache before it will recognize new .mo files.

I have found that for Red Hat, (as well as Windows NT and Windows 2000 under IIS,) this is not the case. I change .mo files all the time, and haven't had to restart the web server yet.
inode bei gmx punkt de
27-Aug-2004 04:46
I use Debian and i don't know if it is obvious, but i noticed that the locale used by setenv() must also be installed in the System.

That means the .mo-Files in the ./locale Directory are not sufficient.
me at junglerat dot org
16-Jul-2003 01:24
I experienced a problem with gettext(). After restarting apache it stopped translating. I found that setting both LC_ALL, LANG and LANGUAGE solved my problem with gettext() on a standard installation of Mandrake 9.0.

Example:
<?php
....
setlocale(LC_ALL,'en_US:en');
putenv('LANG=en_US:en');
putenv('LANGUAGE=en_US:en');
...
?>
adino at adino dot sk
20-May-2003 09:23
If you 're experiencing problems like gettext() is not working and you're getting translated text only occassionaly use: unset LANG before starting apache.
Next thing is that you have to restart apache after you 've changed .mo files because they're treated something like shared libraries.
I've only tested this with Linux (Sourcemage Linux distro, Mandrake) but it might be true for others as well.
hardy at acm dot org
18-Mar-2003 11:12
After many hours of tests/debug I found this behaviour in Red Hat 7.3 (I don't  test with other versions/distros)

The i18n settings doesn't work if not exits the right country code under 
/usr/lib/locale/. On my original instalation I just haved /usr/lib/locale/en* files, so after reinstall the glibc-common package the example showed here it works !.

* To force reinstall of the package

     rpm -i --force glibc-common-2.2.5-34.i386.rpm

* The working example $HOME/i18n.php

----begin----
#!/usr/bin/php
<?php
// Current locale settings
echo "Current i18n:".setlocale(LC_ALL, 0)."\n\n";

// i18n support information here
$language = 'es_ES';
$newLocale=setlocale (LC_ALL, $language);
echo
"After i18n:$newLocale\n\n";

// Set the text domain as 'messages'
$domain = 'messages';
bindtextdomain($domain, "./locale");
textdomain($domain);

echo
gettext("The string must be here\n");
?>
----end----

* My $HOME/locale/es_ES/LC_MESSAGES/messages.po

----begin----
# 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"
"POT-Creation-Date: 2003-03-18 10:52+0400\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=ISO-8859-1\n"
"Content-Transfer-Encoding: 8bit\n"

#: i18n.php:16
msgid "The string must be here\n"
msgstr "La cadena debe ir aquí\n"
----end----

I hope this helps to you

Thanks
Hardy Beltran Monasterios
jespersaNOSPAM at diku dot NO_SPAM dot dk
05-May-2002 07:27
There's a good tutorial to the GetText tools used with PHP at http://zez.org/article/articleview/42
The only modification I needed to do was to use the correct ISO-language/country-codes (don't know the ISO number) and call setlocale.
helloworld.php:

putenv("LC_ALL=da_DK"); // For danish/Denmark
setlocale(LC_ALL, "");

// ./locale/da/LC_MESSAGES holds the helloworld.mo file
bindtextdomain("helloworld", "./locale");
textdomain("helloworld");

print(gettext("Hello world!"));

I had a lot of trouble getting this to work on Red Hat (Yellow Dog) Linux though.
nemo at o-k-j dot de
18-Dec-2001 04:32
If you have problem to get this working, like everybody,
try it with the long version of the language parameter:

putenv ("LANG=de_DE");
iguy at ionsphere dot org
04-Mar-2001 10:18
Depending on the implementation of gettext used you might have to call the setlocale(LC_ALL, "") command. 
So your example code would be
<br>

                                 <?php
                                
// Set language to German
                                
putenv ("LANG=de");

            
// set the locale into the instance of gettext
          
setlocale(LC_ALL, "");

                                
// Specify location of translation tables
                                
bindtextdomain ("myPHPApp", "./locale");

                                
// Choose domain
                                
textdomain ("myPHPApp");

                                
// Print a test message
                                
print (gettext ("Welcome to My PHP Application"));
                                
?>

NOTE:  If setlocale returns NULL the LANG specified is invalid and "not supported".

<dngettextngettext>
 Last updated: Thu, 19 May 2005
Copyright © 2001-2005 The PHP Group
All rights reserved.
This unofficial mirror is operated at: The Server Pages
Last updated: Thu May 19 17:35:34 2005 CDT