[英]PHP gettext translations not getting picked up
So I scoured stackoverflow with similar problems and checked everything.所以我用类似的问题搜索了stackoverflow并检查了所有内容。 I just cannot get translations to work with
gettext
.我只是无法让翻译与
gettext
一起使用。
I have the following file structure我有以下文件结构
locale/
en_US/
LC_MESSAGES/
messages.mo
messages.po
test.php
messages.po contains multiple messages, but only one is translated. messages.po 包含多条消息,但只有一条被翻译。 I ran
msgfmt
to generate the.mo file from.po file.我运行
msgfmt
从 .po 文件生成 .mo 文件。
#: test.php:43
msgid "Olen omena"
msgstr "I am an apple"
Trying to run test.php with the following code:尝试使用以下代码运行 test.php:
<?php
$locale = "en_US";
$results = putenv("LANG=" . $locale);
if (!$results) {
exit ('putenv LANG failed');
}
$results = putenv("LANGUAGE=" . $locale);
if (!$results) {
exit ('putenv LANGUAGE failed');
}
$results = setlocale(LC_MESSAGES, $locale);
if (!$results) {
exit ('setlocale failed');
}
echo "Locale set to: $locale<br>";
$domain = 'localhost';
$results = bindtextdomain($domain, "locale/");
if (!$results) {
exit ('bindtextdomain failed');
}
$results = bind_textdomain_codeset($domain, "ISO-8859-1");
if (!$results) {
exit ('bind_textdomain_codeset failed');
}
$results = textdomain($domain);
if (!$results) {
exit ('textdomain failed');
}
echo _("Olen omena");
And it prints它打印
Locale set to: en_US
Olen omena
Apache and PHP run inside a Docker container and I checked with locale -a
that en_US exists. Apache 和 PHP 在 Docker 容器内运行,我使用
locale -a
检查了 en_US 存在。
...
en_US
en_US.iso885915
en_US.utf8
Tried all en_US options.尝试了所有 en_US 选项。 Tried LC_ALL and LC_MESSAGES.
尝试了 LC_ALL 和 LC_MESSAGES。 Tried different domains... At a loss here.
尝试了不同的域......在这里不知所措。 What exactly is wrong?
究竟是什么问题?
Alright.好吧。 I figured it out by looking at PHP.net example!
我通过查看 PHP.net 示例弄清楚了! Domain must be the same as the file name!
域必须与文件名相同! If domain is set to 'localhost' file must be named 'localhost.mo'!
如果域设置为“localhost”,文件必须命名为“localhost.mo”!
Edit I did however observe strange behaviour.编辑我确实观察到了奇怪的行为。 For whatever reason PHP keeps resetting the translation every 7th refresh and the original text pops up.
无论出于何种原因,PHP 每 7 次刷新一次都会重置翻译,并且会弹出原始文本。 Observing the network tab in chrome the initial page load is above 400 B, second is 399 until the 7th is 395:
观察 chrome 中的网络选项卡,初始页面加载高于 400 B,第二个是 399,直到第 7 个是 395:
Does PHP reset some cache every 7th reload? PHP 每 7 次重新加载一次是否会重置一些缓存?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.