博客
✨ 支持翻译包含数组和对象的复杂元数据(包括 Slim SEO 元数据)
作者:Leonardo Losoviz ·
Gato AI Translations for Polylang 正式发布 v12.2.0,带来全新功能:支持翻译包含数组和对象的元数据(除单个字符串外)。
元字段中现已支持以下数据结构:
- 单个字符串。例如:
"hello world" - 字符串数组。例如:
["hello", "world"] - 对象第一层级的字符串。例如:
{ level1_key1: "hello", level1_key2: "world"}
插件无需任何手动干预即可识别数据结构,并相应地翻译字符串:
- 检测元字段中的数组和对象
- 提取可翻译的字符串
- 翻译它们
- 将结果存入数据库,同时保留原始数据结构
扩展与更多插件的兼容性
此功能为在元字段中存储数组和对象的插件提供支持。
一个典型示例是 Slim SEO——一款将元数据以 JSON 对象形式存储在元键 slim_seo 下的热门 SEO 插件。
例如,某篇文章的元数据在数据库中的存储方式如下:
a:4:{s:5:"title";s:67:"This is the meta title in English, will it be translated my friend?";s:11:"description";s:42:"I hope it will, or else I'm fried!";s:7:"noindex";i:1;}...代表以下 JSON 对象:
{
"title": "This is the meta title in English, will it be translated my friend?",
"description": "I hope it will, or else I'm fried!",
"noindex": 1
}要翻译文章和页面的 Slim SEO 元数据,只需在翻译自定义文章元数据的设置中添加 slim_seo 元键即可:

现在,当翻译包含 Slim SEO 元数据的页面时……

……翻译后的页面将同时包含已翻译的 SEO 元数据:

尽情享用!