Queries-Bibliothek
Queries-BibliothekSeiten für mehrsprachige WordPress-Website übersetzen (Classic editor)

Seiten für mehrsprachige WordPress-Website übersetzen (Classic editor)

Diese GraphQL-query hilft dabei, eine mehrsprachige Website zu erstellen, bei der jede Website die Übersetzung für eine bestimmte Sprache ist. Der Inhalt basiert auf dem Classic editor und eignet sich am besten für ein WordPress-Multisite-Netzwerk.

Diese query erfordert, dass der Endpoint Nested Mutations aktiviert hat.

Die query muss auf der Master-Website (mit dem Quellinhalt) ausgeführt werden, auf der das Bundle „Power Extensions" installiert sein muss. Alle anderen Websites im Netzwerk können das kostenlose Gato GraphQL-Plugin verwenden.

Zum Verbinden mit den Netzwerk-Websites wird ein Application Password verwendet. Stelle sicher, dass du die Variablen $username und $appPassword für einen Benutzer mit Zugriff auf alle Websites angibst.

Die query ruft die Sprache von der Hauptwebsite und von der externen Website ab und vergleicht sie:

  • Sind sie gleich, werden alle Seiten einfach repliziert
  • Sind sie unterschiedlich, werden alle Seiten mit übersetztem Inhalt repliziert

(Auf diese Weise werden alle Seiten trotzdem repliziert, wenn die Hauptwebsite die Sprache en hat und eine andere Website im Netzwerk ebenfalls en hat.)

Anschließend werden die Seiten von der Master-Website abgerufen und (falls erforderlich) alle auf einmal in die Sprache der Zielwebsite übersetzt, indem ein einziger Aufruf an die Google Translate API ausgeführt wird.

Schließlich werden all diese Seiten auf der externen Website erstellt.

########################################################################
# 
# Variables:
#   - $username: The username to log into the external site
#   - $appPassword: The application password to log into the external site
#   - $externalSiteURL: The URL of the external site, where all pages will be (translated and) created
#   - (Optional) $pageIDs: Restrict translating/creating the pages with given IDs
#   - (Optional) $pageStatus: Fetch pages with given status
#   - (Optional) $externalSiteGraphQLEndpointPath: Path to the GraphQL endpoint on the external site
#
# *********************************************************************
#
# === Description ===
#
# This Persisted GraphQL query helps create a Multilingual site
# where every site is the translation for some language. The content
# is based on the Classic editor, and it is most suitable with a
# WordPress multisite network.
# 
# It must be executed on the master site, which must have the PRO plugin.
# All other sites in the network can have the free Gato GraphQL plugin.
#
# The query retrieves the language from the main site and from the external
# site, and compares them:
#
#   - If they are the same, simply replicate all pages
#   - If they are different, replicate all pages with translated content
#
#   (This way, if the main site has "en" language, and another site in
#   the network also has "en", all pages are still replicated.)
#
# Then it grabs all the pages from the master site, and (if required) it
# translates them all at once (in bulk) to the destination site's language,
# by executing a single call to the Google Translate API.
#
# Finally it creates all those pages in the external site.
#
########################################################################
 
query InitializeExternalSiteVariables
  @configureWarningsOnExportingDuplicateVariable(enabled: false)
{
  isGutenbergEditorEnabled
    @export(as: "isGutenbergEditorEnabled")
 
  emptyBool: _echo(value: false)
    @export(as: "hasMasterPages")
    @export(as: "executeTranslation")
    @remove
}
 
query ExportData(
  $username: String!
  $appPassword: String!
  $externalSiteURL: URL!
  $externalSiteGraphQLEndpointPath: String! = "/graphql/internal/"
  $pageIDs: [ID!]! = []
  $pageStatus: [CustomPostStatusEnum!]! = [publish]
)
  @depends(on: "InitializeExternalSiteVariables")
  @skip(if: $isGutenbergEditorEnabled)
{
  # Retrieve the language of the content
  siteLanguage
    @export(as: "fromLanguage")
 
  # Generate the authorization header to connect to the external site
  loginCredentials: _sprintf(
    string: "%s:%s",
    values: [$username, $appPassword]
  )
    @remove
  base64EncodedLoginCredentials: _strBase64Encode(
    string: $__loginCredentials
  )
    @remove
  authorizationHeaderValue: _sprintf(
    string: "Basic %s",
    values: [$__base64EncodedLoginCredentials]
  )
    @remove
    @export(as: "authorizationHeaderValue")
  
  # Generate the external site's GraphQL endpoint to connect to
  endpoint: _sprintf(
    string: "%s%s",
    values: [
      $externalSiteURL,
      $externalSiteGraphQLEndpointPath
    ]
  )
    @export(as: "endpoint")
    
  masterPages: pages(filter: { ids: $pageIDs, status: $pageStatus }) {
    id
    emptyArray: _echo(value: [])
      @export(
        as: "rawTitle"
        type: DICTIONARY
      )
      @export(
        as: "rawContent"
        type: DICTIONARY
      )
      @export(
        as: "rawExcerpt"
        type: DICTIONARY
      )
      @remove
  }
 
  hasMasterPages: _notEmpty(value: $__masterPages)
    @export(as: "hasMasterPages")
}
 
query RetrieveAndExportExternalSiteLanguage
  @depends(on: "ExportData")
  @include(if: $hasMasterPages)
{
  # Retrieve the language of the external site
  externalHTTPRequest: _sendGraphQLHTTPRequest(input:{
    endpoint: $endpoint,
    query: """
  
{
  me {
    name
  }
  siteLanguage
}
 
    """,
    options: {
      headers: [
        {
          name: "Authorization",
          value: $authorizationHeaderValue
        }
      ]
    }
  })
  externalSiteLanguage: _objectProperty(
    object: $__externalHTTPRequest,
    by: {
      path: "data.siteLanguage"
    }
  )
    @export(as: "toLanguage")
  
  # Indicate if connecting to the external site was successful
  hasRetrievedExternalSiteLanguage: _notEmpty(
    value: $__externalSiteLanguage
  )
 
  # Only translate the content if the master/destination sites languages are different
  areFromToLanguagesDifferent: _notEquals(
    value1: $fromLanguage
    value2: $__externalSiteLanguage
  )
 
  # Flag to indicate if to translate the content
  executeTranslation: _and(
    values: [
      $__hasRetrievedExternalSiteLanguage,
      $__areFromToLanguagesDifferent,
    ]
  )
    @export(as: "executeTranslation")
}
 
query FetchData(
  $pageIDs: [ID!]! = []
  $pageStatus: [CustomPostStatusEnum!]! = [publish]
)
  @depends(on: "RetrieveAndExportExternalSiteLanguage")
  @include(if: $hasMasterPages)
{
  fetchDataPages: pages(filter: { ids: $pageIDs, status: $pageStatus }) {
    id
 
 
    rawTitle
      @export(
        as: "rawTitle"
        type: DICTIONARY
      )
    rawContent
      @export(
        as: "rawContent"
        type: DICTIONARY
      )
    rawExcerpt
      @export(
        as: "rawExcerpt"
        type: DICTIONARY
      )
  }
}
 
query AdaptData
  @depends(on: "FetchData")
  @include(if: $hasMasterPages)
{
  adaptedToRawTitle: _echo(value: $rawTitle)
    @underEachJSONObjectProperty(
      passValueOnwardsAs: "value"
    )
      @applyField(
        name: "_echo"
        arguments: {
          value: [$value]
        }
        setResultInResponse: true
      )
    @export(as: "adaptedToRawTitle")
  adaptedFromTitle: _echo(value: $rawTitle)
    @underEachJSONObjectProperty
      @applyField(
        name: "_echo"
        arguments: {
          value: [""]
        }
        setResultInResponse: true
      )
    @export(as: "adaptedFromTitle")
 
  adaptedToRawContent: _echo(value: $rawContent)
    @underEachJSONObjectProperty(
      passValueOnwardsAs: "value"
    )
      @applyField(
        name: "_echo"
        arguments: {
          value: [$value]
        }
        setResultInResponse: true
      )
    @export(as: "adaptedToRawContent")
  adaptedFromRawContent: _echo(value: $rawContent)
    @underEachJSONObjectProperty
      @applyField(
        name: "_echo"
        arguments: {
          value: [""]
        }
        setResultInResponse: true
      )
    @export(as: "adaptedFromRawContent")
 
  adaptedToRawExcerpt: _echo(value: $rawExcerpt)
    @underEachJSONObjectProperty(
      passValueOnwardsAs: "value"
    )
      @applyField(
        name: "_echo"
        arguments: {
          value: [$value]
        }
        setResultInResponse: true
      )
    @export(as: "adaptedToRawExcerpt")
  adaptedFromRawExcerpt: _echo(value: $rawExcerpt)
    @underEachJSONObjectProperty
      @applyField(
        name: "_echo"
        arguments: {
          value: [""]
        }
        setResultInResponse: true
      )
    @export(as: "adaptedFromRawExcerpt")
}
 
query TransformData
  @depends(on: "AdaptData")
  @include(if: $hasMasterPages)
{
  transformations: _echo(value: {
    metaRawTitle: {
      from: $adaptedFromTitle,
      to: $adaptedToRawTitle,
    },
    metaRawContent: {
      from: $adaptedFromRawContent,
      to: $adaptedToRawContent,
    },
    metaRawExcerpt: {
      from: $adaptedFromRawExcerpt,
      to: $adaptedToRawExcerpt,
    }
  })
    @if(condition: $executeTranslation)
      @underEachJSONObjectProperty
        @underJSONObjectProperty(by: { key: "to" })
          @underEachJSONObjectProperty
            @underEachArrayItem
              @strTranslate(
                from: $fromLanguage,
                to: $toLanguage
              )
    @export(as: "transformations")
}
 
