I'm trying to achieve, that when someone visits my wordpress page the .po (language-) packages of his pefered language are loaded.
At the moment it is possible to change the language by adding a ?lang= parameter to the URL. But i want the right language to be selected based on the browser language.
My code:
$locale is used to set the language and select the right .po files.
Now i want $locale to be
<blockquote>
$locale = 'en_US'
</blockquote>
by default, but when someone enters the page that has default language "de", "de_DE", "de_CH" or "de_AT" it should be.
<blockquote>
$locale = 'de_DE'
</blockquote>
The Code im currently using isnt working.
Shows me the right language, which is "de_DE", but
doesnt do anything. On the other hand when i set
it works...
Thank you guys in advance.
<strong>Edit:</strong>
When i use
is says de-DE. Thats very stange, because it doesnt work...
<strong>Edit 2:</strong>
Thats because it must be de_DE (underline) not de-DE (minus)... how to fix that?
<strong>Edit 3:</strong>
Finally it works.
At the moment it is possible to change the language by adding a ?lang= parameter to the URL. But i want the right language to be selected based on the browser language.
My code:
Code:
<?php
// start the session
session_start();
$browserlang = " ".$_SERVER['HTTP_ACCEPT_LANGUAGE'];
// if there's a "lang" parameter in the URL...
if( isset( $_GET[ 'lang' ] ) ) {
// ...set a session variable named WPLANG based on the URL parameter...
$_SESSION[ 'WPLANG' ] = $_GET[ 'lang' ];
// ...and define the WPLANG constant with the WPLANG session variable
$locale = $_SESSION[ 'WPLANG' ];
echo 'Based on URL parameter';
// if there isn't a "lang" parameter in the URL...
} else {
// if the WPLANG session variable is already set...
if( isset( $_SESSION[ 'WPLANG' ] ) ) {
// ...define the WPLANG constant with the WPLANG session variable
$locale = $_SESSION[ 'WPLANG' ];
echo 'Based on session variable';
// if the WPLANG session variable isn't set...
} else {
// set the WPLANG constant to your default language code is (or empty, if you don't need it)
$locale = $browserlang;
echo 'Should be based on browser language. It is:' . $browserlang;
}
};
?>
$locale is used to set the language and select the right .po files.
Now i want $locale to be
<blockquote>
$locale = 'en_US'
</blockquote>
by default, but when someone enters the page that has default language "de", "de_DE", "de_CH" or "de_AT" it should be.
<blockquote>
$locale = 'de_DE'
</blockquote>
The Code im currently using isnt working.
Code:
$browserlang = " ".$_SERVER['HTTP_ACCEPT_LANGUAGE'];
echo $browserlang;
Shows me the right language, which is "de_DE", but
Code:
$locale = $browserlang
Code:
$locale = 'de_DE'
Thank you guys in advance.
<strong>Edit:</strong>
When i use
Code:
echo $locale
<strong>Edit 2:</strong>
Thats because it must be de_DE (underline) not de-DE (minus)... how to fix that?
<strong>Edit 3:</strong>
Finally it works.