奇怪的 gettext 行为,值存储在变量中
我有一些如下代码:
<?php $subtitle = _('Products'); ?>
后来我输出了 $subtitle 的值,
<h2><?php echo $subtitle; ?></h2>
我本以为它会输出 Products 的翻译值,但它输出的是英文值。如果我将输出值的部分更改为以下内容,则它可以正常工作。
<h2><?php echo _($subtitle); ?></h2>
谁能解释为什么会发生这种情况?我假设 $subtitle 中的值是翻译后的值,但事实似乎并非如此。
I have some code like the following:
<?php $subtitle = _('Products'); ?>
Later on I output the value of $subtitle
<h2><?php echo $subtitle; ?></h2>
I would have thought that it would output the translated value of Products, but instead it outputs the English value. If I change the section where I output the value to the following, it works correctly.
<h2><?php echo _($subtitle); ?></h2>
Can anyone explain why this is happening? I had assumed that the value in $subtitle would be the translated value, but it does not appear to be the case.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我发现了问题所在,这是非常愚蠢的。在许多页面上,$subtitle 分配是在设置语言环境之前进行的。我没有注意到,因为设置区域设置的代码位于包含的公共头文件中。
I discovered what the problem was, and it was incredibly stupid. On many of the pages, the $subtitle assignment was taking place before the locale was set. I didn't notice because the code where the locale is set is in an included common header file.