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