如何用gettext支持PHP多语言页面?

时间:2017-03-08 14:15:06

标签: php gettext

首先,我创建了一个具有这种结构的小项目:

Locale->en_UK -> LC_MESSAGES -> messages.po
Locale->es_ES -> LC_MESSAGES -> messages.po
Locale->fr_FR -> LC_MESSAGES -> messages.po
example.php

消息是:

msgid "Hello World!"
msgstr "HELLO"
msgid "Testing Translation..."
msgstr "TRANSLATION FOR UK"

我只想更改不同语言的msgstr。

我的PHP文件包含:

<?php
session_start();
// get language preference
if (isset($_GET["lang"])) {
    $language = $_GET["lang"];
}
else if (isset($_SESSION["lang"])) {
    $language  = $_SESSION["lang"];
}
else {
    $language = "en_US";
}
$_SESSION["Language"]  = $language;
$folder = "Locale";
$domain = "messages";
$encoding = "UTF-8";
putenv("LANG=" . $language);
setlocale(LC_ALL, $language);
bindtextdomain($domain, $folder);
bind_textdomain_codeset($domain, $encoding);
textdomain($domain);
echo _("Hello World!"), "<br>";
echo _("Testing Translation...");

当我用?lang=en_UK?lang=fr_FR调用此PHP文件时,PHP不会更改文本,可能是什么问题?谢谢你的帮助!

0 个答案:

没有答案