PHP dil dosyası yapmak için en etkili yolu?

6 Cevap php

Questions Updated instead of making a new question...

I really want to provide a few alternative languages other then English on my social network site I am building, this will be my first time doing any kind of language translation so please bear with me.
I am researching so I am al ear and open to ideas and I have a lot already here is are the questions.

1)
What does i18n mean, I see it often when researching language translation on SO?

2)
Most people say use gettext PHP has an extension or support for it,
well I have been researching it and I have a basic understanding of it, as far as I can tell it is a lot of extra work to go this route,
I mean coding my site to use it's functions ie; _('hello world i'm in English for now') or else gettext('hello world i'm in English for now') is no problem as any route I go will require that.
But then you have to install gettext on your server and get it working,
then use some special editors to create special files and compile them I think?

Sounds like a pain, I understand this is supposed to be the best route to go though, well everyone seems to say it is.
So can someone tell me why this is the route to go?

3)
I really like the simplicity of this approach, just building a language array and calling the phrase you need in a function like the example below , you would then just include a file with the appropriate language array.

Ne gerçekten bilmek istiyorum, bu yüzden lütfen nedenini açıklayabilir gettext kullanarak ve eğer karşılaştırıldığında yüksek bir trafiğe daha az iyi performans yöntemi ve oldukça büyük bir site olurdu, değil mi?

<?PHP
//Have seperate language files for each language I add, this would be english file
function lang($phrase){
    static $lang = array(
        'NO_PHOTO' => 'No photo\'s available',
        'NEW_MEMBER' => 'This user is new'
    );
    return $lang[$phrase];
}
//Then in application where there is text from the site and not from users I would do something like this
echo lang('NO_PHOTO');  // No photo's available would show here
?>

* Brianreavis yıllardan itibaren kullanılan bazı kod aşağıda cevap

6 Cevap

Tekerleği yeniden icat etmeyin. Örneğin, gettext ya da Zend_Translate kullanın.

Muhtemelen dil haritalama işleyen bir işlev tanımlamak için iyi olurdu. Eğer do daha sonra nasıl çalıştığını değiştirmek isterseniz bu şekilde, size $lang[...] kullanılır ve başka bir şey ile değiştirmek durumlar için komut yüzlerce koşuşturmak zorunda değiliz.

Böyle bir şey işe yarayacağını ve güzel & olurdu Hızlı:

function lang($phrase){
    static $lang = array(
        'NO_PHOTO' => 'No photo\'s available',
        'NEW_MEMBER' => 'This user is new'
    );
    return $lang[$phrase];
}

Bu fonksiyon denir her zaman tahsislerini almaz böylece sure dizisi işlev içinde static ilan emin olun. $lang gerçekten büyük olduğunda bu özellikle önemlidir.

Kullanmak için:

echo lang('NO_PHOTO');

Birden çok dil işleme için, sadece ve kullanıcı için require() uygun olanı (en.php, fr.php, vb gibi) birden fazla dosya tanımlanan bu işlevi var.

Kendi dil çerçeve yazmayın. Kullan gettext. PHP kurabilirsiniz standart bindings vardır.

Bu daha iyi çalışabilir:

function _L($phrase){
static $_L = array(
    'NO_PHOTO' => 'No photo\'s available',
    'NEW_MEMBER' => 'This user is new'
);

     return (!array_key_exists($phrase,$_L)) ? $phrase : $_L[$phrase];
}

Ben şimdi için kullanmak ne Thats. Dil bulunamazsa, bunun yerine bir hata, ifade dönecektir.

Bir dizi en fazla 65500 ~ den öğeleri içerebilir dikkat etmelisiniz. Sadece diyorum, ama yeterince iyi olmalıdır.

İşte ben kullanıcının diline kontrol etmek için kullanmak bazı kod:

<?php
function setSessionLanguageToDefault() {
    $ip=$_SERVER['REMOTE_ADDR'];
    $url='http://api.hostip.info/get_html.php?ip='.$ip;
    $data=file_get_contents($url);
    $s=explode (':',$data);
    $s2=explode('(',$s[1]);

    $country=str_replace(')','',substr($s2[1], 0, 3));

    if ($country=='us') {
        $country='en';
    }

    $country=strtolower(ereg_replace("[^A-Za-z0-9]", "", $country ));
    $_SESSION["_LANGUAGE"]=$country;
}

if (!isset($_SESSION["_LANGUAGE"])) {
    setSessionLanguageToDefault();
}

if (file_exists(APP_DIR.'/language/'.$_SESSION["_LANGUAGE"].'.php')) {
    include(APP_DIR.'/language/'.$_SESSION["_LANGUAGE"].'.php');
} else {
    include(APP_DIR.'/language/'.DEFAULT_LANG.'.php');
}

?>

Onun henüz bitmiş, ama iyi i bu çok yardımcı olabileceğini düşünüyorum.

Diğer cevaplar gerçekten tüm sorulara cevap yok gibi, benim cevap olduğu için gitmek artı mantıklı bir alternatif sunan olacaktır.

1) I18n is short for Internationalization and has some similarities to I-eighteen-n.

2) In my honest opinion gettext is a waste of time.

3) Your approach looks good. What you should look for are language variables. The WoltLab Community Framework 2.0 implements a two-way language system. For once there are language variables that are saved in database and inside a template one only uses the name of the variable which will then be replaced with the content of the variable in the current language (if available). The second part of the system provides a way to save user generated content in multiple languages (input in multiple languages required).

Temelde geliştirici ve kullanıcı tarafından tanımlanan içerik tarafından tanımlanan arayüz metni var. Içeriğin dilli metin dil değişkenleri kaydedilir ve (tek dil içeriği de mümkün olduğu gibi) dil değişkenin adı daha sonra belirli bir içerik tablodaki metin alanı için bir değer olarak kullanılır.

WCF yapısı çerçevenin dışında kodu yeniden çok zor bir şekilde ne yazık ki ama ilham kaynağı olarak kullanabilirsiniz. Sistemin kapsamı sadece sizin sitenize elde etmek istediğinize bağlıdır. Bu kesinlikle WCF sistem bir göz atmalısınız daha büyük olacak eğer. Bu geçerli dil için doğru olanı dahil olduğu birkaç özel dil dosyaları (de.php, en.php, vb), küçük ise, yapacağız.

neden sadece bu gibi multi-Boyutlu dizi ... gibi yapmak değil

<?php

$lang = array(
    'EN'=> array(
        'NO_PHOTO'=>'No photo\'s avaiable',
        'NEW_MEMBER'=>'This user is new',
    ),
    'MY'=> array(
        'NO_PHOTO'=>'Tiada gambar',
        'NEW_MEMBER'=>'Ini adalah pengguna baru',
    )
);

?>