我一直听说过 gettext - 我知道它是某种 unix 命令,用于根据提供的字符串参数查找翻译,然后生成一个 .pot 文件,但有人可以用外行的术语向我解释这是如何处理的一个网络框架?
我可能会四处看看一些已建立的框架是如何做到的,但外行的解释会有所帮助,因为它可能有助于在我真正深入研究以提供我自己的解决方案之前更清楚地了解情况。
我一直听说过 gettext - 我知道它是某种 unix 命令,用于根据提供的字符串参数查找翻译,然后生成一个 .pot 文件,但有人可以用外行的术语向我解释这是如何处理的一个网络框架?
我可能会四处看看一些已建立的框架是如何做到的,但外行的解释会有所帮助,因为它可能有助于在我真正深入研究以提供我自己的解决方案之前更清楚地了解情况。
gettext 系统从一组二进制文件中回显字符串,这些二进制文件由源文本文件创建,其中包含同一句子的不同语言的翻译。
查找键是“基础”语言中的句子。
在您的源代码中,您将拥有类似
echo _("Hello, world!");
对于每种语言,您将有一个相应的文本文件,其中包含密钥和翻译版本(注意可与 printf 函数一起使用的 %s )
french
msgid "Hello, world!"
msgstr "Salut, monde!"
msgid "My name is %s"
msgstr "Mon nom est %s"
italian
msgid "Hello, world!"
msgstr "Ciao, mondo!"
msgid "My name is %s"
msgstr "Il mio nome è %s"
这些是创建本地化所需的主要步骤
locale/de_DE/LC_MESSAGES/myPHPApp.mo
locale/en_EN/LC_MESSAGES/myPHPApp.mo
locale/it_IT/LC_MESSAGES/myPHPApp.mo
那么你的 php 脚本必须设置需要使用的语言环境
php手册中的示例对于该部分非常清楚
<?php
// Set language to German
setlocale(LC_ALL, 'de_DE');
// Specify location of translation tables
bindtextdomain("myPHPApp", "./locale");
// Choose domain
textdomain("myPHPApp");
// Translation is looking for in ./locale/de_DE/LC_MESSAGES/myPHPApp.mo now
// Print a test message
echo gettext("Welcome to My PHP Application");
// Or use the alias _() for gettext()
echo _("Have a nice day");
?>
总是从 php 手册中寻找一个好的教程