Annotation of embedaddon/php/ext/standard/tests/strings/setlocale_basic2.phpt, revision 1.1.1.2

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

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>