Polylang
PolylangPolylang

Polylang

Integrazione con il plugin Polylang (e anche Polylang PRO).

Lo schema GraphQL viene arricchito con i campi per recuperare dati multilingue.

Tipi Root/QueryRoot

Interroga i metadati del sito configurati in Polylang.

CampoDescrizione
polylangDefaultLanguageLingua predefinita in Polylang, oppure null se non è abilitata alcuna lingua.
polylangLanguagesElenco delle lingue in Polylang.

Eseguendo questa query:

{
  polylangDefaultLanguage {
    code
  }
  polylangLanguages {
    code
  }
}

...potrebbe produrre:

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

Tipi Post, Page, PostTag, PostCategory e Media

Interroga la lingua dell'entità e gli ID delle traduzioni di tale entità.

Questi tipi implementano l'interfaccia PolylangTranslatable. (Il tipo Media lo fa solo quando il supporto per i media è abilitato, tramite le impostazioni di Polylang.)

CampoDescrizione
polylangLanguageLingua dell'articolo o della pagina, oppure null se non è stata assegnata alcuna lingua (ad es.: Polylang è stato installato in seguito).
polylangTranslationLanguageIDsNodi per tutte le lingue di traduzione dell'entità, sotto forma di oggetto JSON con il codice della lingua come chiave e l'ID dell'entità come valore, oppure null se non è stata assegnata alcuna lingua (ad es.: Polylang è stato installato in seguito).

Il campo polylangTranslationLanguageIDs fornisce gli ID dell'entità per tutte le traduzioni (ossia post/page/category/tag/media). Accetta l'input includeSelf, per indicare se includere nei risultati l'ID dell'entità interrogata (è false per impostazione predefinita), e gli input includeLanguages ed excludeLanguages, per filtrare le lingue incluse nei risultati.

Eseguendo questa 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 })
  }
}

...potrebbe produrre:

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

Tipi GenericCustomPost, GenericTag e GenericCategory

Questi tipi implementano l'interfaccia PolylangMaybeTranslatable.

GenericCustomPost è un tipo utilizzato per rappresentare qualsiasi custom post installato sul sito, come Portfolio, Event, Product o altro. Allo stesso modo, GenericTag e GenericCategory sono utilizzati per rappresentare le rispettive tassonomie.

Ciascuno di questi CPT e tassonomie può essere definito come traducibile nelle impostazioni di Polylang. I campi polylangLanguage e polylangTranslationLanguageIDs avranno quindi lo stesso comportamento descritto sopra per Post e gli altri, e restituiranno anche null se il CPT o la tassonomia dell'entità non è configurato/a per essere tradotto/a.

Inoltre, il campo polylangIsTranslatable indica se il CPT o la tassonomia è configurato/a come traducibile.

CampoDescrizione
polylangLanguageLingua dell'articolo o della pagina, oppure null se non è stata assegnata alcuna lingua (ad es.: Polylang è stato installato in seguito), oppure se l'entità non è configurata per essere tradotta (tramite le Impostazioni di Polylang).
polylangTranslationLanguageIDsNodi per tutte le lingue di traduzione dell'entità, sotto forma di oggetto JSON con il codice della lingua come chiave e l'ID dell'entità come valore, oppure null se non è stata assegnata alcuna lingua (ad es.: Polylang è stato installato in seguito), oppure se l'entità non è configurata per essere tradotta (tramite le Impostazioni di Polylang).
polylangIsTranslatableIndica se l'entità può essere tradotta.

Eseguendo questa 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 })
        }
      }
    }
  }
}

...potrebbe produrre:

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

Mutazioni

Lo schema GraphQL viene arricchito con mutazioni per:

  • Stabilire la lingua di custom post, tag, categorie ed elementi multimediali, e
  • Definire associazioni tra di essi (ossia indicare che un insieme di custom post, tag, categorie o elementi multimediali sono una traduzione l'uno dell'altro).
MutazioneDescrizione
polylangSetCustomPostLanguageImposta la lingua del custom post.
polylangSetTaxonomyTermLanguageImposta la lingua del termine di tassonomia.
polylangSetMediaItemLanguageImposta la lingua dell'elemento multimediale.
polylangSaveCustomPostTranslationAssociationImposta l'associazione di traduzione per il custom post.
polylangSaveTaxonomyTermTranslationAssociationImposta l'associazione di traduzione per il termine di tassonomia.
polylangSaveMediaItemTranslationAssociationImposta l'associazione di traduzione per l'elemento multimediale.

Ad esempio, la query seguente definisce la lingua di 3 articoli (in inglese, spagnolo e francese), quindi definisce che questi 3 articoli sono una traduzione l'uno dell'altro:

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

Filtrare i dati per lingua

Possiamo fornire la lingua in base alla quale filtrare durante il recupero dei dati per:

  • Post
  • Pagine
  • Custom post
  • Categorie
  • Tag
  • Elementi multimediali

I campi corrispondenti ricevono l'input polylangLanguageBy, e possiamo filtrare per codice o locale, e per 1 o più lingue.

Ad esempio, passando $languageCodes: ["es"] verranno recuperati i dati in spagnolo:

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