1--TEST-- 2Test setlocale() function : usage variations - passing multiple valid/invalid locales as argument 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", "Ko_KR.utf8", "zh_CN.utf8") === false) { 10 die('skip en_US.utf8/Ko_KR.utf8/zh_CN.utf8 locales not available'); 11} 12?> 13--FILE-- 14<?php 15/* Test the setlocale() when multiple valid/invalid locales are provided as argument */ 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 get 2 valid locales that can be use to 37 pass as argument to setlocale(), pass 2 invalid arguments along with two valid arguments*/ 38echo "*** Testing setlocale() by passing multiple valid/invalid locales as argument ***\n"; 39 40//set of currency symbol according to above list of locales 41$currency_symbol = array( 42 "en_US.utf8" => "USD", 43 "en_AU.utf8" => "AUD", 44 "ko_KR.utf8" => "KRW", 45 "zh_CN.utf8" => "CNY", 46 "de_DE.utf8" => "EUR", 47 "es_EC.utf8" => "USD", 48 "fr_FR.utf8" => "EUR", 49 "ja_JP.utf8" => "JPY", 50 "el_GR.utf8" => "EUR", 51 "nl_NL.utf8" =>"EUR" 52); 53 54// gather all the locales installed in the system 55$all_system_locales = list_system_locales(); 56 57// Now check for three locales that is present in the system and use that as argument to setlocale() 58if( in_array("en_US.utf8",$all_system_locales) || 59 in_array("Ko_KR.utf8",$all_system_locales) || 60 in_array("zh_CN.utf8",$all_system_locales) ) { 61 echo "-- Testing setlocale() by giving 'category' as LC_ALL & multiple locales(en_US.invalid, en_US.utf8, Ko_KR.utf8, KO_KR.invalid, zh_CN.utf8) --\n"; 62 63 // call setlocale() 64 $new_locale = setlocale(LC_ALL, "en_US.invalid", "en_US.utf8", "Ko_KR.utf8", "KO_KR.invalid", "zh_CN.utf8"); 65 66 // dump the name of the new locale set by setlocale() 67 var_dump($new_locale); 68 69 // check that new locale setting is effective 70 // use localeconv() to get the details of currently set locale 71 $locale_info = localeconv(); 72 $new_currency = trim($locale_info['int_curr_symbol']); 73 74 echo "Checking currency settings in the new locale, expected: ".$currency_symbol[$new_locale].", Found: ".$new_currency."\n"; 75 echo "Test "; 76 if( trim($currency_symbol[$new_locale]) == $new_currency) { 77 echo "PASSED.\n"; 78 } else { 79 echo "FAILED.\n"; 80 } 81} 82 83echo "Done\n"; 84?> 85--EXPECTF-- 86*** Testing setlocale() by passing multiple valid/invalid locales as argument *** 87-- Testing setlocale() by giving 'category' as LC_ALL & multiple locales(en_US.invalid, en_US.utf8, Ko_KR.utf8, KO_KR.invalid, zh_CN.utf8) -- 88string(%d) "%s" 89Checking currency settings in the new locale, expected: %s, Found: %s 90Test PASSED. 91Done 92