10/12/2012

multi-language setting gettext for php

The following code creates multiple language site based on gettext from PHP.

Language:
English and Japanese
English:  Hello
Japanese: こんにちは

Step 1:  Get supported language from client

Method 1, auto-detect
<?php
$language = substr($_SERVER['HTTP_ACCEPT_LANGUAGE'], 0, 2);
if ($language == 'ja') {
    $language = 'ja_JP';
}else{
    $language = 'en_US';
}
?>

Method 2, $_GET-detect
<?php
$language = $_REQUEST ['lan'];
if ($language == 'ja') {
    $language = 'ja_JP';
}else{
    $language = 'en_US';
}
?>


Step 2:  Set locale and default domain
<?php
putenv("LANG=$language");
setlocale(LC_ALL, $language);
$domain = 'domain_file_name';
bindtextdomain($domain, "/var/www/html/locale");
textdomain($domain);
?>

Step 3:  Output
Method 1, 
<?php
echo _('Hello');
?>
Method 1,
<?php
echo gettext('Hello');
?>

Now you may save all codes above with one php file with any name, here I use index.php, which looks like
 ------------------index.php--------------------------------
 
<?php
$language = $_REQUEST ['lan'];
if ($language == 'ja') {
    $language = 'ja_JP';
}else{
    $language = 'en_US';
}

putenv("LANG=$language");
setlocale(LC_ALL, $language);
$domain = 'domain_file_name';
bindtextdomain($domain, "/var/www/html/locale");
textdomain($domain);

echo _('Hello');
?>
 -------------------------------------------

Step 4:   Create po file based on $domain setting, the command looks like

xgettext -d domain_file_name index.php --from-code=utf-8

After executing the command, you may get one new file, whose name is 'domain_file_name.po'. Open it
-------------------------domain_file_name.po------------------------------
# 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 , YEAR.
#
#, fuzzy
msgid ""
msgstr ""
"Project-Id-Version: PACKAGE VERSION\n"
"Report-Msgid-Bugs-To: \n"
"POT-Creation-Date: 2012-10-12 14:41+0900\n"
"PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n"
"Last-Translator: FULL NAME \n"
"Language-Team: LANGUAGE \n"
"MIME-Version: 1.0\n"
"Content-Type: text/plain; charset=CHARSET\n"
"Content-Transfer-Encoding: 8bit\n"

#: index.php:16
msgid "Hello"
msgstr ""


---------------------------------------------------------

Then you may change 
1.  charset=CHARSET  -> charset=utf-8
2. msgstr "" -> msgstr="こんにちは"

------------------------------------------------------------


Step 5:   Create mo file based on domain_file_name.po, the command looks like
 msgfmt domain_file_name.po 

Now you have created one file, whose name is  domain_file_name.mo

Step 6:  Move domain_file_name.mo to special directory like


locale
       /en_US
                 /LC_MESSAGES
                                          / domain_file_name.mo
      /ja_JP
                 /LC_MESSAGES
                                          / domain_file_name.mo


Now you may access the index.php from your browser by 

index.php?lan=ja_JP or index.php

and you may get different message. 










No comments:

Post a Comment