2016-02-10 71 views
0

我一直有問題讓翻譯工作在我的本地WAMP(v2.5)服務器上。這是一個問題的例子。PHP gettext在本地WAMP服務器上不起作用

putenv("LANG=frc");       //output: 1 
setlocale(LC_ALL, "frc");     //output: French_Canada.1252 

$domain = 'messages'; 

bindtextdomain($domain, "locale");   //output: D:\wamp\www\[project folder]\locale 
bind_textdomain_codeset($domain, 'UTF-8'); //output: UTF-8 
textdomain($domain);       //output: messages 

echo gettext("Hello");      //output: Hello 

這裏是文件夾結構:

locale 
    --> French_Canada.1252 
     --> LC_MESSAGES 
      --> messages.po 
      --> messages.mo 

最後PO文件:

msgid "" 
msgstr "" 
"Project-Id-Version: \n" 
"POT-Creation-Date: 2016-02-10 14:01-0500\n" 
"PO-Revision-Date: 2016-02-10 14:01-0500\n" 
"Last-Translator: \n" 
"Language-Team: \n" 
"Language: fr_CA\n" 
"MIME-Version: 1.0\n" 
"Content-Type: text/plain; charset=UTF-8\n" 
"Content-Transfer-Encoding: 8bit\n" 
"X-Generator: Poedit 1.8.2\n" 
"X-Poedit-Basepath: ../../..\n" 
"Plural-Forms: nplurals=2; plural=(n > 1);\n" 
"X-Poedit-SearchPath-0: .\n" 

#: test.php:11 
msgid "Hello" 
msgstr "Bonjour" 

這是我已經試過:
1)卸載64位WAMP和在其位置安裝32位WAMP。
2)使用不同的區域代碼,如fr_CA,fr_FR,fr。 fr是唯一被識別的,但我仍然有同樣的問題(即使將文件夾重命名爲fr後)
3)將代碼集更改爲1252和Windows-1252(在php文件和PO文件中)
4)每次更改後重新啓動所有服務

+0

僅供參考,gettext的停止後PHP/5.3(見[Gettext的總是會使用系統默認區域設置(http://stackoverflow.com/questions/19569362/gettext-will-always-use-在Windows上正常工作系統默認的語言環境))。無論如何,我建議你將你的文件夾從'French_Canada.1252'重新命名爲'fr'。 –

+0

我別無選擇,只能使用[php-gettext](https://launchpad.net/php-gettext/) – Falantar

回答

0

使用GetText類:https://github.com/oscarotero/Gettext 並在您的本地或Web上加載您的.po文件。

use Gettext\Translator; 
use Gettext\GettextTranslator; 

$locale = !empty($_GET['locale']) ? $_GET['locale'] : Locale::acceptFromHttp($_SERVER['HTTP_ACCEPT_LANGUAGE']); 
$_SESSION['locale'] = $locale; 
putenv("LC_ALL=$locale"); 
setlocale(LC_ALL, $locale); 
$domain = 'traduction'; 

if(strstr($_SERVER['HTTP_HOST'], '127.0.0.1')) { 

    $t = new Translator(); 

    //Create a Translations instance using a po file 
    $translations = Gettext\Translations::fromPoFile('locale/'.$locale.'/LC_MESSAGES/'.$domain.'.po'); 
    $t->loadTranslations($translations); 


} else { 

    //Create the translator instance 
    $t = new GettextTranslator(); 

    //Set the language and load the domain 
    $t->setLanguage($locale); 
    $t->loadDomain($domain, 'Locale'); 

} 

//If you want use the global functions 
$t->register(); 

echo __('Pomme'); // "Apple" 
echo n__('%d fenetre', '%d fenetres', 2, 2); // "2 windows" 
相關問題