在多语言网站中使用 PHP Gettext 扩展与 PHP 数组?

发布于 2024-08-23 08:35:32 字数 1248 浏览 12 评论 0原文

到目前为止,我所看到的使用 gettext 而不是数组的唯一两个好处是我不必创建“问候语”“子数组”(或任何其名称)。而且我不必为“默认语言”创建文件夹。

对于多语言网站使用 gettext 和 php 数组还有其他优点和缺点吗?

使用GETTEXT:

spanish/messages.po:

#: test.php:3
msgid "Hello World!"
msgstr "Hola Mundo"

index.php:

<?php echo _("Hello World!"); ?>

index.php?lang=spanish:

<?php echo _("Hello World!"); ?> turns to Hola Mundo

< strong>使用 PHP 数组:

lang.en.php

<?php
$lang = array(
    "greeting" => "Hello World",
);
?>

lang.es.php

<?php
$lang = array(
    "greeting" => "Hola Mundo",
);
?>

index.php:

<?php echo $lang['greeting']; ?> greeting turns to Hello World

index.php php?lang=spanish

<?php echo $lang['greeting']; ?> greeting turns to Hola Mundo

(我首先使用 gettext,但我的共享免费托管 Zymic 不支持它。我不想使用 Zend_translate,我发现它对于我的简单任务来说太复杂了,所以我最后最终使用了 php define,但后来有人告诉我我应该使用数组)

So far the only 2 good things that I've seen about using gettext instead of arrays is that I don't have to create the "greeting" "sub-array" (or whatever its called). And I don't have to create a folder for the "default language".

Are there other pros and cos of using gettext and php arrays for multilingual websites?

USING GETTEXT:

spanish/messages.po:

#: test.php:3
msgid "Hello World!"
msgstr "Hola Mundo"

index.php:

<?php echo _("Hello World!"); ?>

index.php?lang=spanish:

<?php echo _("Hello World!"); ?> turns to Hola Mundo

USING PHP ARRAYS:

lang.en.php

<?php
$lang = array(
    "greeting" => "Hello World",
);
?>

lang.es.php

<?php
$lang = array(
    "greeting" => "Hola Mundo",
);
?>

index.php:

<?php echo $lang['greeting']; ?> greeting turns to Hello World

index.php?lang=spanish

<?php echo $lang['greeting']; ?> greeting turns to Hola Mundo

(I first started with gettext, but it wasn't supported in my shared free hosting Zymic. I didn't want to use Zend_translate, I found it too complicated to my simple task, so I finally ended up using php define, but later on someone told me I should use arrays)

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

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

发布评论

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

评论(5

风透绣罗衣 2024-08-30 08:35:32

我建议使用 gettext,我在 PHP 中这样做了 5 年,效果很好。

首先,如果您使用 echo _('my text to translate') 并且没有翻译,您将在输出中看到原始字符串,这很好。使用像 echo $translation['were is my translation'] 这样的数组,但没有,你什么也看不到。但要注意,使用 poedit 并执行 echo _(''); 并不是一个好主意,poedit 使用 msgid "" 来获取您很可能不知道的项目信息不想向你的观众展示,所以必须注意不要尝试翻译空字符串:)

而且它非常快,并且具有一些用于复数和其他内容的附加功能,例如,poedit 通过拥有翻译数据库使生活更轻松,所以您不得一遍又一遍地翻译相同的内容,您已经翻译过的内容将被预先填写并标记为“检查是否正确”。很舒服。

theby middus 提到了缺点,你必须编译 po 文件,而使用数组时你可以轻松覆盖 php 文件 - 好吧,你也只需覆盖你的 mo 文件,如果使用 poedit,它会在保存文件后进行编译。所以事实上你点击保存并复制文件,就像编辑 php 文件一样。

但真正的缺点是,如果您使用 mod_php。请注意,对于 mod_php 它不是线程安全的,尽管我从未遇到过任何困难问题。

主要是您在使用 mod_php 时必须重新启动 apache,否则 gettext 调用将失败(有时没有返回翻译,有时您最终会得到心爱的没有内容的白色页面)。但是通过使用像 mod_itk 这样的东西(我相信 cgi/fastcgi 可以做到这一点),你甚至不会再遇到这个问题了。

I recommend using gettext, I am doing that in PHP for 5 years with good results.

First of all if you use echo _('my text to translate') and have no translation for it, you will see the original string in the output, which is good. Using arrays like echo $translation['were is my translation'] and there is none, you will just see nothing. but beware, using poedit and doing a echo _(''); is not a good idea, poedit uses the msgid "" for the project information which most likely you don't want to show your audience, so one must take care of not trying to translate empty strings :)

