Internationalisation and Unicode
Removed
setlocale()
String Category Support
A so-called locale defines language and regional settings, for example date formats, or how monetary values are displayed.
Locales are somewhat system-dependent, so there are several intricacies when dealing with locales. The naming scheme, for example, is not consistent across systems, plus you can never really rely on a given locale to be available on a system.
Nevertheless, locale support helps us overcome many
internationalization issues. Using the function
setlocale()
, you cannot only set a locale, but also
query the system for the currently active locale by passing
0
as the second parameter:
var_dump(setlocale(LC_CTYPE, 0));
Depending on the locale active on your system, the output might look something like this:
string(11) "en_US.UTF-8"
When setting locale information, you need to pass two parameters
to setlocale()
, a category and a locale. In older
versions of PHP, you could pass a string as the category, since PHP
7 you must pass an integer. There are several constants that you can
use to make your code more readable, namely LC_ALL
,
LC_COLLATE
, LC_CTYPE
,
LC_MONETARY
, LC_NUMERIC
,
LC_TIME
, and LC_MESSAGES
which is only
available when libintl
was compiled into PHP.
Deprecated
Aliases Removed from intl
Extension
When using the intl
extension, before PHP 7, you
could use the method IntlDateFormatter::setTimeZoneId
and its procedural sibling datefmt_set_timezone_id()
to
set a timezone in a formatter by passing a string as identifier.
Those methods, already deprecated in PHP 5.5, have been removed in PHP 7. Instead of setting a timezone using an identifier, you must now use a different method and pass a timezone object:
$formatter->setTimeZone(new DateTimeZone('Europe/Berlin'));
recode
Extension
Unbundled
The recode
extension allows character set
conversions, just like mbstring
and iconv
do. Since recode
is based on a library that has been
unmaintained for almost 20 years, this extension has now been
unbundled from PHP.
Usually, unbundling an extension from PHP means that the source
code is still available on PECL, and the extension can still be
downloaded, compiled and installed separately. However, at the time
of writing of this chapter, no recode
extension source
code was available at https://pecl.php.net/package/recode.
If your application still uses the recode
extension,
you should switch to iconv
.