setlocale_basic2.phpt 4.1 KB

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