PHP Internationalization Programming
Internationalization By gettext

PHP supports gettext message handling, so you can switch label and message strings based on user locales.

The following are basic gettext steps by World Wide Navi PHP string externalization.
  1. Put Strings To Localize In gettext Function

    Put strings that you want to localize in gettext function (in most cases, '_()').
    These strings get localized (translated) by each external resource file.

    echo "Hello from PHP!"; -> echo _("Hello from PHP!");
    

  2. Write Initalizing Code

    Apply user locale (language & region) and tell the resource name and path to gettext mechanism.

    index.php (or other php)
    
    <?php require_once($_SERVER["DOCUMENT_ROOT"].'/gettext/wwnaviRs/' . 'wwnavi.gettext.php');?>
    // Running initializing code for locale setting and gettext reading.
    ...
    echo _("Hello from PHP!");
    ...
    
    wwnavi.gettext.php // Initializing code.
    
    require_once('wwnaviLang.php');
    // Language and region arrays used later.
    
    $header = $_SERVER['HTTP_ACCEPT_LANGUAGE'];
    // Getting user HTTP header with language info.
    
    $lang='';
    // Parse user locale from HTTP headers.
    // Locale expression are different from each web browser('en', 'en-us', 'en_US'...),
    // but setlocale parameters need to be 'language_REGION'.
    if(strpos($header, ',')>0) {
        $hds = explode(',', $header);
        $lang = $hds[0];
        $lang = str_replace('-', '_', $lang);
        if(strpos($lang, '_')>0) {
            $ls = explode('_', $lang);        
            if(count($ls)>1) $lang = $ls[0] . '_' . strtoupper($ls[1]);        
        }else if(!empty($wwnaviLang[$lang])) {
            $lang .= '_' . $wwnaviLang[$lang]; 
            // If language code (e.g. 'en') only, add region code (e.g. 'US') 
            // from pre-defined arrays because setlocale needs full locale name.
        }
    }
    //echo $lang . '
    '; //putenv("LC_ALL=$lang"); setlocale(LC_ALL, $lang); // $lang need to be language and region name (e.g. "en_US") at least. $domain = 'wwnaviBundle'; // Telling gettext resource name and path. bindtextdomain($domain, dirname(__FILE__)); textdomain($domain); bind_textdomain_codeset($domain, 'UTF-8');
    wwnaviLang.php // Pre-defined arrays of language and default region code.
    
    $wwnaviLang = array(
    ...
    'de'=>'DE',
    'en'=>'US',
    'es'=>'ES',
    'et'=>'EE',
    'eu'=>'ES',
    'fa'=>'IR',
    'fi'=>'FI',
    'fo'=>'FO',
    'fr'=>'FR',
    ...
    'ja'=>'JP',
    ...
    

  3. Create gettext Master Template Resource (.POT)

    You need to create a master resource (.pot) with pairs of msgid and msgstr under the resource path.
    xgettext command is useful which scans '_()' strings and create pot files.

    YOUR_DOCUMENT_ROOT/gettext/wwnaviRs/(*1)
     ... wwnaviBundle.pot(*1)
    
    *1)The same directory as specified in the code above.
    
    wwnaviBundle.pot
    ...
    
    #, fuzzy
    msgid ""
    msgstr ""
    "Project-Id-Version: PACKAGE VERSION\n"
    "Report-Msgid-Bugs-To: \n"
    "POT-Creation-Date: 2012-03-05 17:32+0900\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=UTF-8\n"
    "Content-Transfer-Encoding: 8bit\n"
    
    #: /...../examples/php/gettext/index.php:10
    msgid "Hello from PHP!"
    msgstr ""
    
    #: /...../examples/php/gettext/index.php:13
    msgid "This is a DIV text."
    msgstr ""
    ...
    
    #: /root/wwnavi/workspace/examples/php/gettext/sub/sub2/sub2.php:31
    #, php-format
    msgid "My local time is %c!"
    msgstr ""
    
    
    xgettext -j -L php  -k"_" --from-code=UTF-8 -o /..../examples/php/gettext/wwnaviRs/wwnaviBundle.pot  \ 
       /..../examples/php/gettext/index.php
    
    

  4. Localize .POT Into Each Language Resource (.PO)

    Copy the master template to the target locale directory (LOCALE(e.g. 'ja_JP')/LC_MESSAGES) as a .po file and translate msgstr.

    Case of Japanese Translation
    
    YOUR_DOCUMENT_ROOT/gettext/wwnaviRs/ja_JP/LC_MESSAGES
     ... wwnaviBundle.po
    
    
    ja_JP/LC_MESSAGES/wwnaviBundle.po
    ...
    
    #, fuzzy
    msgid ""
    msgstr ""
    "Project-Id-Version: PACKAGE VERSION\n"
    "Report-Msgid-Bugs-To: \n"
    "POT-Creation-Date: 2012-03-05 17:32+0900\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=UTF-8\n"
    "Content-Transfer-Encoding: 8bit\n"
    
    #: /...../examples/php/gettext/index.php:10
    msgid "Hello from PHP!"
    msgstr "PHPからこんにちは!"
    
    #: /...../examples/php/gettext/index.php:13
    msgid "This is a DIV text."
    msgstr "これはDIVのテキストです。"
    ...
    
    #: /root/wwnavi/workspace/examples/php/gettext/sub/sub2/sub2.php:31
    #, php-format
    msgid "My local time is %c!"
    msgstr "わたしの現地時間は%cです!"
    
    
    ***CAUTION: Do not modify(translate) format string like '%c'.

  5. Compile .PO Into .MO (Resource Binary)

    Run msgfmt command to create a resource binary (.mo) in the target locale directory.

    msgfmt -o wwnaviBundle.mo wwnaviBundle.po
    
    
    Now you can switch your messages between English and Japanese changing your browser language settings.

These process can be checked with PHP string externalization samples in World Wide Navi.

POT/PO localization process gets more easy and effective with our localization tool, Sisulizer.

Go to Internationalization Programming Top


Copyright (C) 2012 Kokusaika JP, Inc.
All rights reserved. No reproduction or republication without written permission.