2010-03-08 55 views
2

我的文件夾lang中有一個文件dk.podk.mo在我的webdir中。如何在php中使用gettext?

我該如何使用這個文件?我嘗試了所有,但我無法讓它工作。

// Lang 
putenv('LC_ALL=dk'); 
setlocale(LC_ALL, 'dk'); 

// Specify location of translation tables 
bindtextdomain("dk", ROOT .'lang'); 

// Choose domain 
textdomain("dk"); 
+2

我想知道是否有必要在調用setlocale之前調用putenv('...')? – Leto 2012-09-26 10:40:51

+0

@Leto據我所知,putenv調用是爲了兼容Windows。 – Merijn 2013-08-05 09:14:03

回答

5

我這個已經工作:

setlocale(LC_ALL, 'ar_LY.utf8'); 
bindtextdomain("trans", $_SERVER["DOCUMENT_ROOT"].'/trans/locale'); 
textdomain("trans"); 

語言文件路徑:

/var/www/trans/locale/ar_LY/LC_MESSAGES/trans.mo 

,我認爲(不知道),你必須使用相同的路徑!

2

這對我的作品 我的文件messages.mo

static function initialize_i18n() { 
    $locale=App::$locale;   
    $locales_root = App::$root."locale"; 
    putenv('LANG='.$locale); 
    putenv('LANGUAGE='.$locale); 
    putenv('LC_ALL='.$locale);  
    putenv('LC_MESSAGES='.$locale); 
    setlocale(LC_ALL, $locale.".utf-8"); 
    $domains = glob($locales_root.'/'.$locale.'/LC_MESSAGES/messages-*.mo'); 
    $current = basename($domains[0],'.mo'); 
    $timestamp = preg_replace('{messages-}i','',$current); 
    bindtextdomain("messages",$locales_root); 
    textdomain("messages"); 
}  
+0

嗯,我不是很瞭解:/我有文件到這個文件夾/var/www/domain.com/lang/dk/dk.mo 你能從這個信息做出樣本嗎? – ParisNakitaKejser 2010-03-08 13:37:32

+0

即時通訊對不起我的變數..用「lang/dk」和App :: $ root替換$ locale與「/var/www/domain.com/」 – 2010-03-08 15:17:23

-1

命名我會認真建議使用Zend_Translate此,你可以在不同類型的系統與locales碰到很多不一致的。

文檔:http://framework.zend.com/manual/en/zend.translate.using.html

+0

如果我有一個大項目,我可以出zend翻譯成這個?還是會很難? – ParisNakitaKejser 2010-03-08 13:35:35

+0

只需抓住整個Zend庫,只包含Zend Translate類,你應該很好。 – 2010-03-08 13:43:19

+0

好吧,我所有的「gettext()」功能仍然有效? :) – ParisNakitaKejser 2010-03-08 13:44:09