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