PHP Internationalization and Localization
Determining the User’s Locale
Problem
You want to use the correct locale as specified by a user’s web browser.
Solution
Pass the incoming Accept-Language HTTP header value to the Locale::accept FromHttp() function to get the proper locale identifier:
if (isset($_SERVER['HTTP_ACCEPT_LANGUAGE'])) {
$localeToUse = Locale::acceptFromHttp($_SERVER['HTTP_ACCEPT_LANGUAGE']);
}
else {
$localeToUse = Locale::getDefault();
}
Discussion
Section 14.4 of RFC 2616, which specifies how HTTP works, provides rules for how web browsers can send an Accept-Language header with a request to indicate what languages are preferred as a response to the request. A web browser may send more than one possible language with values indicating the relative preference for those languages. The Locale::acceptFromHttp() function sorts through those values and returns an ICU locale ID corresponding to the desired language.
If no preferred language is specified, you can use the locale returned by the Locale::get Default() function, whose value comes from the intl.default_locale configuration directive (if not overridden by a call to Locale::setDefault()).
No comments:
Post a Comment