2

I am trying to setup internationalization in PHP on a CentOS server running PHP 7.1

Here is my directory structure:

/home/project/public_html/locale/japanese/LC_MESSAGES/messages.po 
/home/project/public_html/locale/japanese/LC_MESSAGES/messages.mo
/home/project/public_html/index.php

the messages.po containst the line (among others):

"Language: japanese\n"
"Content-Type: text/plain; charset=UTF-8\n"

I have the following code:

$check_putenv = putenv("LC_ALL=japanese");
if (!$check_putenv) {
    echo "Warning: putenv LC_ALL failed!\n";
}

$check_putenv2 = putenv("LANGUAGE=japanese");
if (!$check_putenv2) {
    echo "Warning: putenv LANGUAGE failed!\n";
}    

$check_locale = setlocale(LC_MESSAGES, 'japanese');
if (!$check_locale) {
    echo "Warning: Failed to set locale japanese!\n";
}
$check_bind = bindtextdomain("messages", "/home/project/public_html/locale");
if (!$check_bind) {
    echo "Warning: Failed to bind text domain!\n";
}
$check_textdomain = textdomain("messages");
if ($check_textdomain !== "messages") {
    echo "Warning: Failed to set text domain!\n";
}

the output is

Warning: Failed to bind text domain!

locale -a returns (among others)

ja_JP
ja_JP.utf8
japanese

any idea what could be wrong?

uncovery
  • 794
  • 5
  • 19

1 Answers1

3

As discussed in the comments, the gettext extension relies on the standard locale specifiers containing the language and region code, i.e. ja_JP for "Japanese in Japan" or with the specified encoding ja_JP.utf-8. Even if there is an alias like japanese, the PHP implementation of gettext does not accept this. Note that the locale has to be installed and configured on your system.

Language and region specifiers can be found in IANA language-subtag-registry

This code already should work for the Japanese language:

$dir     = $_SERVER['DOCUMENT_ROOT'] . '/locale';
$domain  = 'messages';
$locale  = 'ja_JP.utf8';
$codeset = 'UTF-8';

setlocale( LC_MESSAGES, $locale);
bindtextdomain($domain, $dir);
textdomain($domain);
bind_textdomain_codeset($domain, $codeset);

Remember to also rename your directory into locale/ja_JP.utf8. Ensure your .po files are stored with the correct encoding, i.e. UTF-8 in this example, and contain the line

"Content-Type: text/plain; charset=UTF-8\n"

(as you already have done).

Pinke Helga
  • 6,378
  • 2
  • 22
  • 42