Polylang
PolylangPolylang

Polylang

Polylang 플러그인(및 Polylang PRO)과의 통합입니다.

GraphQL 스키마에는 다국어 데이터를 가져오기 위한 필드가 제공됩니다.

타입 Root/QueryRoot

Polylang에서 설정된 사이트 메타데이터를 조회합니다.

필드설명
polylangDefaultLanguagePolylang의 기본 언어. 활성화된 언어가 없는 경우 null.
polylangLanguagesPolylang의 언어 목록.

이 쿼리를 실행하면:

{
  polylangDefaultLanguage {
    code
  }
  polylangLanguages {
    code
  }
}

...다음과 같은 결과가 반환될 수 있습니다:

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

타입 Post, Page, PostTag, PostCategoryMedia

엔티티의 언어와 해당 엔티티의 번역 ID를 조회합니다.

이러한 타입은 인터페이스 PolylangTranslatable을 구현합니다.(타입 Media는 Polylang 설정에서 미디어 지원이 활성화된 경우에만 해당됩니다.)

필드설명
polylangLanguage게시물 또는 페이지의 언어. 언어가 할당되지 않은 경우 null(예: Polylang이 나중에 설치된 경우).
polylangTranslationLanguageIDs엔티티의 모든 번역 언어 노드. 언어 코드를 키, 엔티티 ID를 값으로 하는 JSON 객체. 언어가 할당되지 않은 경우 null(예: Polylang이 나중에 설치된 경우).

필드 polylangTranslationLanguageIDs는 모든 번역(게시물/페이지/카테고리/태그/미디어)의 엔티티 ID를 제공합니다. 입력 includeSelf로 쿼리 대상 엔티티의 ID를 결과에 포함할지 여부를 지정할 수 있으며(기본값은 false), includeLanguagesexcludeLanguages로 결과에 포함할 언어를 필터링할 수 있습니다.

이 쿼리를 실행하면:

{
  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, GenericTagGenericCategory

이러한 타입은 인터페이스 PolylangMaybeTranslatable을 구현합니다.

GenericCustomPostPortfolio, Event, Product 등 사이트에 설치된 임의의 커스텀 게시물을 나타내는 타입입니다. 마찬가지로, GenericTagGenericCategory는 각각의 택소노미를 나타냅니다.

이러한 CPT와 택소노미 각각은 Polylang 설정에서 번역 가능하도록 정의할 수 있습니다. 그러면 필드 polylangLanguagepolylangTranslationLanguageIDsPost 등과 동일하게 동작하며(위 설명 참조), 엔티티의 CPT 또는 택소노미가 번역 대상으로 설정되지 않은 경우에도 null을 반환합니다.

또한, 필드 polylangIsTranslatable은 CPT 또는 택소노미가 번역 가능하도록 설정되어 있는지를 나타냅니다.

필드설명
polylangLanguage게시물 또는 페이지의 언어. 언어가 할당되지 않은 경우(예: Polylang이 나중에 설치된 경우), 또는 엔티티가 번역 대상으로 설정되지 않은 경우(Polylang 설정에 의해)null.
polylangTranslationLanguageIDs엔티티의 모든 번역 언어 노드. 언어 코드를 키, 엔티티 ID를 값으로 하는 JSON 객체. 언어가 할당되지 않은 경우(예: Polylang이 나중에 설치된 경우), 또는 엔티티가 번역 대상으로 설정되지 않은 경우(Polylang 설정에 의해)null.
polylangIsTranslatable엔티티를 번역할 수 있는지 여부를 나타냅니다.

이 쿼리를 실행하면:

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

뮤테이션

GraphQL 스키마에는 다음 작업을 수행하는 뮤테이션이 제공됩니다:

  • 커스텀 게시물, 태그, 카테고리, 미디어 아이템의 언어를 설정하고,
  • 이들 간의 번역 연관 관계를 정의합니다(즉, 커스텀 게시물·태그·카테고리·미디어 아이템의 집합이 서로의 번역임을 나타냅니다).
뮤테이션설명
polylangSetCustomPostLanguage커스텀 게시물의 언어를 설정합니다.
polylangSetTaxonomyTermLanguage택소노미 텀의 언어를 설정합니다.
polylangSetMediaItemLanguage미디어 아이템의 언어를 설정합니다.
polylangSaveCustomPostTranslationAssociation커스텀 게시물의 번역 연관 관계를 설정합니다.
polylangSaveTaxonomyTermTranslationAssociation택소노미 텀의 번역 연관 관계를 설정합니다.
polylangSaveMediaItemTranslationAssociation미디어 아이템의 번역 연관 관계를 설정합니다.

예를 들어, 다음 쿼리는 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
  }
}