简体   繁体   English

PHP-在Windows 10和Xamp Server上使用gettext()

[英]PHP - using gettext() on windows 10 and xamp server

I've been trying to use this gettext functionality to add localization functionality to my website. 我一直在尝试使用此gettext功能向我的网站添加本地化功能。 Please keep in mind I'm not a webdev and this field is rather unknow to me. 请记住,我不是Webdev,并且这个字段对我而言是未知的。 After following several tutorials I have installed the extension and have enabled it in the php.ini. 在遵循了几个教程之后,我安装了扩展并在php.ini中启用了它。 I'm also using PoEdit for creating the .po files. 我还使用PoEdit创建.po文件。 My php code looks like this : 我的PHP代码看起来像这样:

<?php
$language = "en_US";
putenv("LC_ALL={$locale}");

$domain = 'messages';
bindtextdomain($domain, './locale'); 
bind_textdomain_codeset($domain, 'UTF-8'); 
textdomain($domain); 
?>

Then I call it like this : 然后我这样称呼它:

<p class="welcome"><?=_('welcome');?></p>

As my understanding goes "_" is an alieas for the gettext method. 就我的理解而言,“ _”是gettext方法的别名。 Both dont seem to work. 两者似乎都不起作用。 The messages .po file looks like this : 消息.po文件如下所示:

msgid ""
msgstr ""
"Project-Id-Version: \n"
"POT-Creation-Date: 2017-04-23 11:27+0300\n"
"PO-Revision-Date: 2017-04-23 11:36+0300\n"
"Language-Team: \n"
"MIME-Version: 1.0\n"
"Content-Type: text/plain; charset=UTF-8\n"
"Content-Transfer-Encoding: 8bit\n"
"X-Generator: Poedit 2.0.1\n"
"X-Poedit-Basepath: ../../..\n"
"Plural-Forms: nplurals=2; plural=(n != 1);\n"
"Last-Translator: \n"
"Language: en\n"
"X-Poedit-SearchPath-0: index.php\n"

#: index.php:63
msgid "welcome"
msgstr "Welcome"

#: index.php:64
msgid "to-leadersplay"
msgstr "to Leadersplay"

My directory structure looks like this : 我的目录结构如下所示:

在此处输入图片说明

NO matter what I do the strings that are being displayed seem to be the keys and not the translations from the file. 无论我做什么,显示的字符串似乎都是键,而不是文件的翻译。 In my current example I get welcome but I expect to get Welcome (with a capital W) 在当前示例中,我受到欢迎,但我希望受到欢迎(大写字母W)

I think the problem is that 我认为问题是

$language = "en_US";
putenv("LC_ALL={$locale}");

should be 应该

$locale = "en_US";
putenv("LC_ALL={$locale}");

instead. 代替。

Because after this change the snippet works just fine for me. 因为在进行此更改后,该代码段对我来说效果很好。

see my answer @ Gettext example not working on Windows 7 - XAMPP 看到我的答案@ Gettext示例在Windows 7上不起作用-XAMPP

In Windows gettext looks at the 'LANG' variable which must be set before Apache starts, 在Windows中,gettext查看必须在Apache启动之前设置的'LANG'变量,

set LANG=nl_NL
xampp-control

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM