Polylang
PolylangPolylang

Polylang

Tích hợp với plugin Polylang (và cả Polylang PRO).

Lược đồ GraphQL được bổ sung các trường để truy xuất dữ liệu đa ngôn ngữ.

Các kiểu Root/QueryRoot

Truy vấn siêu dữ liệu trang web được cấu hình trong Polylang.

TrườngMô tả
polylangDefaultLanguageNgôn ngữ mặc định trên Polylang, hoặc null nếu không có ngôn ngữ nào được bật.
polylangLanguagesDanh sách các ngôn ngữ trên Polylang.

Chạy queries này:

{
  polylangDefaultLanguage {
    code
  }
  polylangLanguages {
    code
  }
}

...có thể trả về:

{
  "data": {
    "polylangDefaultLanguage": {
      "code": "en"
    },
    "polylangLanguages": [
      {
        "code": "en"
      },
      {
        "code": "es"
      },
      {
        "code": "fr"
      }
    ]
  }
}

Các kiểu Post, Page, PostTag, PostCategoryMedia

Truy vấn ngôn ngữ của thực thể và các ID bản dịch của thực thể đó.

Các kiểu này triển khai giao diện PolylangTranslatable. (Kiểu Media chỉ triển khai khi tính năng hỗ trợ media được bật, thông qua cài đặt Polylang.)

TrườngMô tả
polylangLanguageNgôn ngữ của bài đăng hoặc trang, hoặc null nếu không có ngôn ngữ nào được gán (ví dụ: Polylang được cài đặt sau đó).
polylangTranslationLanguageIDsCác nút cho tất cả ngôn ngữ dịch của thực thể, dưới dạng đối tượng JSON với mã ngôn ngữ làm khóa và ID thực thể làm giá trị, hoặc null nếu không có ngôn ngữ nào được gán (ví dụ: Polylang được cài đặt sau đó).

Trường polylangTranslationLanguageIDs cung cấp các ID thực thể cho tất cả bản dịch (tức là post/page/category/tag/media). Nó chấp nhận đầu vào includeSelf, để chỉ định có bao gồm ID của thực thể được truy vấn trong kết quả hay không (mặc định là false), và các đầu vào includeLanguagesexcludeLanguages, để lọc các ngôn ngữ được bao gồm trong kết quả.

Chạy queries này:

{
  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 })
  }
}

...có thể trả về:

{
  "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
        }
      }
    ]
  }
}

Các kiểu GenericCustomPost, GenericTagGenericCategory

Các kiểu này triển khai giao diện PolylangMaybeTranslatable.

GenericCustomPost là một kiểu được dùng để đại diện cho bất kỳ custom post nào được cài đặt trên trang web, chẳng hạn như Portfolio, Event, Product hoặc các loại khác. Tương tự, GenericTagGenericCategory được dùng để đại diện cho các phân loại của chúng.

Mỗi CPT và phân loại này có thể được định nghĩa là có thể dịch trong cài đặt Polylang. Các trường polylangLanguagepolylangTranslationLanguageIDs khi đó sẽ có cùng hành vi như với Post và các kiểu khác (được mô tả ở trên), và cũng trả về null nếu CPT hoặc phân loại của thực thể không được cấu hình để dịch.

Ngoài ra, trường polylangIsTranslatable chỉ định liệu CPT hoặc phân loại có được cấu hình để có thể dịch hay không.

TrườngMô tả
polylangLanguageNgôn ngữ của bài đăng hoặc trang, hoặc null nếu không có ngôn ngữ nào được gán (ví dụ: Polylang được cài đặt sau đó), hoặc nếu thực thể không được cấu hình để dịch (qua Cài đặt Polylang).
polylangTranslationLanguageIDsCác nút cho tất cả ngôn ngữ dịch của thực thể, dưới dạng đối tượng JSON với mã ngôn ngữ làm khóa và ID thực thể làm giá trị, hoặc null nếu không có ngôn ngữ nào được gán (ví dụ: Polylang được cài đặt sau đó), hoặc nếu thực thể không được cấu hình để dịch (qua Cài đặt Polylang).
polylangIsTranslatableChỉ định liệu thực thể có thể được dịch hay không.

Chạy queries này:

{
  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 })
        }
      }
    }
  }
}

...có thể trả về:

{
  "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

Lược đồ GraphQL được cung cấp các mutation để:

  • Thiết lập ngôn ngữ cho các custom post, tag, danh mục và mục media, và
  • Xác định các liên kết giữa chúng (tức là chỉ định rằng một tập hợp các custom post, tag, danh mục hoặc mục media là bản dịch của nhau).
MutationMô tả
polylangSetCustomPostLanguageĐặt ngôn ngữ của custom post.
polylangSetTaxonomyTermLanguageĐặt ngôn ngữ của thuật ngữ phân loại.
polylangSetMediaItemLanguageĐặt ngôn ngữ của mục media.
polylangSaveCustomPostTranslationAssociationĐặt liên kết dịch cho custom post.
polylangSaveTaxonomyTermTranslationAssociationĐặt liên kết dịch cho thuật ngữ phân loại.
polylangSaveMediaItemTranslationAssociationĐặt liên kết dịch cho mục media.

Ví dụ, queries sau đây định nghĩa ngôn ngữ cho 3 bài đăng (sang tiếng Anh, tiếng Tây Ban Nha và tiếng Pháp), rồi định nghĩa rằng 3 bài đăng này là bản dịch của nhau:

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
      }
    }
  }
}

Lọc dữ liệu theo ngôn ngữ

Chúng ta có thể cung cấp ngôn ngữ để lọc khi lấy dữ liệu cho:

  • Bài đăng (Posts)
  • Trang (Pages)
  • Custom posts
  • Danh mục (Categories)
  • Tag
  • Mục media (Media items)

Các trường tương ứng nhận đầu vào polylangLanguageBy, và chúng ta có thể lọc theo mã hoặc locale, và theo 1 hoặc nhiều hơn 1 ngôn ngữ.

Ví dụ, truyền $languageCodes: ["es"] sẽ lấy dữ liệu bằng tiếng Tây Ban Nha:

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
  }
}