Ben çoklu dil web sitesi için benim index.php olarak aşağıdaki kodu var. Her kullanılabilir dil bir dizin bulunur.
<?php
if (isset($_POST['URL']) && strlen($_POST['URL']) == 3) {
header("location: ".$_POST[URL]);
}
else {
function lixlpixel_get_env_var($Var)
{
if(empty($GLOBALS[$Var]))
{
$GLOBALS[$Var]=(!empty($GLOBALS['_SERVER'][$Var]))?
$GLOBALS['_SERVER'][$Var] : (!empty($GLOBALS['HTTP_SERVER_VARS'][$Var])) ? $GLOBALS['HTTP_SERVER_VARS'][$Var]:'';
}
}
function lixlpixel_detect_lang()
{
// Detect HTTP_ACCEPT_LANGUAGE & HTTP_USER_AGENT.
lixlpixel_get_env_var('HTTP_ACCEPT_LANGUAGE');
lixlpixel_get_env_var('HTTP_USER_AGENT');
$_AL=strtolower($GLOBALS['HTTP_ACCEPT_LANGUAGE']);
$_UA=strtolower($GLOBALS['HTTP_USER_AGENT']);
// Try to detect Primary language if several languages are accepted.
foreach($GLOBALS['_LANG'] as $K)
{
if(strpos($_AL, $K)===0)
return $K;
}
// Try to detect any language if not yet detected.
foreach($GLOBALS['_LANG'] as $K)
{
if(strpos($_AL, $K)!==false)
return $K;
}
foreach($GLOBALS['_LANG'] as $K)
{
if(preg_match("/[[( ]{$K}[;,_-)]/",$_UA))
return $K;
}
// Return default language if language is not yet detected.
return $GLOBALS['_DLANG'];
}
// Define default language.
$GLOBALS['_DLANG']='en';
// Define all available languages.
// WARNING: uncomment all available languages
$GLOBALS['_LANG'] = array(
'en', // english.
'es', // spanish.
'fr', // french.
);
/*
$GLOBALS['_LANG'] = array(
'af', // afrikaans.
'ar', // arabic.
'bg', // bulgarian.
'ca', // catalan.
'cs', // czech.
'da', // danish.
'de', // german.
'el', // greek.
'en', // english.
'es', // spanish.
'et', // estonian.
'fi', // finnish.
'fr', // french.
'gl', // galician.
'he', // hebrew.
'hi', // hindi.
'hr', // croatian.
'hu', // hungarian.
'id', // indonesian.
'it', // italian.
'ja', // japanese.
'ko', // korean.
'ka', // georgian.
'lt', // lithuanian.
'lv', // latvian.
'ms', // malay.
'nl', // dutch.
'no', // norwegian.
'pl', // polish.
'pt', // portuguese.
'ro', // romanian.
'ru', // russian.
'sk', // slovak.
'sl', // slovenian.
'sq', // albanian.
'sr', // serbian.
'sv', // swedish.
'th', // thai.
'tr', // turkish.
'uk', // ukrainian.
'zh' // chinese.
);
*/
// Redirect to the correct location.
header('location: /'.lixlpixel_detect_lang());
//header('location: http://www.your_site.com/index_'.lixlpixel_detect_lang().'.php'); // Example Implementation
echo 'The Language detected is: '.lixlpixel_detect_lang(); // For Demonstration
}
>
Sorun kullanıcı tarayıcılarda bu (Googlebot gibi) arama motorları ile mükemmel çalışır, ancak aşağıdaki hata atılır olmasıdır:
<br />
<b>Warning</b>: preg_match() [<a href='function.preg-match'>function.preg-match</a>]: Compilation failed: range out of order in character class at offset 12 in <b>/index.php</b> on line <b>41</b><br />
<br />
<b>Warning</b>: preg_match() [<a href='function.preg-match'>function.preg-match</a>]: Compilation failed: range out of order in character class at offset 12 in <b>/index.php</b> on line <b>41</b><br />
<br />
<b>Warning</b>: preg_match() [<a href='function.preg-match'>function.preg-match</a>]: Compilation failed: range out of order in character class at offset 12 in <b>/index.php</b> on line <b>41</b><br />
<br />
<b>Warning</b>: Cannot modify header information - headers already sent by (output started at /index.php:41) in <b>/index.php</b> on line <b>110</b><br />
<br />
<b>Warning</b>: preg_match() [<a href='function.preg-match'>function.preg-match</a>]: Compilation failed: range out of order in character class at offset 12 in <b>/index.php</b> on line <b>41</b><br />
<br />
<b>Warning</b>: preg_match() [<a href='function.preg-match'>function.preg-match</a>]: Compilation failed: range out of order in character class at offset 12 in <b>/index.php</b> on line <b>41</b><br />
<br />
<b>Warning</b>: preg_match() [<a href='function.preg-match'>function.preg-match</a>]: Compilation failed: range out of order in character class at offset 12 in <b>/index.php</b> on line <b>41</b><br />
The Language detected is: en
Ben hata işleme denedim ama ben bir PHP programcısı değilim, ben bir CF PROGAMMER değilim, bu yüzden gerçekten yardıma ihtiyacı var!