Windows 上的 PHP gettext

发布于 2024-08-05 22:18:42 字数 1037 浏览 4 评论 0原文

有一些关于 gettext 的教程(带有 Poedit)...不幸的是,它主要适用于 UNIX 环境。更不幸的是,我在 Windows XP 上运行 WAMP 服务器(但我正在为 UNIX 环境进行开发),并且没有一个教程可以让 gettext 对我来说正常工作。从手册页来看,它似乎是一个不同的过程Windows环境。我已经尝试了评论中的一些解决方案,但仍然无法使其工作!我花了很多时间在这上面,希望有人能指出我正确的方向,让这件事发挥作用! (我确信还有其他人和我一样感到沮丧。)到目前为止,通过我的设置,我只得到输出“Hello World!”而我应该得到翻译后的字符串。

到目前为止,这是我的设置/代码:

// test.php
if (!defined('LC_MESSAGES')) {
    define('LC_MESSAGES', 6);
}

$locale = "deu_DEU"; // apparently the locales are different on a WINDOWS platform

putenv("LC_ALL=$locale");
setlocale(LC_ALL, $locale);
bindtextdomain("greetings", ".\locale");
textdomain("greetings");

echo _("Hello World"); 

文件夹结构

root:     C:\Program Files\WampServer 2\www
test.php: C:\Program Files\WampServer 2\www\site
.po:      C:\Program Files\WampServer 2\www\site\locale\deu_DEU\LC_MESSAGES\greetings.po
.mo:      C:\Program Files\WampServer 2\www\site\locale\deu_DEU\LC_MESSAGES\greetings.mo

There are some tutorials out there for gettext (with Poedit)... unfortunately, it's mostly for a UNIX environment. And even more unfortunate is that I am running my WAMP server on Windows XP (but I am developing for a UNIX environment) and none of the tutorials can get gettext working properly for me. From the manual page, it appears that it's a different process on a Windows environment. I've tried out some of the solutions in the comments but I still can't get it to work! I've spent many hours on this, hopefully someone can point me in the right direction to get this thing to work! (And I'm sure there are others out there who share my frustration.) So far with my setup, I'm only getting output "Hello World!" whereas I should be getting the translated string.

Here is my setup/code so far:

// test.php
if (!defined('LC_MESSAGES')) {
    define('LC_MESSAGES', 6);
}

$locale = "deu_DEU"; // apparently the locales are different on a WINDOWS platform

putenv("LC_ALL=$locale");
setlocale(LC_ALL, $locale);
bindtextdomain("greetings", ".\locale");
textdomain("greetings");

echo _("Hello World"); 

Folder structure

root:     C:\Program Files\WampServer 2\www
test.php: C:\Program Files\WampServer 2\www\site
.po:      C:\Program Files\WampServer 2\www\site\locale\deu_DEU\LC_MESSAGES\greetings.po
.mo:      C:\Program Files\WampServer 2\www\site\locale\deu_DEU\LC_MESSAGES\greetings.mo

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

衣神在巴黎 2024-08-12 22:18:42

我在本地计算机上使用 gettext 时遇到了问题,经过一番搜索后,我发现这个页面解决了我的问题:
http://www.kipras。 com/blog/getting-gettext-to-work-in-apache-on-windows/96

我从网页引用:

在 Linux 服务器(或任何其他服务器
除了窗户),这样做的方法是
这个:

setlocale(LC_MESSAGES, “en_US”);

设置位置的正确方法
窗户是这样的:

putenv(“LC_ALL=en_US”);

I ran into problems while working with gettext on my local machine, and after some search i found this page which solved my problem:
http://www.kipras.com/blog/getting-gettext-to-work-in-apache-on-windows/96

I am quoting from the web page:

On Linux servers (or any other servers
apart windows), the way to do it is
this:

setlocale(LC_MESSAGES, “en_US”);

The correct way to set locality on
windows is this:

putenv(“LC_ALL=en_US”);
紫罗兰の梦幻 2024-08-12 22:18:42

我也遇到了同样的问题,浪费了差不多一天的时间。最后,我找到了一个非常简单的解决方案,即卸载 WAMP Server(版本 2.1),然后安装更新版本(WampServer 2.1e - 32 位)。虽然很奇怪,但是彻底解决了问题。

这是对我有用的示例代码:

<?php

    $locale = "deu_DEU";

    putenv("LC_ALL=$locale");
    setlocale(LC_ALL, $locale);

    bindtextdomain("greetings", "./locale");
    textdomain("greetings");

    echo _("Hello World");

?>

希望有帮助。

I had the same problem, and wasted almost a day or so on it. Finally, I found a very simple solution, namely to uninstall WAMP Server (version 2.1), and install a newer version (WampServer 2.1e - 32 bits). It's strange, but it solved the problem completely.