query PrepareMetaReplacements
  @depends(on: "TransformData")
  @include(if: $hasMasterPages)
{
  transformedMetaTitle: _echo(value: $rawTitle)
    @underEachJSONObjectProperty(
      passKeyOnwardsAs: "pageID"
      affectDirectivesUnderPos: [1, 2, 3]
    )
      @applyField(
        name: "_sprintf",
        arguments: {
          string: "metaRawTitle.to.%s",
          values: [$pageID]
        }
        passOnwardsAs: "titlePath"
      )
      @applyField(
        name: "_objectProperty",
        arguments: {
          object: $transformations
          by: { path: $titlePath }
        }
        passOnwardsAs: "transformedPostTitleAsArray"
      )
      @applyField(
        name: "_arrayItem",
        arguments: {
          array: $transformedPostTitleAsArray
          position: 0
        }
        setResultInResponse: true
      )
    @export(
      as: "transformedRawTitle"
    )
 
  transformedMetaRawContent: _echo(value: $rawContent)
    @underEachJSONObjectProperty(
      passKeyOnwardsAs: "pageID"
      affectDirectivesUnderPos: [1, 2, 3]
    )
      @applyField(
        name: "_sprintf",
        arguments: {
          string: "metaRawContent.to.%s",
          values: [$pageID]
        }
        passOnwardsAs: "rawContentPath"
      )
      @applyField(
        name: "_objectProperty",
        arguments: {
          object: $transformations
          by: { path: $rawContentPath }
        }
        passOnwardsAs: "transformedPostRawContentAsArray"
      )
      @applyField(
        name: "_arrayItem",
        arguments: {
          array: $transformedPostRawContentAsArray
          position: 0
        }
        setResultInResponse: true
      )
    @export(
      as: "transformedRawContent"
    )
 
  transformedMetaRawExcerpt: _echo(value: $rawExcerpt)
    @underEachJSONObjectProperty(
      passKeyOnwardsAs: "pageID"
      affectDirectivesUnderPos: [1, 2, 3]
    )
      @applyField(
        name: "_sprintf",
        arguments: {
          string: "metaRawExcerpt.to.%s",
          values: [$pageID]
        }
        passOnwardsAs: "rawExcerptPath"
      )
      @applyField(
        name: "_objectProperty",
        arguments: {
          object: $transformations
          by: { path: $rawExcerptPath }
        }
        passOnwardsAs: "transformedPostRawExcerptAsArray"
      )
      @applyField(
        name: "_arrayItem",
        arguments: {
          array: $transformedPostRawExcerptAsArray
          position: 0
        }
        setResultInResponse: true
      )
    @export(
      as: "transformedRawExcerpt"
    )
}
 
query ExportMutationInputs(
  $pageIDs: [ID!]! = []
  $pageStatus: [CustomPostStatusEnum!]! = [publish]
)
  @depends(on: "PrepareMetaReplacements")
  @include(if: $hasMasterPages)
{
  exportPages: pages(filter: { ids: $pageIDs, status: $pageStatus }) {
    id
    transformedRawContent: _objectProperty(
      object: $transformedRawContent,
      by: {
        key: $__id
      }
    )
    transformedRawTitle: _objectProperty(
      object: $transformedRawTitle,
      by: {
        key: $__id
      }
    )
    transformedSlug: _echo(value: $__transformedRawTitle)
    transformedRawExcerpt: _objectProperty(
      object: $transformedRawExcerpt,
      by: {
        key: $__id
      }
    )
    input: _echo(value: {
      status: draft,
      title: $__transformedRawTitle,
      slug: $__transformedSlug,
      excerpt: $__transformedRawExcerpt,
      contentAs: {
        html: $__transformedRawContent
      }
    })
      @export(
        as: "createPostMutationInputs"
        type: LIST
      )
  }
}
 
mutation CreatePagesWithTranslationOnExternalSite
  @depends(on: "ExportMutationInputs")
  @include(if: $hasMasterPages)
{
  createExternalSitePageHTTPRequests: _echo(value: $createPostMutationInputs)
    @underEachArrayItem(
      passValueOnwardsAs: "input"
    )
      @applyField(
        name: "_sendGraphQLHTTPRequest"
        arguments: {
          input: {
            endpoint: $endpoint,
            query: """
          
mutation CreatePageFromMasterSite($input: JSONObject!) {
  createPage(input: $input) {
    status
    errors {
      __typename
      ...on ErrorPayload {
        message
      }
    }
    page {
      id
      slug
      title
      content
      status
      url
      excerpt
    }
  }
}
 
            """,
            variables: [
              {
                name: "input",
                value: $input
              }
            ],
            options: {
              headers: [
                {
                  name: "Authorization",
                  value: $authorizationHeaderValue
                }
              ]
            }
          }
        },
        setResultInResponse: true
      )
}