(PHP 4, PHP 5)

gettextИщет сообщение в текущем домене


string gettext ( string $message )

Ищет сообщение в текущем домене.

Список параметров


Переводимое сообщение.

Возвращаемые значения

Возвращает переведённую строку (string), если она найдена в таблице перевода, иначе - переданное сообщение.


Пример #1 gettext()-check

// Устанавливаем русский язык

// Указываем путь к таблицам переводов

// Выбираем домен

// Теперь поиск переводов будет идти в ./locale/ru_RU/LC_MESSAGES/

// Выводим тестовое сообщение
echo gettext("Welcome to My PHP Application");

// Или с использованием псевдонима _()
echo _("Have a nice day");



Можно использовать символ подчёркивания '_' в качестве псевдонима этой функции.


На некоторых системах может быть недостаточно указания языка, в таких случаях используйте putenv() для указания текущей локали.

Смотрите также

  • setlocale() - Устанавливает настройки локали


Depending on the implementation of gettext used you might have to call the setlocale(LC_ALL, "") command. 
So your example code would be 


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

// set the locale into the instance of gettext

// 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".
2001-03-04 22:18:59
There's a good tutorial to the GetText tools used with PHP at
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. 

("LC_ALL=da_DK"); // For danish/Denmark

// ./locale/da/LC_MESSAGES holds the file

gettext("Hello world!"));

I had a lot of trouble getting this to work on Red Hat (Yellow Dog) Linux though.
2002-05-05 08:27:03
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.
2003-05-20 10:23:48
Take care when extracting the strings from the source files : if your source files are not encoded in ascii, then xgettext must be used with the --from-code option, and the generated .po file is *always* UTF-8 (even if you used a different --from-code charset). 

The usage of gettext will not work later on strings which include non ascii caracters. For make it working, you have to translate the .po file to your proper charset with msgconv.

Example :
my source files are encoded in iso-8859-1
$ xgettext --from-code=iso-8859-1 -n *.php -o myapp.po
==> myapp.po is in UTF-8 (and generated .mo files will not work with gettext). 
I have to convert it to iso-8859-1 before translating :
$ msgconv --to-code=iso-8859-1 myapp.po -o myapp.po
...and now translate the file.
2005-08-30 10:05:56
Gettext translations are cached. If you change *.mo files your page may not be translated as expected. Here's simple workaround which does not require restarting webserver (I know, this is just a dirty hack):

function initialize_i18n($locale) {
$domains glob($locales_root.'/'.$locale.'/LC_MESSAGES/messages-*.mo');
$current basename($domains[0],'.mo');
$timestamp preg_replace('{messages-}i','',$current);

to make this work you have to put your locale inside file messages-[unix_time].mo and use this name (without .mo) as your domain to fool caching mechanism (domain names differ)

msgfmt messages.po -o messages-`date +%s`.mo

for me this works fine (although this is not very elegant solution)
2005-10-31 03:38:23
Default behavior is name .mo file equally in every language version:

--- en_US

--- sk_SK

I think, better form is:

--- en_US

--- sk_SK

Then the following code works very well (surprisingly on win32 too), and you don't need restart apache and do other confusing things:

'sk'// change by language
setlocale(LC_ALL'sk_SK.UTF-8'); // change by language, directory name sk_SK, not sk_SK.UTF-8

Have nice day :-)
2005-12-10 06:35:45
If like me, you are stuck with making a lot of code localizable, you have to go through all your php files and wrap all srings in _("string"). Here's an elisp function which can help you out. 

This function enables you to highlight some text in an emacs buffer and make it a localizable string using the keyboard shortcut C-l (Ctrl and l). If the first character highlighted is " or ', then it assumes the text is in php-context and changes it to: _(HIGHLIGHTED_TEXT). Otherwise it assumes the text is in html-context and changes it to <?=_('HIGHLIGHTED_TEXT')?> 

The shortcut C-k can be used for translating parts of php strings which contain html tabs. We dont want to translate the entire string including the tabs, so we highlight just the substring that needs to be translated and use C-k. 

To use it, do either of:
Copy and paste the following code into your .emacs file. This would permanently associate the keyboard shortcut C-l with this function. 
Save the code in a new file ending with the .el extension. Evaluate it using M-x eval-buffer. This makes the C-l keyboard shortcut only last for the current Emacs session. 

;author: Vinay Kuruvila March 01 2006
;updated to handle php strings containing html tabs 

