setlocale_basic3.phpt 3.4 KB

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