How to run or load .po/.mo files for localization in php(如何在 php 中运行或加载 .po/.mo 文件以进行本地化)
问题描述
我已经安装 poedit 并通过它运行我的文件,它会为它们创建 .po 和 .mo 文件.但是我在加载和使用这些文件来翻译我的文本时遇到了问题.我不知道如何加载或打开翻译文件并显示翻译内容.任何人都可以帮我解决这个问题.我尝试了所有可能的来源,但没有成功.
I have installed poedit and running my file through it, it creates .po and .mo files for them. But I have a problem to load and use these files for translating my text. I don't know how to load or open the translated files and to show the translated content. Can anyone help me about this. I tried every possible source but not succeeded.
推荐答案
首先你需要告诉 PHP 你正在使用哪个语言环境和域.
First of all you need to inform PHP which locale and domain you are using.
putenv("LANG=da_DK");
setlocale('LC_ALL', "da_DK");
bindtextdomain("mycatalog", "./locale/");
textdomain("mycatalog");
在这种情况下,我有一个丹麦语翻译和一个名为 mycatalog.mo(和 .po)的文件.这些文件(从您的根目录)放置在此处:locale/da_DK/LC_MESSAGES/mycatalog.mo/po
In this case I'm having a Danish translation and a file called mycatalog.mo (and .po). These files are placed (from your root) here: locale/da_DK/LC_MESSAGES/mycatalog.mo/po
为了显示您的翻译,您将这样做:
In order to show your translation, you will do this:
echo _("Hello world"); // Which would become "Hej verden"
_();是 gettext() 的别名;gettexts 的聪明之处在于,如果没有翻译,您的 UI 中就不会出现像MSG_HELLO_WORLD"这样难看的语言代码,而是一个更好的选择:简单的纯英文文本.
_(); is an alias of gettext(); The smart thing about gettexts is that if there's no translation you will not have an ugly language code like "MSG_HELLO_WORLD" in your UI, but instead a better alternative: Simply the plain English text.
在 messages.po 文件中,您必须在此表单上包含所有消息(区分大小写以及使用的逗号、点、冒号等):
In the messages.po file you must have all the messages (case-sensitive and also with respect to used commas, dots, colons, etc.) on this form:
msgid "Hello world!"
msgstr "Hej verden!"
当你把它添加到你的 .po 文件中后,你在 poedit 中打开这个文件,点击保存",它会生成一个 .mo 文件.该文件与 .po 文件上传到同一目录(通常类似于脚本根目录中的 localeda_DKLC_MESSAGES)
When you have added this to your .po file, you open this file in poedit, hit "Save" and it will generate a .mo file. This file is uploaded to the same directory as the .po file (typically something like localeda_DKLC_MESSAGES from the script root)
要翻译动态/可变内容,您可以使用 - 除其他外 - sprintf,以这种方式:
To translate dynamic/variable content you can use - among other things - sprintf, in this manner:
echo sprintf(_("My name is %s"), $name);
在这种情况下,%s 将出现在 .po 文件中;当您拥有翻译后的字符串(包含 %s)时,sprintf 将确保将 %s 替换为变量内容.如果变量也必须翻译,您可以这样做:
In this case the %s will occur in the .po file; When you have the translated string (which contains the %s), sprintf will make sure to replace the %s with the variable content. IF the variable must be translated too, you can do this:
echo sprintf(_("The color of my house is %s"), _($color));
那么你不需要为每一种颜色写一个完整的句子,但你仍然可以翻译颜色.
Then you don't need a full sentence for every color, but you still get the colors translated.
请务必注意,第一次在服务器上运行 .mo 时,它会被缓存 - 并且如果不重新启动就无法从缓存中删除此文件(Apache 或类似的东西本身就足够了).这意味着您在第一次使用 .mo 后对其所做的任何更改都不会生效.有许多技巧可以解决这个问题,但老实说,它们大多不是很漂亮(它们包括复制 .mo,在其后面添加 time(),然后再次导入并缓存它).仅当您不打算一次翻译整个内容而是分块翻译时,最后一段才重要.
It is important to note that the first time a .mo is run on the server it is cached - and there is no way of removing this file from the cache without restarting (Apache or the like itself should be enough). This means that any changes you make to the .mo after the first time it is used, will not be effective. There are a number of hacks to work around this, but honestly, they are mostly not very pretty (they include copying the .mo, add the time() behind it and then import and cache it again). This last paragraph is only of importance if you aren't going to translate the whole thing at once, but in chunks.
如果您想在某个时候创建自己的翻译工具,此工具可帮助您使用 PHP 将 .po 转换为 .mo:
If you want to create your own translation tool at some point, this tool helps you convert .po to .mo using PHP:
http://www.josscrowcroft.com/2011/code/php-mo-convert-gettext-po-file-to-binary-mo-file-php/
这篇关于如何在 php 中运行或加载 .po/.mo 文件以进行本地化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何在 php 中运行或加载 .po/.mo 文件以进行本地化
- 从 PHP 中的输入表单获取日期 2022-01-01
- 没有作曲家的 PSR4 自动加载 2022-01-01
- 带有通配符的 Laravel 验证器 2021-01-01
- 如何定位 php.ini 文件 (xampp) 2022-01-01
- SoapClient 设置自定义 HTTP Header 2021-01-01
- Mod使用GET变量将子域重写为PHP 2021-01-01
- Laravel 仓库 2022-01-01
- Oracle 即时客户端 DYLD_LIBRARY_PATH 错误 2022-01-01
- 正确分离 PHP 中的逻辑/样式 2021-01-01
- PHP Count 布尔数组中真值的数量 2021-01-01