【问题标题】:Gettext example not working on Windows 7 - XAMPPGettext 示例在 Windows 7 上不起作用 - XAMPP
【发布时间】:2015-06-01 09:53:53
【问题描述】:

我有最新的 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 正常工作?

【问题讨论】:

    标签: php localization gettext


    【解决方案1】:

    我发现语言环境设置不正确,尽管 PHP 报告它确实如此。 PHP 中有一个错误导致此问题:https://bugs.php.net/bug.php?id=66265

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

    虽然该错误已关闭并且它应该可以部分工作,但我无法让它运行。 相反,我现在使用的是读取 gettext .po 文件并且有效的 PHP 解决方案:php-gettext (https://launchpad.net/php-gettext)

    【讨论】:

    【解决方案2】:

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

    我打开一个命令提示符(以管理员身份),下一步:

    set LANG=nl_NL
    xampp-control
    

    【讨论】:

    • 在 'set LANG=nl_NL' 和 'xampp-control' 之间有一个新行 :)
    • 补充一点,如果您已将 XAMPP shell 安装在默认目录中,则可以在 C:\xampp\xampp-shell.bat 中找到它。在执行此操作之前,您还需要退出(即右键单击 > 退出)所有现有的 XAMPP 实例。我花了几天时间在 Windows 和 Ubuntu 上对 gettext 进行故障排除以使它们正常工作,并且我在这里记录了所有对我有用的东西:blog.terresquall.com/2020/09/troubleshooting-php-gettext
    • 不幸的是,这对我在 Windows 上的 xampp 64 不起作用:-(
    • 我找到了一个完全不同的解决方法。 stackoverflow.com/questions/62353692/…
    【解决方案3】:

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

    GetText doesn't translate my text properly when using dutch

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-09-11
      • 1970-01-01
      • 1970-01-01
      • 2011-12-04
      • 2012-06-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多