note 113764 added to function.setlocale
| From: | RobQuist | Date: | Thu, 28 Nov 2013 14:51:46 +0000 |
| Subject: | note 113764 added to function.setlocale | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-197650@lists.php.net to get a copy of this message | ||
In addition to russ, about getting / backing up the locale:
I'm using this in unit-tests. I wanted to test something based on locale, and reset the locale
after the tests were done.
Yet there were some errors;
* setlocale doesn't like strings anymore. You need to use constants.
* Some contants don't exist anymore.
Here's an updated piece of code:
<?php
$originalLocales = explode(";", setlocale(LC_ALL, 0));
setlocale(LC_ALL, 'nl_NL.UTF-8');
//Do something here
//Recover to the default setting
$skipConstants = array( //these will be returned by setlocale(LC_ALL, 0), but don't
exist anymore.
'LC_PAPER',
'LC_NAME',
'LC_ADDRESS',
'LC_TELEPHONE',
'LC_MEASUREMENT',
'LC_IDENTIFICATION'
);
foreach ($originalLocales as $localeSetting) {
if (strpos($localeSetting, "=") !== false) {
list ($category, $locale) = explode("=", $localeSetting);
} else {
$category = LC_ALL;
$locale = $localeSetting;
}
if (!in_array($category, $skipConstants)) {
setlocale(constant($category), $locale); //Using strings is deprecated.
}
}
?>
----
Server IP: 72.52.91.14
Probable Submitter: 72.52.91.14
----
Manual Page -- http://php.net/manual/en/function.setlocale.php
Edit -- https://master.php.net/note/edit/113764
Del: integrated -- https://master.php.net/note/delete/113764/integrated
Del: useless -- https://master.php.net/note/delete/113764/useless
Del: bad code -- https://master.php.net/note/delete/113764/bad+code
Del: spam -- https://master.php.net/note/delete/113764/spam
Del: non-english -- https://master.php.net/note/delete/113764/non-english
Del: in docs -- https://master.php.net/note/delete/113764/in+docs
Del: other reasons-- https://master.php.net/note/delete/113764
Reject -- https://master.php.net/note/reject/113764
Search -- https://master.php.net/manage/user-notes.php