Also it's very fast and has some additional features for plurals and stuff, also poedit for example makes the life easier by having a translation db so you must not translate the same stuff over and over again, those you did already will be prefilled and marked as "check if it's right". Very comfortable.

theby middus mentioned downside that you have to compile the po file while you could easy overwrite a php-file when using arrays - well you just overwrite your mo file too, and if using poedit, it does the compiling after saving the file. So in fact you hit save and copy the file, sam as with editing a php-file.

But a real downside is, if you use mod_php. Be aware that with mod_php it is not threadsafe, though I never had any hard problems.

It's mostly just that you have to restart your apache when using mod_php or your gettext calls will fail (sometimes no translations returned, sometimes you end up with the beloved white page with no contents). But by using something like mod_itk (I believe cgi/fastcgi can do this to) you wont even have this problem no more.

戈亓 2024-08-30 08:35:32

使用 GNU gettext,您可以获得对占位符的支持,例如 printf 和国际复数形式。占位符顺序可以在翻译中更改或跳过。

PHP 文档中的示例:


<?php
setlocale(LC_ALL, 'cs_CZ');
printf(ngettext("%d window", "%d windows", 1), 1); // gives "1 okno"
printf(ngettext("%d window", "%d windows", 2), 2); // gives "2 okna"
printf(ngettext("%d window", "%d windows", 5), 5); // gives "5 oke"
?>

另一个优点是您可以使用标准工具进行术语管理、翻译记忆库和机器翻译,如 @middus 所指出的。

对于共享环境,Danilo Segan 提供了一个很棒的 php-gettext 库

Using GNU gettext you get support for placeholders like with printf and international plural forms. Placeholders order can be changed in translation or skipped.

Example from PHP documentation:


<?php
setlocale(LC_ALL, 'cs_CZ');
printf(ngettext("%d window", "%d windows", 1), 1); // gives "1 okno"
printf(ngettext("%d window", "%d windows", 2), 2); // gives "2 okna"
printf(ngettext("%d window", "%d windows", 5), 5); // gives "5 oke"
?>

Another pro is that you can use standard tools for terminology management, translations memory and machine translation as pointed by @middus.

For shared environments there is a great php-gettext library from Danilo Segan.

携君以终年 2024-08-30 08:35:32

使用 gettext() 最明显的优点当然是源字符串位于它所属的位置。也就是说,写这个比更有意义

echo _("This is a string");

,更不用说

echo $lang['a_string'];

你必须为每个可能的翻译制作一个新的变量占位符。对于 gettext(),翻译字符串本身充当索引。

The most obvious pro for using gettext() is of course that the source string is positioned where it belongs. I.e. it makes much more sense to write this

echo _("This is a string");

than

echo $lang['a_string'];

Not to mention that you have to craft a new variable placeholder for every possible translation. With gettext() the translation string itself acts as the index.

晨曦÷微暖 2024-08-30 08:35:32

在我看来,对于像 php 这样的动态语言使用二进制格式(gettext 的 .mo 文件)没有太大意义。

然而,gettext 的优点是存在一个巨大的工具生态系统,您的软件翻译人员可以使用这些工具(例如 Poedit )。缺点是,在部署之前,您始终必须将 .po 编译为 .mo,而您可以动态替换包含数组的 php 文件。

另一个缺点是,正如您已经认识到的那样,它并非在所有 php 安装上都可用。

In my opinion it does not make too much sense to use a binary format (gettext's .mo files) for a dynamical language such as php.

However, a pro of gettext is the existence of a huge ecosystem of tools that translators of your software are able to use (e.g. Poedit). Downside is that you always have to compile your .po to .mo before you deploy it while you can just replace a php file containing an array on the fly.

Another con is, as you've already recognized, it is not available on all installations of php.

镜花水月 2024-08-30 08:35:32

当用户从下拉列表或任何区域选择任何语言时,然后将所选语言 ID 保存在会话中,例如

$_SESSION['language']=1;

现在根据会话中存储的语言 ID 从数据库表“内容”中获取数据。

更多详细信息请参见

http://skillrow.com/multilingual-website-in-php-2 /

When user selects any language from dropdown or any area then save selected language id in session like,

$_SESSION['language']=1;

Now fetch data from database table ‘content’ based on language id stored in session.

More detail here

http://skillrow.com/multilingual-website-in-php-2/

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