Locale::acceptFromHttp

locale_accept_from_http

(PHP 5 >= 5.3.0, PHP 7, PHP 8, PECL intl >= 1.0.0)

Locale::acceptFromHttp -- locale_accept_from_http β€” ΠŸΠΎΠΏΡ‹Ρ‚Π°Ρ‚ΡŒΡΡ ΠΎΠΏΡ€Π΅Π΄Π΅Π»ΠΈΡ‚ΡŒ Π½Π°ΠΈΠ»ΡƒΡ‡ΡˆΡƒΡŽ локаль ΠΎΡΠ½ΠΎΠ²Ρ‹Π²Π°ΡΡΡŒ Π½Π° Π·Π°Π³ΠΎΠ»ΠΎΠ²ΠΊΠ΅ HTTP "Accept-Language"

ОписаниС

ΠžΠ±ΡŠΠ΅ΠΊΡ‚Π½ΠΎ-ΠΎΡ€ΠΈΠ΅Π½Ρ‚ΠΈΡ€ΠΎΠ²Π°Π½Π½Ρ‹ΠΉ ΡΡ‚ΠΈΠ»ΡŒ

public static function Locale::acceptFromHttp(string $header): string|false

ΠŸΡ€ΠΎΡ†Π΅Π΄ΡƒΡ€Π½Ρ‹ΠΉ ΡΡ‚ΠΈΠ»ΡŒ

function locale_accept_from_http(string $header): string|false

ΠŸΡ‹Ρ‚Π°Π΅Ρ‚ΡΡ ΠΎΠΏΡ€Π΅Π΄Π΅Π»ΠΈΡ‚ΡŒ локаль, ΡƒΠ΄ΠΎΠ²Π»Π΅Ρ‚Π²ΠΎΡ€ΡΡŽΡ‰ΡƒΡŽ списку языков, Π·Π°ΠΏΡ€ΠΎΡˆΠ΅Π½Π½ΠΎΠΌΡƒ Π² HTTP-Π·Π°Π³ΠΎΠ»ΠΎΠ²ΠΊΠ΅ "Accept-Language".

Бписок ΠΏΠ°Ρ€Π°ΠΌΠ΅Ρ‚Ρ€ΠΎΠ²

header

Π‘Ρ‚Ρ€ΠΎΠΊΠ°, содСрТащая Π·Π°Π³ΠΎΠ»ΠΎΠ²ΠΎΠΊ "Accept-Language" Π² соотвСтствии с Ρ„ΠΎΡ€ΠΌΠ°Ρ‚ΠΎΠΌ RFC 2616.

Π’ΠΎΠ·Π²Ρ€Π°Ρ‰Π°Π΅ΠΌΡ‹Π΅ значСния

Π‘ΠΎΠΎΡ‚Π²Π΅Ρ‚ΡΡ‚Π²ΡƒΡŽΡ‰ΠΈΠΉ ΠΈΠ΄Π΅Π½Ρ‚ΠΈΡ„ΠΈΠΊΠ°Ρ‚ΠΎΡ€ Π»ΠΎΠΊΠ°Π»ΠΈ.

Π’ΠΎΠ·Π²Ρ€Π°Ρ‰Π°Π΅Ρ‚ false, Ссли Π΄Π»ΠΈΠ½Π° header ΠΏΡ€Π΅Π²Ρ‹ΡˆΠ°Π΅Ρ‚ INTL_MAX_LOCALE_LEN.

ΠŸΡ€ΠΈΠΌΠ΅Ρ€Ρ‹

ΠŸΡ€ΠΈΠΌΠ΅Ρ€ #1 ΠŸΡ€ΠΈΠΌΠ΅Ρ€ использования locale_accept_from_http()

<?php
$locale
= locale_accept_from_http($_SERVER['HTTP_ACCEPT_LANGUAGE']);
echo
$locale;
?>

ΠŸΡ€ΠΈΠΌΠ΅Ρ€ #2 ΠŸΡ€ΠΈΠΌΠ΅Ρ€ использования Π² ΠΎΠ±ΡŠΠ΅ΠΊΡ‚Π½ΠΎ-ΠΎΡ€ΠΈΠ΅Π½Ρ‚ΠΈΡ€ΠΎΠ²Π°Π½Π½ΠΎΠΌ стилС

<?php
$locale
= Locale::acceptFromHttp($_SERVER['HTTP_ACCEPT_LANGUAGE']);
echo
$locale;
?>

Π Π΅Π·ΡƒΠ»ΡŒΡ‚Π°Ρ‚ выполнСния ΠΏΡ€ΠΈΠ²Π΅Π΄Ρ‘Π½Π½ΠΎΠ³ΠΎ ΠΏΡ€ΠΈΠΌΠ΅Ρ€Π°:

en_US

Π‘ΠΌΠΎΡ‚Ρ€ΠΈΡ‚Π΅ Ρ‚Π°ΠΊΠΆΠ΅

  • locale_lookup() - Поиск языковых ΠΌΠ΅Ρ‚ΠΎΠΊ Π½Π°ΠΈΠ±ΠΎΠ»Π΅Π΅ подходящих Π·Π°Π΄Π°Π½Π½ΠΎΠΉ Π»ΠΎΠΊΠ°Π»ΠΈ
οΌ‹Π”ΠΎΠ±Π°Π²ΠΈΡ‚ΡŒ

ΠŸΡ€ΠΈΠΌΠ΅Ρ‡Π°Π½ΠΈΡ ΠΏΠΎΠ»ΡŒΠ·ΠΎΠ²Π°Ρ‚Π΅Π»Π΅ΠΉ 5 notes

up
46
Adam Lange ΒΆ
13 years ago
It's good to mention that if user browser will not send HTTP_ACCEPT_LANGUAGE, the output from:

 Locale::acceptFromHttp($_SERVER['HTTP_ACCEPT_LANGUAGE']);

Will be null. 

So remember to set up a fail over scenario!
up
11
rasmus at mindplay dot dk ΒΆ
8 years ago
If you're struggling to figure out how to use this function in a project that only supports certain languages, move along.

Unfortunately, this function doesn't let you specify languages supported by your project - and since the internal header parsing and negotiation logic isn't exposed in any other way, you'll most likely want to ditch this function and go for a custom implementation of the same thing:

https://github.com/willdurand/Negotiation
up
1
mollasadra at g dot com ΒΆ
8 years ago
Didn't see this being documented anywhere and the bug hasn't been addressed yet, so to save headache for otherS, this method does a little weird thing with these different locales:

php > echo locale_accept_from_http("zh_TW");
zh
php > echo locale_accept_from_http("zh_CN");
zh
up
0
bill at m$ dot com ΒΆ
3 years ago
A nice way to detect humans. Bots usually dont have this HTTP_ACCEPT_LANGUAGE set.
up
0
bohwaz ΒΆ
4 years ago
Please note that this method can throw IntlException in some cases when the passed header string is invalid AND you have enabled exceptions.  For example:

<?php

ini_set('intl.use_exceptions', 1);

$header = 'fr-FR,fr;q=0.9,fr;q=0.9;q=0.8,en-gb;q=0.8;q=0.7,en;q=0.6,en;q=0.7;q=0.5,*;q=0.5;q=0.4';
var_dump(locale_accept_from_http($header));

?>

Returns:

PHP Warning:  Uncaught IntlException: locale_accept_from_http: failed to find acceptable locale in ...