Polylang
PolylangPolylang

Polylang

Integracja z wtyczką Polylang (a także Polylang PRO).

Schemat GraphQL otrzymuje pola umożliwiające pobieranie danych wielojęzycznych.

Typy Root/QueryRoot

Pobiera metadane witryny skonfigurowane w Polylang.

PoleOpis
polylangDefaultLanguageDomyślny język w Polylang, lub null jeśli nie ma włączonych języków.
polylangLanguagesLista języków w Polylang.

Uruchamiając tę query:

{
  polylangDefaultLanguage {
    code
  }
  polylangLanguages {
    code
  }
}

...może zwrócić:

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

Typy Post, Page, PostTag, PostCategory i Media

Pobiera język encji oraz identyfikatory tłumaczeń tej encji.

Te typy implementują interfejs PolylangTranslatable. (Typ Media robi to tylko wtedy, gdy obsługa mediów jest włączona w ustawieniach Polylang.)

PoleOpis
polylangLanguageJęzyk posta lub strony, lub null jeśli nie przypisano żadnego języka (np. Polylang został zainstalowany później).
polylangTranslationLanguageIDsWęzły dla wszystkich języków tłumaczeń encji, jako obiekt JSON z kodem języka jako kluczem i identyfikatorem encji jako wartością, lub null jeśli nie przypisano żadnego języka (np. Polylang został zainstalowany później).

Pole polylangTranslationLanguageIDs dostarcza identyfikatory encji dla wszystkich tłumaczeń (tj. post/page/category/tag/media). Przyjmuje argument includeSelf, wskazujący czy uwzględnić identyfikator zapytywanej encji w wynikach (domyślnie false), a także argumenty includeLanguages i excludeLanguages, służące do filtrowania uwzględnionych języków w wynikach.

Uruchamiając tę 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 })
  }
}

...może zwrócić:

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

Typy GenericCustomPost, GenericTag i GenericCategory

Te typy implementują interfejs PolylangMaybeTranslatable.

GenericCustomPost to typ używany do reprezentowania dowolnego custom posta zainstalowanego w witrynie, takiego jak Portfolio, Event, Product lub inny. Podobnie GenericTag i GenericCategory służą do reprezentowania ich taksonomii.

Każdy z tych CPT i taksonomii może zostać zdefiniowany jako przetłumaczalny w ustawieniach Polylang. Pola polylangLanguage i polylangTranslationLanguageIDs będą wtedy działać tak samo jak dla Post i pozostałych (opisanych powyżej), a także zwrócą null, jeśli CPT lub taksonomia encji nie jest skonfigurowana do tłumaczenia.

Ponadto pole polylangIsTranslatable wskazuje, czy CPT lub taksonomia jest skonfigurowana jako przetłumaczalna.

PoleOpis
polylangLanguageJęzyk posta lub strony, lub null jeśli nie przypisano żadnego języka (np. Polylang został zainstalowany później), lub jeśli encja nie jest skonfigurowana do tłumaczenia (via Ustawienia Polylang).
polylangTranslationLanguageIDsWęzły dla wszystkich języków tłumaczeń encji, jako obiekt JSON z kodem języka jako kluczem i identyfikatorem encji jako wartością, lub null jeśli nie przypisano żadnego języka (np. Polylang został zainstalowany później), lub jeśli encja nie jest skonfigurowana do tłumaczenia (via Ustawienia Polylang).
polylangIsTranslatableWskazuje, czy encja może być tłumaczona.

Uruchamiając tę 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 })
        }
      }
    }
  }
}

...może zwrócić:

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

Mutacje

Schemat GraphQL otrzymuje mutacje umożliwiające:

  • Ustawienie języka dla custom postów, tagów, kategorii i elementów multimedialnych, oraz
  • Definiowanie powiązań między nimi (tj. wskazanie, że zestaw custom postów, tagów, kategorii lub elementów multimedialnych jest tłumaczeniem nawzajem siebie).
MutacjaOpis
polylangSetCustomPostLanguageUstawia język custom posta.
polylangSetTaxonomyTermLanguageUstawia język terminu taksonomii.
polylangSetMediaItemLanguageUstawia język elementu multimedialnego.
polylangSaveCustomPostTranslationAssociationUstawia powiązanie tłumaczenia dla custom posta.
polylangSaveTaxonomyTermTranslationAssociationUstawia powiązanie tłumaczenia dla terminu taksonomii.
polylangSaveMediaItemTranslationAssociationUstawia powiązanie tłumaczenia dla elementu multimedialnego.

Na przykład poniższa query definiuje język dla 3 postów (angielski, hiszpański i francuski), a następnie określa, że te 3 posty są tłumaczeniami nawzajem siebie:

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

Filtrowanie danych według języka

Możemy podać język do filtrowania podczas pobierania danych dla:

  • Postów
  • Stron
  • Custom postów
  • Kategorii
  • Tagów
  • Elementów multimedialnych

Odpowiednie pola przyjmują argument polylangLanguageBy, a filtrowanie można przeprowadzać według kodu lub locale, a także według 1 lub więcej języków.

Na przykład przekazanie $languageCodes: ["es"] spowoduje pobranie danych w języku hiszpańskim:

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