;makes the text starting at left and ending at right in the
;current buffer a localizable string, assuming that the 
;string is within php context
(defun make-localizable-string-in-php-context(left right)
(goto-char left)
(insert "_(")
(goto-char (+ right 2))
(insert ")")

;makes the text starting at left and ending at right in the
;current buffer a localizable string, assuming that the 
;string is within html context
(defun make-localizable-string-in-html-context(left right)
(goto-char left)
(insert "<?= _('")
(goto-char (+ right 7))
(insert "'

;makes the highlighted text a localizable string
;uses php-context localization if the first char highlighted 
;is " or '
;otherwise uses html-context localization
(defun make-localizable-string()

;find the positions of the left and right ends of 
;the highlighted text
(if (> (point) (mark)) 
(setq right (point)) 
(setq left (mark))

(setq right (mark)) 
(setq left (point))

;determine php-context or html-context and dispatch
(if (or (char-equal (char-after left) ?\") (char-equal (char-after left) ?'))
(make-localizable-string-in-php-context left right)
(make-localizable-string-in-html-context left right)

;to handle php strings which contain html tabs
;we dont want to translate the html tabs
(defun make-localizable-string-within-php-string ()

;find the positions of the left and right ends of 
;the highlighted text
(if (> (point) (mark)) 
(setq right (point)) 
(setq left (mark))

(setq right (mark)) 
(setq left (point))
(goto-char left)
(insert "\". _(\"")
(goto-char (+ right 6))
(insert "\").\"") 

;assigns a keyboard shortcut
(global-set-key "\C-l" 'make-localizable-string)
(global-set-key "\C-k" 'make-localizable-string-within-php-string)
2006-03-03 16:55:19
I had a problem like "adino at adino dot sk" said, so I did the unsetting of LANG ($ unset LANG), but it was not sufficient, I had to unset the environment variable LANGUAGE too.

I tried to do this using "sudo", but it didn't works, so I had to change to root using "su"

If you need to know, I use Musix (a sub-distro of Devia).

I hope this will help you, Seeya! ^__^'
2007-04-24 17:06:59
As of php 5.3, you can use the following code to get the preferred locale of the http agent.

2010-03-04 02:26:22
For me it is sufficient to call setlocale() with a string like "nl_BE" as the second parameter, to make gettext() work. Just plain "nl" was not enough.

Ditto when using an environment variable like LANG: "en", "fr", "nl", "de" are not enough: I have to specify the country, too.
2011-01-06 08:47:03
gettext returns the headers from .mo files
if the message parameter is set to empty.

So if you are for example using Smarty blocks, make sure that the values given checks if the text has content or else your text will have a bunch of headers printing.

If you are putting a variable to the gettext, like so:
you are better of making another function like this:
function __($text){
$text)) return "";
2012-05-09 13:57:46
The simplest way to by-pass gettext() cache, without restart apache nor change domain.

The fix is incredible simple, first create a dummy link to the locale folder where .mo files stored:

cd locale
ln -s . nocache

Then add one single line before bindtextdomain()


Now the cache is forced to flush every time.
2012-11-29 18:34:42
I just wanted to say that gettext won't work on WAMP Server 2.4 / 64 Bit, see the thread I posted here:

Title: Gettext doesn't work on WAMP 64 Bits,120770,120770#msg-120770

I haven't tested with only apache 64 Bit, so, I don't know if the issue is related to apache or WAMP. Anyway, to make it work with WAMP, install the 32 Bit version of WAMP and only do this:

define('LOCALE_DIR', '<root_dir_of_your_po_files>'); //ie: C:/wamp/www/your_app/locale
$locale = '<your_locale>'; //ie: es_CO
$domain = 'your_gettext_domain'; //ie: messages
putenv('LC_ALL=' . $locale);
bindtextdomain($domain, LOCALE_DIR);
echo _('<your_string>'; // ie: Hello world

"setlocale" nor setting the LANG, LANGUAGE, and LC_MESSAGES environment variables seems to be necessary under windows. I got it working by setting "LC_ALL" only.
2014-01-07 12:23:02
Worth noting is that gettext honors environment variables while selecting the language to use:

"When a program looks up locale dependent values, it does this according to the following environment variables, in priority order:

    LC_xxx, according to selected locale category: LC_CTYPE, LC_NUMERIC, LC_TIME, LC_COLLATE, LC_MONETARY, LC_MESSAGES, ...

Variables whose value is set but is empty are ignored in this lookup. "

In short, if you have non-empty LANGUAGE, you may end up with unexpected localization strings. On the other hand, LANGUAGE can be used to define fallback language if some translation is missing.
2014-01-08 08:17:54
This function is not able to handle null values. It can return the complete translation file if you enter null as argument.
2014-02-05 17:43:02
on OSX (10.9.3) and PHP (5.4.24) you need to use full local name including codeset

i.e. for German need to use de_DE.UTF-8 even setlocale returns success when used without .UTF-8 the lookups will not work.
2014-06-26 14:16:35
If your PO/MO files make use of msgctxt (Context), you'll be frustrated to find that gettext(msgid) won't work as you might expect (even if msgid is unique).

In this case, you must use ASCII char 4 [EOT, End Of Text] glued between the msgctxt and msgid.

Example PO content:

    msgctxt "Context"
    msgid "Test string"
    msgstr "Test translation"

    msgid "Standard string"
    msgstr "Standard translation"

The following will work:

('Context' "\004" 'Test string');
gettext('Standard string');

The following will NOT work:

('Test string');
2018-07-03 02:13:09
putenv(...) can cause hidden problems when upgrading or moving between systems which are difficult to diagnose.

On one Linux server we had the following working perfectly with setlocale


We switched servers and found gettext wouldn't work despite having all the same locale files and settings

The recommendation to switch to


didn't fix the problem.



did. So if you have problems, check all three settings.
2019-01-11 11:39:30
If you come across a situation where the translation works fine on CLI but not on Apache, it may be caused by the Perl Apache module.

Basic translate.php:

// Set locale
$res setlocale(LC_ALL'de_CH.UTF-8''de_CH''de');

$results gettext("My English Text");
if (
$results === "My English Text") {
"Original English was returned. Something wrong\n";
} else {

On the CLI the translation works:

$ php7.3 translate.php
Mein deutscher Text.

But not via Apache web server:

$ curl localhost/translate.php
Original English was returned. Something wrong

Disable Perl module and restart Apache:

# a2dismod perl
# systemctl restart apache2

And suddenly the translation works:

$ curl localhost/translate.php
Mein deutscher Text.

The exact reason for this behaviour is (as of right now) unknown to me.
2020-11-16 14:57:04

    Поддержать сайт на родительском проекте КГБ