PHP dili algılama

4 Cevap

İlk multilangual sitesi kurmak için çalışan bir hobbycoder Im

Kullanıcıların dili algılamak için bu kod parçası kullanın. Eğer bir dil seçilmiş sığınak eğer i tho onu alır nerede bilmiyorum HTTP_ACCEPT_LANGUAGE dayalı dil dosyasını içerecektir ..

session_start();

if (!isset($_SESSION['lang'])) {
   $_SESSION['lang'] = substr($_SERVER['HTTP_ACCEPT_LANGUAGE'], 0, 2);
}

elseif (isset($_GET['setLang']) && $_GET['setLang'] == 'en') $_SESSION['lang'] = "en";
elseif (isset($_GET['setLang']) && $_GET['setLang'] == 'sv') $_SESSION['lang'] = "sv";
elseif (isset($_GET['setLang']) && $_GET['setLang'] == 'pl') $_SESSION['lang'] = "pl";
elseif (isset($_GET['setLang']) && $_GET['setLang'] == 'fr') $_SESSION['lang'] = "fr";

include('languages/'.$_SESSION['lang'].'.php');

Bu benim için çalışıyor ve lehçe lang dosyasını içerir. Ama bu kod doğru mu? Ya da başka bir yolu var mı? Nasıl YouTube örneğin yapar sizce?

Would be great if some english, french, swedish or polish folks could visit my site and see if it includes the right file! So i know that it doesnt just work for me :) http://jorm.homeftp.org/

Neyse benim kod geliştirmek olabilir mi? i tüm bu elseif ile daha fazla dil eklemek gibi o pis görünüyor olacak!

Teşekkürler

4 Cevap

Tarayıcı genellikle bir HTTP başlığı gönderir, isim Accept-Language kullanıcı almak için istekli olduğu dilleri gösterir.

Örneğin, bu başlık olabilir:

Accept-Language: en-us,en;q=0.5

Öncelik kavramı btw, içinde var ;-)

PHP olarak, $_SERVER süper global bu alabilirsiniz:

var_dump($_SERVER['HTTP_ACCEPT_LANGUAGE']);

Beni alacak:

string 'en-us,en;q=0.5' (length=14)

Şimdi, bunu ayrıştırmak zorunda ;-)


If I edit my preferences in the browser's option to say "I want french, and if you don't can't serve me french, get me english from the US ; and if you can't get me that either, just get me english), the header will be :

Accept-Language: fr-fr,en-us;q=0.7,en;q=0.3

Ve, PHP:

string 'fr-fr,en-us;q=0.7,en;q=0.3' (length=26)


For more informations, you can take a look at section 14.4 of the HTTP RFC.

Ve muhtemelen bu başlık ayrıştırmak için PHP kod örneği çok bulabilirsiniz; Örneğin: Parse Accept-Language to detect a user's language

Eğlenin!

Bu deneyin

function getUserLanguage() {
$langs = array();
if (isset($_SERVER['HTTP_ACCEPT_LANGUAGE'])) {
// break up string into pieces (languages and q factors)
preg_match_all(‘/([a-z]{1,8}(-[a-z]{1,8})?)\s*(;\s*q\s*=\s*(1|0\.[0-9]+))?/i’,
$_SERVER['HTTP_ACCEPT_LANGUAGE'], $lang_parse);
if (count($lang_parse[1])) {
// create a list like “en” => 0.8
$langs = array_combine($lang_parse[1], $lang_parse[4]);
// set default to 1 for any without q factor
foreach ($langs as $lang => $val) {
if ($val === ”) $langs[$lang] = 1;
}
// sort list based on value
arsort($langs, SORT_NUMERIC);
}
}
//extract most important (first)
foreach ($langs as $lang => $val) { break; }
//if complex language simplify it
if (stristr($lang,”-”)) {$tmp = explode(“-”,$lang); $lang = $tmp[0]; }
return $lang;
}

Burada iki dilli bir site için kullanılan komut dosyası bulunuyor. Bu index.php ve mysite.com olarak kullanılacak. Kullanıcının tercih langauge sitesi mevcut değilse, kullanıcının tarayıcının dil tercihine göre, bu site veya varsayılan dil sitenin İstediğiniz dil sürümü için yönlendirme olur.

<?php
// List of available localized versions as 'lang code' => 'url' map
$sites = array(
    "en" => "http://en.mysite.com/",
    "bn" => "http://bn.mysite.com/",
);

// Get 2 char lang code
$lang = substr($_SERVER['HTTP_ACCEPT_LANGUAGE'], 0, 2);

// Set default language if a `$lang` version of site is not available
if (!in_array($lang, array_keys($sites)))
    $lang = 'en';

// Finally redirect to desired location
header('Location: ' . $sites[$lang]);
?>

Your code looks just fine. You might want to add a final else default choice if the visitor asks for a language you aren't providing. Also, if the visitor himself selects a language you should save that choice in a persistent cookie and check its value, giving it precedence over HTTP_ACCEPT_LANGUAGE.

Bildiğim kadarıyla söyleyebilirim Youtube HTTP_ACCEPT_LANGUAGE kullanıyor, ancak ziyaretçinin ülke dildeki uyuşmuyor, aynı zamanda dilde bir değişiklik önermek için IP geolocation kullanır. Kesinlikle rahatsız edici.

Sadece nitpicking: bunu bir switch() ifadesi daha okunabilir olabilir listesine dilleri ekleyin eğer.