Polylang
PolylangPolylang

Polylang

Integração com o plugin Polylang (e também Polylang PRO).

O schema GraphQL recebe os campos para recuperar dados multilíngues.

Tipos Root/QueryRoot

Consulta os metadados do site configurados no Polylang.

CampoDescrição
polylangDefaultLanguageIdioma padrão no Polylang, ou null se não houver idiomas habilitados.
polylangLanguagesLista de idiomas no Polylang.

Executando esta query:

{
  polylangDefaultLanguage {
    code
  }
  polylangLanguages {
    code
  }
}

...pode produzir:

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

Tipos Post, Page, PostTag, PostCategory e Media

Consulta o idioma da entidade e os IDs das traduções dessa entidade.

Esses tipos implementam a interface PolylangTranslatable. (O tipo Media só o faz quando o suporte a mídia está habilitado, por meio das configurações do Polylang.)

CampoDescrição
polylangLanguageIdioma do post ou página, ou null se nenhum idioma foi atribuído (ex.: Polylang foi instalado posteriormente).
polylangTranslationLanguageIDsNós para todos os idiomas de tradução da entidade, como um objeto JSON com o código do idioma como chave e o ID da entidade como valor, ou null se nenhum idioma foi atribuído (ex.: Polylang foi instalado posteriormente).

O campo polylangTranslationLanguageIDs fornece os IDs da entidade para todas as traduções (ou seja, post/page/category/tag/media). Ele aceita o input includeSelf, para indicar se o ID da entidade consultada deve ser incluído nos resultados (é false por padrão), e os inputs includeLanguages e excludeLanguages, para filtrar os idiomas incluídos nos resultados.

Executando esta 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 })
  }
}

...pode produzir:

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

Tipos GenericCustomPost, GenericTag e GenericCategory

Esses tipos implementam a interface PolylangMaybeTranslatable.

GenericCustomPost é um tipo usado para representar qualquer custom post instalado no site, como Portfolio, Event, Product ou outro. Da mesma forma, GenericTag e GenericCategory são usados para representar suas taxonomias.

Cada um desses CPTs e taxonomias pode ser definido como traduzível nas configurações do Polylang. Os campos polylangLanguage e polylangTranslationLanguageIDs terão então o mesmo comportamento descrito acima para Post e os demais, e também retornarão null se o CPT ou a taxonomia da entidade não estiver configurado para ser traduzido.

Além disso, o campo polylangIsTranslatable indica se o CPT ou a taxonomia está configurado como traduzível.

CampoDescrição
polylangLanguageIdioma do post ou página, ou null se nenhum idioma foi atribuído (ex.: Polylang foi instalado posteriormente), ou se a entidade não está configurada para ser traduzida (via Configurações do Polylang).
polylangTranslationLanguageIDsNós para todos os idiomas de tradução da entidade, como um objeto JSON com o código do idioma como chave e o ID da entidade como valor, ou null se nenhum idioma foi atribuído (ex.: Polylang foi instalado posteriormente), ou se a entidade não está configurada para ser traduzida (via Configurações do Polylang).
polylangIsTranslatableIndica se a entidade pode ser traduzida.

Executando esta 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 })
        }
      }
    }
  }
}

...pode produzir:

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

O schema GraphQL recebe mutations para:

  • Estabelecer o idioma de custom posts, tags, categorias e itens de mídia, e
  • Definir associações entre eles (ou seja, indicar que um conjunto de custom posts, tags, categorias ou itens de mídia é uma tradução uns dos outros).
MutationDescrição
polylangSetCustomPostLanguageDefine o idioma do custom post.
polylangSetTaxonomyTermLanguageDefine o idioma do termo de taxonomia.
polylangSetMediaItemLanguageDefine o idioma do item de mídia.
polylangSaveCustomPostTranslationAssociationDefine a associação de tradução para o custom post.
polylangSaveTaxonomyTermTranslationAssociationDefine a associação de tradução para o termo de taxonomia.
polylangSaveMediaItemTranslationAssociationDefine a associação de tradução para o item de mídia.

Por exemplo, a query a seguir define o idioma de 3 posts (para inglês, espanhol e francês) e, em seguida, define que esses 3 posts são uma tradução uns dos outros:

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

Filtrar dados por idioma

Podemos fornecer o idioma para filtrar ao buscar dados para:

  • Posts
  • Páginas
  • Custom posts
  • Categorias
  • Tags
  • Itens de mídia

Os campos correspondentes recebem o input polylangLanguageBy, e podemos filtrar por código ou locale, e por 1 ou mais de 1 idioma.

Por exemplo, passando $languageCodes: ["es"] serão buscados os dados em espanhol:

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