1--TEST-- 2Test setlocale() function : basic functionality - setting system locale to a specific 3--SKIPIF-- 4<?php 5if (substr(PHP_OS, 0, 3) == 'WIN') { 6 die('skip Not valid for windows'); 7} 8if (setlocale(LC_ALL, "en_US.utf8", "en_AU.utf8", "ko_KR.utf8", "zh_CN.utf8", "de_DE.utf8", "es_EC.utf8", "fr_FR.utf8", "ja_JP.utf8", "el_GR.utf8", "nl_NL.utf8") === false) { 9 die('skip available locales not usable'); 10} 11?> 12--FILE-- 13<?php 14/* test setlocale by specifying a specific locale as input */ 15 16function list_system_locales() { 17 // start the buffering of next command to internal output buffer 18 ob_start(); 19 20 // run the command 'locale -a' to fetch all locales available in the system 21 system('locale -a'); 22 23 // get the contents from the internal output buffer 24 $all_locales = ob_get_contents(); 25 26 // fflush and end the output buffering to internal output buffer 27 ob_end_clean(); 28 29 $system_locales = explode("\n", $all_locales); 30 31 // return all the locale found in the system 32 return $system_locales; 33} 34 35/* Collect existing system locales and set one among them, 36 Check the currency settings in the new locale */ 37echo "*** Testing setlocale() : basic functionality - set to a specific locale ***\n"; 38 39//set of locales to be used 40$common_locales = array( 41 "english_US"=> "en_US.utf8", 42 "english_AU" => "en_AU.utf8", 43 "korean_KR" => "ko_KR.utf8", 44 "Chinese_zh" => "zh_CN.utf8", 45 "germen_DE" => "de_DE.utf8", 46 "spanish_es" => "es_EC.utf8", 47 "french_FR" => "fr_FR.utf8", 48 "japanees_JP" => "ja_JP.utf8", 49 "greek_GR" => "el_GR.utf8", 50 "dutch_NL" => "nl_NL.utf8" 51); 52 53//set of currency symbol according to above list of locales 54$currency_symbol = array( 55 "en_US.utf8" => "USD", 56 "en_AU.utf8" => "AUD", 57 "ko_KR.utf8" => "KRW", 58 "zh_CN.utf8" => "CNY", 59 "de_DE.utf8" => "EUR", 60 "es_EC.utf8" => "USD", 61 "fr_FR.utf8" => "EUR", 62 "ja_JP.utf8" => "JPY", 63 "el_GR.utf8" => "EUR", 64 "nl_NL.utf8" =>"EUR" 65); 66 67// gather all the locales installed in the system 68$all_system_locales = list_system_locales(); 69 70// set the system locale to a locale, choose the right locale by 71// finding a common locale in commonly used locale stored in 72// $common_locales & locales that are available in the system, stored 73// in $all_system_locales. 74echo "Setting system locale(LC_ALL) to "; 75foreach($common_locales as $value) { 76 // check if a commonly used locale is installed in the system 77 if(in_array($value, $all_system_locales)){ 78 echo "$value\n"; // print, this is found 79 // set the found locale as current locale 80 var_dump(setlocale(LC_ALL, $value )); 81 // stop here 82 break; 83 } 84 else{ 85 // continue to check if next commonly locale is installed in the system 86 continue; 87 } 88} 89 90// check that new locale setting is effective 91// use localeconv() to get the details of currently set locale 92$locale_info = localeconv(); 93 94//checking currency settings in the new locale to see if the setlocale() was effective 95$new_currency = trim($locale_info['int_curr_symbol']); 96echo "Checking currency settings in the new locale, expected: ".$currency_symbol[$value].", Found: ".$new_currency."\n"; 97echo "Test "; 98if(trim($currency_symbol[$value]) == $new_currency){ 99 echo "PASSED."; 100} else { 101 echo "FAILED."; 102} 103 104echo "\nDone\n"; 105?> 106--EXPECTF-- 107*** Testing setlocale() : basic functionality - set to a specific locale *** 108Setting system locale(LC_ALL) to %s 109string(%d) %s 110Checking currency settings in the new locale, expected: %s, Found: %s 111Test PASSED. 112Done 113