Gettext示例不适用于Windows 7 - XAMPP

时间:2015-06-01 09:53:53

标签: php localization gettext

我有最新的XAMPP(3.2.1)和PHP 5.6.8并启用了gettext。我试图让gettext()在那里使用我在互联网上找到的样本https://github.com/leftnode/gettext-example,但它无法正常工作。

我试过的其他代码(translation.php):

<?php
  session_start();

  $textdomain = "messages";
  $lang = "de_DE";

  echo(putenv("LC_ALL=".$lang)."<br>"); // outputs 1
  setlocale(LC_ALL, $lang);
  echo(bindtextdomain($textdomain, "locale")."<br>"); // successfully outputs the correct path to locale
  textdomain($textdomain);

  echo ((function_exists("_") && function_exists("gettext"))?"gettext loaded\n<br>":""); // outputs gettext loaded

  $name = "Vic";
  printf(_("Hello, %s, it is nice to see you today.\n"), $name); // outputs original english
?>

翻译文件 - 使用Poedit 1.8.1(messages.po)编译

# SOME DESCRIPTIVE TITLE.
# Copyright (C) YEAR Codegroove.net
# This file is distributed under the same license as the PACKAGE package.
# FIRST AUTHOR <EMAIL@ADDRESS>, YEAR.
#
msgid ""
msgstr ""
"Project-Id-Version: Codegroove.net Example Translations 0.0.1\n"
"Report-Msgid-Bugs-To: vmc@codegroove.net\n"
"POT-Creation-Date: 2015-06-01 11:34+0100\n"
"PO-Revision-Date: 2015-06-01 11:38+0100\n"
"Last-Translator: \n"
"Language-Team: \n"
"Language: de_DE\n"
"MIME-Version: 1.0\n"
"Content-Type: text/plain; charset=UTF-8\n"
"Content-Transfer-Encoding: 8bit\n"
"X-Generator: Poedit 1.8.1\n"
"X-Poedit-SourceCharset: UTF-8\n"
"X-Poedit-KeywordsList: _\n"
"X-Poedit-Basepath: ..\\..\\..\\\n"
"X-Poedit-SearchPath-0: .\n"

#: translation.php:15
msgid "Hello, %s, it is nice to see you today.\n"
msgstr "Hallo, %s, es ist schön dich zu sehen heute.\n"

档案位置

gettext\translation.php
gettext\locale\de_DE\LC_MESSAGES\messages.po
gettext\locale\de_DE\LC_MESSAGES\messages.mo

我还可以尝试使gettext工作吗?

3 个答案:

答案 0 :(得分:1)

我发现虽然PHP报告确实没有正确设置语言环境。 PHP中存在导致此问题的错误:https://bugs.php.net/bug.php?id=66265

因此,gettext始终使用系统默认语言环境进行翻译(在使用我的系统语言环境创建翻译后,它实际上有效)。 相关StackOverflow问题:Gettext will always use system default locale

虽然bug已经关闭但它应该在某种程度上有效但我无法让它运行。 相反,我现在使用的PHP解决方案读取gettext .po文件并且有效:php-gettexthttps://launchpad.net/php-gettext

答案 1 :(得分:0)

我遇到了同样的问题,并通过在启动apache之前设置 LANG 参数找到了解决方案。

我打开命令提示符(以管理员身份),然后:

set LANG=nl_NL
xampp-control

答案 2 :(得分:0)

一整天没有任何工作后,我找到了一个正常工作且简单的 5 行解决方法,我在另一篇文章中对此进行了解释:

GetText doesn't translate my text properly when using dutch