setlocale_basic1.phpt 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. --TEST--
  2. Test setlocale() function : basic functionality - setting system locale to a specific
  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 ,
  17. * or FALSE if locale functionality is not implemented in this platform.
  18. * Source code: ext/standard/string.c
  19. */
  20. /* test setlocale by specifying a specific locale as input */
  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 set one among them,
  40. Check the currency settings in the new locale */
  41. echo "*** Testing setlocale() : basic functionality - set to a specific locale ***\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. // set the system locale to a locale, choose the right locale by
  71. // finding a common locale in commonly used locale stored in
  72. // $common_locales & locales that are available in the system, stored
  73. // in $all_system_locales.
  74. echo "Setting system locale(LC_ALL) to ";
  75. foreach($common_locales as $value) {
  76. // check if a commonly used locale is installed in the system
  77. if(in_array($value, $all_system_locales)){
  78. echo "$value\n"; // print, this is found
  79. // set the found locale as current locale
  80. var_dump(setlocale(LC_ALL, $value ));
  81. // stop here
  82. break;
  83. }
  84. else{
  85. // continue to check if next commonly locale is installed in the system
  86. continue;
  87. }
  88. }
  89. // check that new locale setting is effective
  90. // use localeconv() to get the details of currently set locale
  91. $locale_info = localeconv();
  92. //checking currency settings in the new locale to see if the setlocale() was effective
  93. $new_currency = trim($locale_info['int_curr_symbol']);
  94. echo "Checking currency settings in the new locale, expected: ".$currency_symbol[$value].", Found: ".$new_currency."\n";
  95. echo "Test ";
  96. if(trim($currency_symbol[$value]) == $new_currency){
  97. echo "PASSED.";
  98. } else {
  99. echo "FAILED.";
  100. }
  101. echo "\nDone\n";
  102. ?>
  103. --EXPECTF--
  104. *** Testing setlocale() : basic functionality - set to a specific locale ***
  105. Setting system locale(LC_ALL) to %s
  106. string(%d) %s
  107. Checking currency settings in the new locale, expected: %s, Found: %s
  108. Test PASSED.
  109. Done