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