html - Detect Browser Language in PHP and set $locale accordingly -
i'm trying achieve, when visits wordpress page .po (language-) packages of pefered language loaded. @ moment possible change language adding ?lang= parameter url. want right language selected based on browser language.
my code:
<?php // start session session_start(); $browserlang = " ".$_server['http_accept_language']; // if there's "lang" parameter in url... if( isset( $_get[ 'lang' ] ) ) { // ...set session variable named wplang based on url parameter... $_session[ 'wplang' ] = $_get[ 'lang' ]; // ...and define wplang constant wplang session variable $locale = $_session[ 'wplang' ]; echo 'based on url parameter'; // if there isn't "lang" parameter in url... } else { // if wplang session variable set... if( isset( $_session[ 'wplang' ] ) ) { // ...define wplang constant wplang session variable $locale = $_session[ 'wplang' ]; echo 'based on session variable'; // if wplang session variable isn't set... } else { // set wplang constant default language code (or empty, if don't need it) $locale = $browserlang; echo 'should based on browser language. is:' . $browserlang; } }; ?>
$locale used set language , select right .po files.
now want $locale
$locale = 'en_us'
by default, when enters page has default language "de", "de_de", "de_ch" or "de_at" should be.
$locale = 'de_de'
the code im using isnt working.
$browserlang = " ".$_server['http_accept_language']; echo $browserlang;
shows me right language, "de_de", $locale = $browserlang
doesnt anything. on other hand when set $locale = 'de_de'
works...
thank guys in advance.
edit:
when use echo $locale
says de-de. thats stange, because doesnt work...
edit 2:
thats because must de_de (underline) not de-de (minus)... how fix that?
edit 3:
finally works.
shows me right language, "de_de",
it shouldn't. code you've shown inserts space before header value.
also, code not handle multi-valued accept-language header can include preferences (e.g. see here parser)
how normalize value ( {"de", "de_de", "de_ch","de_at"} -> "de_de" ) you.
Comments
Post a Comment