1--TEST--
2Test setlocale() function : basic functionality - setting system locale to a specific
3--SKIPIF--
4<?php
5if (setlocale(LC_ALL, 'invalid') === 'invalid') { die('skip setlocale() is broken /w musl'); }
6if (substr(PHP_OS, 0, 3) == 'WIN') {
7    die('skip Not valid for windows');
8}
9if (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) {
10    die('skip available locales not usable');
11}
12?>
13--FILE--
14<?php
15/* test setlocale by specifying a specific locale as input */
16
17function list_system_locales() {
18  // start the buffering of next command to internal output buffer
19  ob_start();
20
21  // run the command 'locale -a' to fetch all locales available in the system
22  system('locale -a');
23
24  // get the contents from the internal output buffer
25  $all_locales = ob_get_contents();
26
27  // fflush and end the output buffering to internal output buffer
28  ob_end_clean();
29
30  $system_locales = explode("\n", $all_locales);
31
32  // return all the locale found in the system
33  return $system_locales;
34}
35
36/* Collect existing system locales and set one among them,
37   Check the currency settings in the new locale  */
38echo "*** Testing setlocale() : basic functionality - set to a specific locale ***\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// set the system locale to a locale, choose the right locale by
72// finding a common locale in commonly used locale stored in
73// $common_locales & locales that are available in the system, stored
74// in $all_system_locales.
75echo "Setting system locale(LC_ALL) to ";
76foreach($common_locales as $value) {
77  // check if a commonly used locale is installed in the system
78  if(in_array($value, $all_system_locales)){
79    echo "$value\n"; // print, this is found
80    // set the found locale as current locale
81    var_dump(setlocale(LC_ALL, $value ));
82    // stop here
83    break;
84  }
85  else{
86    // continue to check if next commonly locale is installed in the system
87    continue;
88  }
89}
90
91// check that new locale setting is effective
92// use localeconv() to get the details of currently set locale
93$locale_info = localeconv();
94
95//checking currency settings in the new locale to see if the setlocale() was effective
96$new_currency = trim($locale_info['int_curr_symbol']);
97echo "Checking currency settings in the new locale, expected: ".$currency_symbol[$value].", Found: ".$new_currency."\n";
98echo "Test ";
99if(trim($currency_symbol[$value]) == $new_currency){
100  echo "PASSED.";
101} else {
102  echo "FAILED.";
103}
104
105echo "\nDone\n";
106?>
107--EXPECTF--
108*** Testing setlocale() : basic functionality - set to a specific locale ***
109Setting system locale(LC_ALL) to %s
110string(%d) %s
111Checking currency settings in the new locale, expected: %s, Found: %s
112Test PASSED.
113Done
114