PHP Locale for Indonesia Language

Posted in PHP on November 22nd, 2008 – Tags: Be the first to comment

To format a local time/date according to locale settings, like month and weekday names and other language dependent strings, we can use strftime() function respect the current locale set with setlocale().

Example on Linux

On Linux, you have to use xx_XX format for locale country code

setlocale(LC_ALL, 'id_ID');
echo strftime("Today in Indonesia is %A") . "\n";

//And output in UTF-8 charset is easy
setlocale(LC_ALL, 'id_ID.UTF8');
echo strftime("Today in Indonesia is %A") . "\n";

//And will output "March"
//in German language correctly: "März"
setlocale(LC_ALL, 'de_DE.UTF8');
echo strftime("This month in German is %B") . "\n";

Example on Windows

On Windows you have to use XXX format for locale country code. If it’s does not work, try xx_XX format.

setlocale(LC_ALL, 'IND');
echo strftime("Today in Indonesia is %A") . "\n";

//To output in UTF-8...
//And to output "March" in German language correctly ("März")
setlocale(LC_ALL, 'DEU');
$str1 = strftime("This month in German is %B");
$str2 = iconv('ISO-8859-1', 'UTF-8', $str1);

echo $str1 . "\n"; //not correct
echo $str2 . "\n"; //correct

Workaround: for Linux, Windows and All Platform

If we develop our PHP code on Windows workstation, it’s really pain if we have to change locale format everytime we upload it to Linux or Nix server–as post production machine. For example, on FreeBSD machine some user report that they have to use the charset to get desired result, ie: setlocale(LC_ALL, ‘fr_FR.8859-1′) to display correct French language.

Fortunately, we can put as many parameters for setlocale() function to solve our problem.

Syntax:

setlocale(category, param-1 [, param-2, ... param-n])

PHP will use the first available parameter provided by the machine.
For example we can use both xx_XX and XXX format as second and third parameter when use setlocale:

setlocale(LC_ALL, 'id_ID', 'IND', 'Indonesian', 'Indonesia');

It’s safe to add English as default language, in case Indonesian language not available in the machine:

setlocale(LC_ALL, 'id_ID', 'IND', 'Indonesian', 'Indonesia', 'en_US', 'ENG', 'American', 'English');

Or you can add a charset as desired after the locale parameter. Example: xx_XXX.UTF-8 or XXX.8859-1 (iso 8859-1).

setlocale(LC_ALL, 'id_ID.UTF8', 'id_ID.UTF-8', 'id_ID.8859-1', 'id_ID', 'ind_IND.UTF8', 'ind_IND.UTF-8', 'ind_IND.8859-1', 'ind_IND', 'IND.UTF8', 'IND.UTF-8', 'IND.8859-1', 'IND', 'Indonesian.UTF8', 'Indonesian.UTF-8', 'Indonesian.8859-1', 'Indonesian', 'Indonesia', 'id', 'ID', 'en_US.UTF8', 'en_US.UTF-8', 'en_US.8859-1', 'en_US', 'American', 'English');

Links


Leave a Reply