Gato GraphQL + Bricks Builder + Translation demo

Translating Bricks content

Translate the text content in a Bricks page to the given language

Leonardo Losoviz
Leonardo Losoviz -
Logo
Image
Target Image
Target Image

We can use Gato GraphQL with the Bricks extension and the Translation extension to automatically translate the content in Bricks page elements to different languages.

This query translates the content in the heading, text, text-basic, button and dropdown elements of a Bricks page to a different language.

We must provide the following variables:

  • customPostId: The ID of the Bricks custom post to update
  • targetLanguageCode: The language code to translate the content to (e.g., "es", "fr", "de")

Here is the GraphQL query:

query InitializeGlobalVariables
  @configureWarningsOnExportingDuplicateVariable(enabled: false)
{
  emptyArray: _echo(value: [])
    @export(as: "elementToUpdateIDs")
    @export(as: "elementToUpdateTexts")
    @remove
}
 
query GetAndTranslateBricksData(
  $customPostId: ID!
  $targetLanguageCode: String!
)
  @depends(on: "InitializeGlobalVariables")
{
  customPost(by:{ id: $customPostId }, status: any) {
    id
    title
    bricksData(filterBy: { include: [
      "heading",
      "text",
      "text-basic",
      "button",
      "dropdown",
    ] })
      @underEachArrayItem(
        affectDirectivesUnderPos: [1, 3]
      )
        @underJSONObjectProperty(by: { key: "id" })
          @export(as: "elementToUpdateIDs")
        @underJSONObjectProperty(
          by: { path: "settings.text" }
          affectDirectivesUnderPos: [1, 2]
        )
          @strTranslate(to: $targetLanguageCode)
          @export(as: "elementToUpdateTexts")
  }
}
 
query GetElementToUpdateData
  @depends(on: "GetAndTranslateBricksData")
{
  elementToUpdateMergeInputElements: _echo(value: $elementToUpdateTexts)
    @underEachArrayItem(
      passIndexOnwardsAs: "index",
      passValueOnwardsAs: "elementToUpdateText"
      affectDirectivesUnderPos: [1, 2]
    )
      @applyField(
        name: "_arrayItem",
        arguments: {
          array: $elementToUpdateIDs,
          position: $index
        },
        passOnwardsAs: "elementToUpdateID"
      )
      @applyField(
        name: "_echo",
        arguments: {
          value: {
            id: $elementToUpdateID,
            settings: {
              text: $elementToUpdateText
            }
          }
        }
        setResultInResponse: true
      )
    @export(as: "elementToUpdateMergeInputElements")
}
 
mutation StoreUpdatedElementText($customPostId: ID!)
  @depends(on: "GetElementToUpdateData")
{
  bricksMergeCustomPostElementDataItem(input: {
    customPostID: $customPostId
    elements: $elementToUpdateMergeInputElements
  }) {
    status
    errors {
      __typename
      ...on ErrorPayload {
        message
          @passOnwards(as: "message")
          @fail(
            message: $message
            condition: ALWAYS
          )
      }
    }
    customPost {
      __typename
      ...on CustomPost {
        id
        bricksData
      }
    }
  }
}

The variables would look like this:

{
  "customPostId": 123,
  "targetLanguageCode": "es"
}

Subscribe to our newsletter

Stay in the loop on all new things concerning Gato GraphQL.