Polylang
PolylangPolylang

Polylang

Integración con el plugin Polylang (y también Polylang PRO).

El esquema GraphQL se proporciona con los campos para obtener datos multilingües.

Tipos Root/QueryRoot

Consulta los metadatos del sitio configurados en Polylang.

CampoDescripción
polylangDefaultLanguageIdioma por defecto en Polylang, o null si no hay idiomas habilitados.
polylangLanguagesLista de idiomas en Polylang.

Al ejecutar esta consulta:

{
  polylangDefaultLanguage {
    code
  }
  polylangLanguages {
    code
  }
}

...podría producir:

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

Tipos Post, Page, PostTag, PostCategory y Media

Consulta el idioma de la entidad, y los IDs de las traducciones de esa entidad.

Estos tipos implementan la interfaz PolylangTranslatable. (El tipo Media solo lo hace cuando el soporte de medios está habilitado, mediante los ajustes de Polylang.)

CampoDescripción
polylangLanguageIdioma de la entrada o página, o null si no se asignó ningún idioma (por ejemplo: Polylang se instaló más tarde).
polylangTranslationLanguageIDsNodos para todos los idiomas de traducción de la entidad, como un objeto JSON con el código de idioma como clave y el ID de la entidad como valor, o null si no se asignó ningún idioma (por ejemplo: Polylang se instaló más tarde).

El campo polylangTranslationLanguageIDs proporciona los IDs de entidad para todas las traducciones (es decir, post/page/category/tag/media). Acepta el input includeSelf, para indicar si incluir el ID de la entidad consultada en los resultados (es false por defecto), y los inputs includeLanguages y excludeLanguages, para filtrar los idiomas incluidos en los resultados.

Al ejecutar esta consulta:

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

...podría producir:

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

Estos tipos implementan la interfaz PolylangMaybeTranslatable.

GenericCustomPost es un tipo usado para representar cualquier custom post instalado en el sitio, como Portfolio, Event, Product, u otro. De forma similar, GenericTag y GenericCategory se usan para representar sus taxonomías.

Cada uno de estos CPTs y taxonomías puede definirse como traducible en los ajustes de Polylang. Los campos polylangLanguage y polylangTranslationLanguageIDs tendrán entonces el mismo comportamiento que para Post y los demás (descrito arriba), y también devolverán null si el CPT o taxonomía de la entidad no está configurado para ser traducido.

Además, el campo polylangIsTranslatable indica si el CPT o taxonomía está configurado como traducible.

CampoDescripción
polylangLanguageIdioma de la entrada o página, o null si no se asignó ningún idioma (por ejemplo: Polylang se instaló más tarde), o si la entidad no está configurada para ser traducida (mediante los Ajustes de Polylang).
polylangTranslationLanguageIDsNodos para todos los idiomas de traducción de la entidad, como un objeto JSON con el código de idioma como clave y el ID de la entidad como valor, o null si no se asignó ningún idioma (por ejemplo: Polylang se instaló más tarde), o si la entidad no está configurada para ser traducida (mediante los Ajustes de Polylang).
polylangIsTranslatableIndica si la entidad puede ser traducida.

Al ejecutar esta consulta:

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

...podría producir:

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

Mutaciones

El esquema GraphQL se proporciona con mutaciones para:

  • Establecer el idioma para custom posts, etiquetas, categorías y elementos multimedia, y
  • Definir asociaciones entre ellos (es decir, indicar que un conjunto de custom posts, etiquetas, categorías o elementos multimedia es una traducción entre sí).
MutaciónDescripción
polylangSetCustomPostLanguageEstablece el idioma del custom post.
polylangSetTaxonomyTermLanguageEstablece el idioma del término de taxonomía.
polylangSetMediaItemLanguageEstablece el idioma del elemento multimedia.
polylangSaveCustomPostTranslationAssociationEstablece la asociación de traducción para el custom post.
polylangSaveTaxonomyTermTranslationAssociationEstablece la asociación de traducción para el término de taxonomía.
polylangSaveMediaItemTranslationAssociationEstablece la asociación de traducción para el elemento multimedia.

Por ejemplo, la siguiente consulta define el idioma de 3 entradas (a inglés, español y francés), y luego define que estas 3 entradas son una traducción entre sí:

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 datos por idioma

Podemos proporcionar el idioma por el que filtrar al obtener datos para:

  • Posts
  • Pages
  • Custom posts
  • Categorías
  • Etiquetas
  • Elementos multimedia

Los campos correspondientes reciben el input polylangLanguageBy, y podemos filtrar por código o locale, y por 1 o más de 1 idioma.

Por ejemplo, pasando $languageCodes: ["es"] obtendrá datos en español:

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