如何自动制作“wp_set_script_translations"功能? [英] How to make work 'wp_set_script_translations' function automatically?
问题描述
我正在尝试使 'wp_set_script_translations' 与 wpml 结合使用主题.
I'm trying to make 'wp_set_script_translations' work with theme in conjunction with wpml.
我已经实施了 Wordpress Codex 的所有建议.WPML 插件在\wp-content\languages\wpml"文件夹中以locale-domain-handle.json"格式生成文件.但是文件生成时没有翻译,并且使用错误的域名消息"而不是我指定的域.我还检查了 WPML 字符串翻译文件,似乎 JED 函数生成了错误的 json,该函数始终设置消息"域并忽略客户域名.
I had implemented all the recommendations from Wordpress Codex. WPML plugin generates file in format "locale-domain-handle.json" in "\wp-content\languages\wpml" folder. But file generates without translations and with wrong domain name "messages" instead domain that I specified. Also I checked WPML String Translation files and it seems that wrong json generates by JED function that always sets "message" domain and ignores customer domain name.
{
"translation-revision-date": "2019-04-23 08:33:37+0000",
"generator": "WPML String Translation 2.10.2",
"domain": "messages",
"locale_data": {
"messages": {
"": {
"domain": "messages",
"plural-forms": "nplurals=2; plural=n != 1;",
"lang": "fr_FR"
}
}
}
}
我希望在文件中看到具有正确域名的翻译,或者无需手动生成翻译文件的替代和自动化方法.
I would like to see the translations with right domain name in the file or alternative and automated method without manual generation of translation files.
推荐答案
翻译终于得到了适当的开发和记录:https://developer.wordpress.org/block-editor/developers/internationalization/
Translation has finally been properly developed and documented: https://developer.wordpress.org/block-editor/developers/internationalization/
基本上,您需要:
- 使用 wp-i18n 包来定义项目中哪些是可翻译的字符串.
- 编译您的代码(
wp i18n make-pot
将在您编译的代码中搜索翻译字符串,而不是在您的源代码中). - 使用 WP-CLI (
wp i18n make-pot
) 创建.pot
文件,就像我们一直使用的 WP 主题和插件一样. - 根据我们之前创建的
.pot
生成一个.po
文件并翻译其内容. - 再次使用 WP-CLI (
wp i18n make-json
) 将我们的.po
文件转换为所需的 JSON 格式. - 使用
wp_set_script_translations
PHP 函数加载翻译文件.
- Use the wp-i18n package to define which are the strings in your project to be translatable.
- Compile your code (
wp i18n make-pot
will search for translation strings in your compiled code, not in your source). - Use the WP-CLI (
wp i18n make-pot
) to create a.pot
file, just like we've always used with WP themes and plugins. - Generate a
.po
file based on our previously created.pot
and translate its content. - Use the WP-CLI again (
wp i18n make-json
) to convert our.po
file to the JSON format needed. - Use the
wp_set_script_translations
PHP function to load the translation files.
希望这可以帮助那里的人;)
Hope this helps someone out there ;)
这篇关于如何自动制作“wp_set_script_translations"功能?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!