Here is the sample code that worked for me:

<?php

    $locale = "deu_DEU";

    putenv("LC_ALL=$locale");
    setlocale(LC_ALL, $locale);

    bindtextdomain("greetings", "./locale");
    textdomain("greetings");

    echo _("Hello World");

?>

Hope it helps.

海未深 2024-08-12 22:18:42

我没有对此进行全面调查,但我确信您的错误之一是您使用了无效的区域设置代码 - 即使在 Windows 上,区域设置代码也是相同的 - 这些是 gettext 区域设置,并且它们是跨平台的。

请尝试仅使用“de”作为区域设置代码,它应该可以工作。还要确保您的 PHP 已安装并激活 gettext 扩展(使用 phpinfo 检查)。

I didn't made a full investigation on this but I'm sure that one of your mistakes is that you used invalid locale codes - even on Windows the locales codes are the same - these are gettext locales and they are cross platform.

Please try to use just "de" as locale code, it should work. Also be sure that your PHP has gettext extension installed and activated (check with phpinfo).

山人契 2024-08-12 22:18:42

可能为时已晚,但我遇到了类似的问题,直到我在 WAMP php 设置中启用“短开放标记”

Might be too late but I was having similar issues until I enabled "short open tag" in my WAMP php settings

调妓 2024-08-12 22:18:42

这是对我有用的解决方案。这适用于最新的 wampserver。 (来源:http://www.extradrm.com/blog/?p=1035 )

1) 从这里下载 php-gettext: https://launchpad.net/php-gettext/+下载并解压

2) 添加在 test-language.php 所在文件夹中的包根目录中找到的以下文件:
- gettext.inc
- 获取文本.php
- Streams.php

3) 打开 php.ini 并注释掉 wampserver php_gettext.dll:

;extension=php_gettext.dll

4) 这是新的测试文件 test-language.php

<?php
error_reporting(E_ALL | E_STRICT);

// define constants
define('PROJECT_DIR', realpath('./'));
define('LOCALE_DIR', 'C:/wamp/www/test/locale');
define('DEFAULT_LOCALE', 'es_ES');

require_once('gettext.inc');

$supported_locales = array('en_US', 'sr_CS', 'de_CH','es_ES');
$encoding = 'UTF-8';

$locale = (isset($_GET['lang']))? $_GET['lang'] : DEFAULT_LOCALE;

//var_dump($locale);die();

// gettext setup
T_setlocale(LC_MESSAGES, $locale);
// Set the text domain as 'messages'
$domain = 'messages';
bindtextdomain($domain, LOCALE_DIR);
// bind_textdomain_codeset is supported only in PHP 4.2.0+
if (function_exists('bind_textdomain_codeset'))
bind_textdomain_codeset($domain, $encoding);
textdomain($domain);

echo gettext("HELLO_WORLD");
?>

在完成所有这些之后,您必须创建 locale 文件夹、en_US(或其他语言)文件夹、LC_MESSAGES 文件夹,然后放入 messages.po 文件。

This is the solution that worked for me. This works on the latest wampserver. (source: http://www.extradrm.com/blog/?p=1035 )

1) Download php-gettext from here: https://launchpad.net/php-gettext/+download and unzip it

2) Add the following files found in the package root in the same folder as test-language.php :
- gettext.inc
- gettext.php
- streams.php

3) Open your php.ini and comment out wampserver php_gettext.dll:

;extension=php_gettext.dll

4) This is the new test file test-language.php

<?php
error_reporting(E_ALL | E_STRICT);

// define constants
define('PROJECT_DIR', realpath('./'));
define('LOCALE_DIR', 'C:/wamp/www/test/locale');
define('DEFAULT_LOCALE', 'es_ES');

require_once('gettext.inc');

$supported_locales = array('en_US', 'sr_CS', 'de_CH','es_ES');
$encoding = 'UTF-8';

$locale = (isset($_GET['lang']))? $_GET['lang'] : DEFAULT_LOCALE;

//var_dump($locale);die();

// gettext setup
T_setlocale(LC_MESSAGES, $locale);
// Set the text domain as 'messages'
$domain = 'messages';
bindtextdomain($domain, LOCALE_DIR);
// bind_textdomain_codeset is supported only in PHP 4.2.0+
if (function_exists('bind_textdomain_codeset'))
bind_textdomain_codeset($domain, $encoding);
textdomain($domain);

echo gettext("HELLO_WORLD");
?>

After all of this you must create locale folder, en_US ( or other language ) folder, LC_MESSAGES folder and then put the messages.po file.

月光色 2024-08-12 22:18:42

这是因为我没有安装语言环境。

It was because I didn't have the locales installed.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文