Polylang
การผสานรวมกับปลั๊กอิน Polylang (และ Polylang PRO ด้วย)
GraphQL schema มีฟิลด์สำหรับดึงข้อมูลหลายภาษา
ประเภท Root/QueryRoot
ดึงข้อมูล metadata ของเว็บไซต์ที่กำหนดค่าไว้ใน 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 ของคำแปลสำหรับเอนทิตีนั้น
ประเภทเหล่านี้ implement อินเทอร์เฟซ PolylangTranslatable (ประเภท Media จะทำเช่นนี้เฉพาะเมื่อเปิดใช้งานการรองรับสื่อผ่านการตั้งค่า Polylang เท่านั้น)
| ฟิลด์ | คำอธิบาย |
|---|---|
polylangLanguage | ภาษาของโพสต์หรือเพจ หรือ null หากไม่มีการกำหนดภาษา (เช่น: Polylang ถูกติดตั้งในภายหลัง) |
polylangTranslationLanguageIDs | โหนดสำหรับทุกภาษาคำแปลของเอนทิตี ในรูปแบบ JSON object โดยใช้รหัสภาษาเป็นคีย์และ ID ของเอนทิตีเป็นค่า หรือ null หากไม่มีการกำหนดภาษา (เช่น: Polylang ถูกติดตั้งในภายหลัง) |
ฟิลด์ polylangTranslationLanguageIDs ให้ ID ของเอนทิตีสำหรับทุกคำแปล (เช่น โพสต์/เพจ/หมวดหมู่/แท็ก/สื่อ) โดยรับอินพุต includeSelf เพื่อระบุว่าจะรวม ID ของเอนทิตีที่ query หรือไม่ (ค่าเริ่มต้นคือ 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
ประเภทเหล่านี้ implement อินเทอร์เฟซ PolylangMaybeTranslatable
GenericCustomPost เป็นประเภทที่ใช้แทนคัสตอมโพสต์ใด ๆ ที่ติดตั้งอยู่บนเว็บไซต์ เช่น Portfolio, Event, Product หรืออื่น ๆ ในทำนองเดียวกัน GenericTag และ GenericCategory ใช้แทน taxonomy ของพวกมัน
CPT และ taxonomy เหล่านี้แต่ละรายการสามารถกำหนดให้แปลได้ในการตั้งค่า Polylang จากนั้นฟิลด์ polylangLanguage และ polylangTranslationLanguageIDs จะมีพฤติกรรมเหมือนกับ Post และอื่น ๆ (ตามที่อธิบายข้างต้น) และจะคืนค่า null ด้วยหาก CPT หรือ taxonomy ของเอนทิตีไม่ได้ถูกตั้งค่าให้แปล
นอกจากนี้ ฟิลด์ polylangIsTranslatable ระบุว่า CPT หรือ taxonomy ถูกตั้งค่าให้แปลได้หรือไม่
| ฟิลด์ | คำอธิบาย |
|---|---|
polylangLanguage | ภาษาของโพสต์หรือเพจ หรือ null หากไม่มีการกำหนดภาษา (เช่น: Polylang ถูกติดตั้งในภายหลัง) หรือหากเอนทิตีไม่ได้ถูกตั้งค่าให้แปล (ผ่านการตั้งค่า Polylang) |
polylangTranslationLanguageIDs | โหนดสำหรับทุกภาษาคำแปลของเอนทิตี ในรูปแบบ JSON object โดยใช้รหัสภาษาเป็นคีย์และ ID ของเอนทิตีเป็นค่า หรือ null หากไม่มีการกำหนดภาษา (เช่น: Polylang ถูกติดตั้งในภายหลัง) หรือหากเอนทิตีไม่ได้ถูกตั้งค่าให้แปล (ผ่านการตั้งค่า Polylang) |
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 schema มี mutation สำหรับ:
- กำหนดภาษาให้กับคัสตอมโพสต์ แท็ก หมวดหมู่ และรายการสื่อ และ
- กำหนดความสัมพันธ์ระหว่างกัน (เช่น ระบุว่าชุดของคัสตอมโพสต์ แท็ก หมวดหมู่ หรือรายการสื่อเป็นคำแปลของกันและกัน)
| Mutation | คำอธิบาย |
|---|---|
polylangSetCustomPostLanguage | กำหนดภาษาของคัสตอมโพสต์ |
polylangSetTaxonomyTermLanguage | กำหนดภาษาของ taxonomy term |
polylangSetMediaItemLanguage | กำหนดภาษาของรายการสื่อ |
polylangSaveCustomPostTranslationAssociation | กำหนดความสัมพันธ์ของคำแปลสำหรับคัสตอมโพสต์ |
polylangSaveTaxonomyTermTranslationAssociation | กำหนดความสัมพันธ์ของคำแปลสำหรับ taxonomy term |
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 และเราสามารถกรองตามรหัสหรือ locale และตาม 1 ภาษาหรือมากกว่า 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
}
}