PHP Internationalization Programming
Multi-Byte, Locale, Format, and Time Zone

The following are general points of PHP internationalization.
  1. Multi-Byte Character

    For handling multi-byte characters like Japanese, you need mbstring module installed in your web server.
    The functions mb_***() provide multi-byte character handling and some functions without mb_ cannot handle correctly so you replace them with mb_***().

    The major important functions are as follows.

    === Major Multi-Byte Function Conversion === (Deprecated -> Recommended)
    
    mail() -> mb_send_mail()
    
    strlen() -> mb_strlen()
    strpos() -> mb_strpos()
    strrpos() -> mb_strrpos()
    substr() -> mb_substr()
    str_replace() -> mb_ereg_replace()
    strstr() -> mb_strstr()
    strtolower() -> mb_strtolower()
    strtoupper() -> mb_strtoupper() 
    
    ereg() -> mb_ereg() or preg_match()
    eregi() -> mb_eregi() or preg_match() with 'i'
    ereg_replace() -> mb_ereg_replace() or preg_replace()
    eregi_replace() -> mb_eregi_replace() or preg_replace() with 'i'
    split() -> mb_split() or preg_split()
    
    For all multi-byte functions, 
    Refer to PHP Manual - Multibyte String Functions
    
    For all deprecated functions, 
    Refer to PHP Manual - Deprecated features in PHP 5.3.x
    
    
    *You can use the deprecated functions as overloaded multi-byte functions by mbstring.func_overload bit flag.
    Refer to PHP Manual - Function Overloading Feature.

  2. Setting User Locale

    For applying user locale (user's language & region) to date handling and culture specific formatting (date, currency...), you need to call setlocale function.

    $locale = "en_US.UTF-8" or "ja_JP.UTF-8"... 
    These should be retrieved from user HTTP headers (Accept-Language) or saved account info.
    
    setlocale(LC_ALL, $locale);  ... For all locale sensitive functions. 
    e.g.) switching gettext resources.
    
    setlocale(LC_TIME, $locale); ... For date locale sensitive functions. 
    e.g.) switching date function format.
    
    

  3. Formatting On User Culture

    Locale sensitive functions can switch the format by setlocale function.

    setlocale(LC_TIME, "en_US.UTF-8");
    strftime('%c'); ... Returns 'Mon 01 Jan 2012...'. (English style)
    
    setlocale(LC_TIME, "ja_JP.UTF-8");
    strftime('%c'); ... Returns '2012年1月1日...'. (Japanese style)
    
    *'%c' is locale sensitive format pattern.
    
    

  4. Conversion Between Server And User Time

    For handling time in multilingual & global web services, using GMT/UTC is recommended. You can handle every user activity in common time and convert them to user timezones with date_default_timezone_set.

    About GMT/UTC handling, refer to Server Side Programming.

    $timestamp = strtotime(gmdate("Y-m-d H:i:s"));  ... GMT/UTC timestamp
    
    date_default_timezone_set("Asia/Tokyo");  ... Set Japan timezone
    strftime('%c', $timestamp); ... Return Japan local time.
    
    date_default_timezone_set("America/Los_Angeles");  ... Set USA timezone
    strftime('%c', $timestamp); ... Return USA local time.
    
    
    *User timezone should be retrived from user account info. For non login users, access IP region or JavaScript on users' web browser can tell us.
    About JavaScript timezone detection, refer to Locale, Format, and Time Zone (in JavaScript chapter).

Go to Internationalization Programming Top


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