Polylang
与 Polylang 插件(以及 Polylang PRO)集成。
GraphQL 模式中提供了用于获取多语言数据的字段。
类型 Root/QueryRoot
查询在 Polylang 中配置的站点元数据。
| 字段 | 描述 |
|---|---|
polylangDefaultLanguage | Polylang 的默认语言,如果没有启用任何语言则为 null。 |
polylangLanguages | Polylang 的语言列表。 |
执行此 Query:
{
polylangDefaultLanguage {
code
}
polylangLanguages {
code
}
}...可能返回:
{
"data": {
"polylangDefaultLanguage": {
"code": "en"
},
"polylangLanguages": [
{
"code": "en"
},
{
"code": "es"
},
{
"code": "fr"
}
]
}
}类型 Post、Page、PostTag、PostCategory 和 Media
查询实体的语言及该实体各翻译版本的 ID。
这些类型实现了接口 PolylangTranslatable。(类型 Media 仅在通过 Polylang 设置启用媒体支持时才适用。)
| 字段 | 描述 |
|---|---|
polylangLanguage | 文章或页面的语言,如果未分配语言则为 null(例如:Polylang 是在之后安装的)。 |
polylangTranslationLanguageIDs | 该实体所有翻译语言的节点,以语言代码为键、实体 ID 为值的 JSON 对象;如果未分配语言则为 null(例如:Polylang 是在之后安装的)。 |
字段 polylangTranslationLanguageIDs 提供所有翻译版本(即文章/页面/分类/标签/媒体)的实体 ID。它接受输入 includeSelf,用于指定是否将被查询实体的 ID 包含在结果中(默认为 false),以及输入 includeLanguages 和 excludeLanguages,用于过滤结果中包含的语言。
执行此 Query:
{
posts {
__typename
id
title
polylangLanguage {
code
}
polylangTranslationLanguageIDs
polylangTranslationLanguageIDsWithSelf: polylangTranslationLanguageIDs(filter: { includeSelf: true })
categories {
__typename
id
name
polylangLanguage {
code
}
polylangTranslationLanguageIDs
polylangTranslationLanguageIDsWithSelf: polylangTranslationLanguageIDs(filter: { includeSelf: true })
}
tags {
__typename
id
name
polylangLanguage {
code
}
polylangTranslationLanguageIDs
polylangTranslationLanguageIDsWithSelf: polylangTranslationLanguageIDs(filter: { includeSelf: true })
}
}
pages {
__typename
id
title
polylangLanguage {
code
}
polylangTranslationLanguageIDs
polylangTranslationLanguageIDsWithSelf: polylangTranslationLanguageIDs(filter: { includeSelf: true })
}
mediaItems {
__typename
id
title
polylangLanguage {
code
}
polylangTranslationLanguageIDs
polylangTranslationLanguageIDsWithSelf: polylangTranslationLanguageIDs(filter: { includeSelf: true })
}
}...可能返回:
{
"data": {
"posts": [
{
"__typename": "Post",
"id": 1668,
"title": "Some post translated using Polylang",
"polylangLanguage": {
"code": "en"
},
"polylangTranslationLanguageIDs": {
"fr": 1670,
"es": 1672
},
"polylangTranslationLanguageIDsWithSelf": {
"en": 1668,
"fr": 1670,
"es": 1672
},
"categories": [
{
"__typename": "PostCategory",
"id": 61,
"name": "Category for Polylang",
"polylangLanguage": {
"code": "en"
},
"polylangTranslationLanguageIDs": {
"fr": 63,
"es": 65
},
"polylangTranslationLanguageIDsWithSelf": {
"en": 61,
"fr": 63,
"es": 65
}
}
],
"tags": [
{
"__typename": "PostTag",
"id": 67,
"name": "Tag for Polylang",
"polylangLanguage": {
"code": "en"
},
"polylangTranslationLanguageIDs": {
"fr": 69,
"es": 71
},
"polylangTranslationLanguageIDsWithSelf": {
"en": 67,
"fr": 69,
"es": 71
}
}
]
}
],
"pages": [
{
"__typename": "Page",
"id": 1674,
"title": "Some page translated using Polylang",
"polylangLanguage": {
"code": "en"
},
"polylangTranslationLanguageIDs": {
"fr": 1676,
"es": 1678
},
"polylangTranslationLanguageIDsWithSelf": {
"en": 1674,
"fr": 1676,
"es": 1678
}
}
],
"mediaItems": [
{
"__typename": "Media",
"id": 40,
"title": "Media-for-Polylang",
"polylangLanguage": {
"code": "en"
},
"polylangTranslationLanguageIDs": {
"fr": 42,
"es": 44
},
"polylangTranslationLanguageIDsWithSelf": {
"en": 40,
"fr": 42,
"es": 44
}
}
]
}
}类型 GenericCustomPost、GenericTag 和 GenericCategory
这些类型实现了接口 PolylangMaybeTranslatable。
GenericCustomPost 是用于表示站点上安装的任意自定义文章类型(如 Portfolio、Event、Product 等)的类型。类似地,GenericTag 和 GenericCategory 分别用于表示其对应的分类法。
这些 CPT 及分类法均可在 Polylang 设置中定义为可翻译。字段 polylangLanguage 和 polylangTranslationLanguageIDs 的行为与 Post 等类型相同(见上文),如果实体的 CPT 或分类法未被配置为可翻译,也会返回 null。
此外,字段 polylangIsTranslatable 表示该 CPT 或分类法是否被配置为可翻译。
| 字段 | 描述 |
|---|---|
polylangLanguage | 文章或页面的语言,如果未分配语言则为 null(例如:Polylang 是在之后安装的),或者如果实体未被配置为可翻译(通过 Polylang 设置)则为 null。 |
polylangTranslationLanguageIDs | 该实体所有翻译语言的节点,以语言代码为键、实体 ID 为值的 JSON 对象;如果未分配语言则为 null(例如:Polylang 是在之后安装的),或者如果实体未被配置为可翻译(通过 Polylang 设置)则为 null。 |
polylangIsTranslatable | 表示实体是否可被翻译。 |
执行此 Query:
{
customPosts(filter: { customPostTypes: ["some-cpt", "another-cpt"] }) {
__typename
...on GenericCustomPost {
id
title
customPostType
polylangIsTranslatable
polylangLanguage {
code
}
polylangTranslationLanguageIDs
polylangTranslationLanguageIDsWithSelf: polylangTranslationLanguageIDs(filter: { includeSelf: true })
categories(taxonomy: "some-category") {
__typename
...on GenericCategory {
id
name
polylangIsTranslatable
polylangLanguage {
code
}
polylangTranslationLanguageIDs
polylangTranslationLanguageIDsWithSelf: polylangTranslationLanguageIDs(filter: { includeSelf: true })
}
}
tags(taxonomy: "some-tag") {
__typename
...on GenericTag {
id
name
polylangIsTranslatable
polylangLanguage {
code
}
polylangTranslationLanguageIDs
polylangTranslationLanguageIDsWithSelf: polylangTranslationLanguageIDs(filter: { includeSelf: true })
}
}
}
}
}...可能返回:
{
"data": {
"customPosts": [
{
"__typename": "GenericCustomPost",
"id": 10,
"title": "Some CPT that has Polylang translation enabled",
"customPostType": "some-cpt",
"polylangIsTranslatable": true,
"polylangLanguage": {
"code": "en"
},
"polylangTranslationLanguageIDs": {
"fr": 12,
"es": 14
},
"polylangTranslationLanguageIDsWithSelf": {
"en": 10,
"fr": 12,
"es": 14
},
"categories": [
{
"__typename": "GenericCategory",
"id": 30,
"name": "Some Category for Polylang",
"polylangIsTranslatable": true,
"polylangLanguage": {
"code": "en"
},
"polylangTranslationLanguageIDs": {
"fr": 32,
"es": 34
},
"polylangTranslationLanguageIDsWithSelf": {
"en": 30,
"fr": 32,
"es": 34
}
}
],
"tags": [
{
"__typename": "GenericTag",
"id": 50,
"name": "Some Tag for Polylang",
"polylangIsTranslatable": true,
"polylangLanguage": {
"code": "en"
},
"polylangTranslationLanguageIDs": {
"fr": 52,
"es": 54
},
"polylangTranslationLanguageIDsWithSelf": {
"en": 50,
"fr": 52,
"es": 54
}
}
]
},
{
"__typename": "GenericCustomPost",
"id": 20,
"title": "Another CPT that does not have Polylang translation enabled",
"customPostType": "another-cpt",
"polylangIsTranslatable": false,
"polylangLanguage": null,
"polylangTranslationLanguageIDs": null,
"polylangTranslationLanguageIDsWithSelf": null,
"categories": [
{
"__typename": "GenericCategory",
"id": 70,
"name": "Category without support for Polylang",
"polylangIsTranslatable": false,
"polylangLanguage": null,
"polylangTranslationLanguageIDs": null,
"polylangTranslationLanguageIDsWithSelf": null
}
],
"tags": [
{
"__typename": "GenericTag",
"id": 72,
"name": "Tag without support for Polylang",
"polylangIsTranslatable": false,
"polylangLanguage": null,
"polylangTranslationLanguageIDs": null,
"polylangTranslationLanguageIDsWithSelf": null
}
]
}
]
}
}Mutations
GraphQL 模式中提供了用于以下操作的 mutation:
- 设置自定义文章、标签、分类及媒体项的语言,以及
- 定义它们之间的关联关系(即表明一组自定义文章、标签、分类或媒体项互为彼此的翻译版本)。
| Mutation | 描述 |
|---|---|
polylangSetCustomPostLanguage | 设置自定义文章的语言。 |
polylangSetTaxonomyTermLanguage | 设置分类法术语的语言。 |
polylangSetMediaItemLanguage | 设置媒体项的语言。 |
polylangSaveCustomPostTranslationAssociation | 设置自定义文章的翻译关联。 |
polylangSaveTaxonomyTermTranslationAssociation | 设置分类法术语的翻译关联。 |
polylangSaveMediaItemTranslationAssociation | 设置媒体项的翻译关联。 |
例如,以下 Query 为 3 篇文章设置语言(英语、西班牙语和法语),然后定义这 3 篇文章互为彼此的翻译:
mutation {
post1: polylangSetCustomPostLanguage(input: {id: 1, languageBy: { code: "en" }}) {
status
errors {
__typename
...on ErrorPayload {
message
}
}
}
post2: polylangSetCustomPostLanguage(input: {id: 2, languageBy: { code: "es" }}) {
status
errors {
__typename
...on ErrorPayload {
message
}
}
}
post3: polylangSetCustomPostLanguage(input: {id: 3, languageBy: { code: "fr" }}) {
status
errors {
__typename
...on ErrorPayload {
message
}
}
}
polylangSaveCustomPostTranslationAssociation(input: {
ids: [1, 2, 3]
}) {
status
errors {
__typename
...on ErrorPayload {
message
}
}
}
}按语言过滤数据
在获取以下数据时,可以提供语言进行过滤:
- 文章(Posts)
- 页面(Pages)
- 自定义文章(Custom posts)
- 分类(Categories)
- 标签(Tags)
- 媒体项(Media items)
对应字段接受输入 polylangLanguageBy,可按代码或语言区域进行过滤,支持指定 1 种或多种语言。
例如,传入 $languageCodes: ["es"] 将获取西班牙语的数据:
query FilterByLanguage($languageCodes: [String!])
{
posts(filter: {
polylangLanguagesBy: { codes: $languageCodes }
}) {
id
title
}
pages(filter: {
polylangLanguagesBy: { codes: $languageCodes }
}) {
id
title
}
customPosts(filter: {
customPostTypes: ["some-cpt"]
polylangLanguagesBy: { codes: $languageCodes }
}) {
id
title
}
postCategories(filter: {
polylangLanguagesBy: { codes: $languageCodes }
}) {
id
name
}
postTags(filter: {
polylangLanguagesBy: { codes: $languageCodes }
}) {
id
name
}
categories(
taxonomy: "some-category"
filter: { polylangLanguagesBy: { codes: $languageCodes } }
) {
id
name
}
tags(
taxonomy: "some-tag"
filter: { polylangLanguagesBy: { codes: $languageCodes } }
) {
id
name
}
mediaItems(filter: {
polylangLanguagesBy: { codes: $languageCodes }
}) {
id
title
}
}