/    Sign up×
Community /Pin to ProfileBookmark

Hi to all of you.

I have a multilingual php website (en, de, cz). Each language I keep in separate .php file which I include into script when user change language (from icon). But I would like to make my script more clever, and exactly when user from UK access to my page, the script show the text in english language; the same for other supporting languages. I heard before that this is possible if I watch into HTTP header return by Apache webserver(Accept-Language) ???? If anyone of you know more about this please help me, I’ll be appreciate.

Thanks

to post a comment
PHP

5 Comments(s)

Copy linkTweet thisAlerts:
@NogDogJun 24.2006 — You could do something like:
[code=php]
<?php
if(isset($_COOKIE['language'])) // see if you already have preference
{
$language = $_COOKIE['language'];
}
elseif(!empty($_SERVER['HTTP_ACCEPT_LANGUAGE'])) // use http header if there
{
$language = strtolower(array_shift(explode('-',
$_SERVER['HTTP_ACCEPT_LANGUAGE'])));
}
else // otherwise set to default
{
$language = 'en'; // change to whatever default language is desired
}
switch($language)
{
case 'en':
// display in English
break;
case 'de':
// display in German
break;
case 'fr':
// display in French
break;
default:
// display in default language
}
?>
[/code]
Copy linkTweet thisAlerts:
@bokehJun 24.2006 — Nogdog's approach is good but but needs to include cookie setting. The following is something I wrote ages ago for [URL=http://costablancatranslations.com/]this site[/URL]. It runs the whole string and if more than one of available languages is contained in the accept language header it will choose the one that appears earlier in the string (which is the correct behaviour).[code=php]<?php

function language()
{
# function returns longhand form of the language.
# function expects longhand form of the language in the
# $_GET variable to switch languages (?language=english)

// Set variables
$default_language = 'english';
$supported_languages = array(
'en' => 'english',
'cz' => 'czech',
'de' => 'german'
);

// Client is setting language with query string '?language=english
if((!empty($_GET['language'])) and (in_array($_GET['language'], $supported_languages)))
{
setcookie ('language', $_GET['language'], time()+100000000, '/', '', 0);
return $_GET['language'];
}

// Check for preset language in $_COOKIE['language']
if((!empty($_COOKIE['language'])) and (in_array($_COOKIE['language'], $supported_languages)))
{
return $_COOKIE['language'];
}

// If none of the above the script finds the first
// matching browser language or returns the language
// in the $default_language variable on no match
if(empty($_SERVER['HTTP_ACCEPT_LANGUAGE']))
{
return $default_language;
}
$bar = ''; # to use in regex pattern
foreach($supported_languages as $k => $v){
$pattern .= $bar.$k;
$bar = '|';
}
if(preg_match('/'.$pattern.'/', $_SERVER['HTTP_ACCEPT_LANGUAGE'], $matches))
{
return $supported_languages[$matches['0']];
}
return $default_language;
}

#test it
echo '<p>Currently active language: <span style="color:#f00">'. language() .'</span></p>';


?>

<p><a href="<?php echo $_SERVER['PHP_SELF']?>?language=english">english</a></p>
<p><a href="<?php echo $_SERVER['PHP_SELF']?>?language=german">german</a></p>
<p><a href="<?php echo $_SERVER['PHP_SELF']?>?language=czech">czech</a></p>
<p>After changing language delete the query string and refresh. Notice your choice sticks.</p>[/code]
Copy linkTweet thisAlerts:
@NogDogJun 24.2006 — Nogdog's approach is good but but needs to include cookie setting. [/quote]
I can't do [i]everything[/i]! :p
Copy linkTweet thisAlerts:
@bokehJun 24.2006 — I can't do [i]everything[/i]! :p[/QUOTE]Nor can I! At the minute I can't find the time to learn Java.
Copy linkTweet thisAlerts:
@NogDogJun 24.2006 — Nor can I! At the minute I can't find the time to learn Java.[/QUOTE]
I've got the time - I just keep falling asleep when I try to read my Java books. :rolleyes:
×

Success!

Help @riverside spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.27,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...