setlocale_basic2.phpt 3.6 KB

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