Demo Gato GraphQL + Bricks Builder + Translation

Dịch nội dung Bricks

Dịch nội dung văn bản trong một trang Bricks sang ngôn ngữ được chỉ định

Leonardo Losoviz
Leonardo Losoviz -
Logo
Image
Target Image
Target Image

Chúng ta có thể sử dụng Gato GraphQL cùng với tiện ích mở rộng Brickstiện ích mở rộng Translation để tự động dịch nội dung trong các phần tử trang Bricks sang các ngôn ngữ khác nhau.

Query này dịch nội dung trong các phần tử heading, text, text-basic, buttondropdown của một trang Bricks sang một ngôn ngữ khác.

Chúng ta cần cung cấp các biến sau:

  • customPostId: ID của custom post Bricks cần cập nhật
  • targetLanguageCode: Mã ngôn ngữ cần dịch nội dung sang (ví dụ: "es", "fr", "de")

Đây là 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
      }
    }
  }
}

Các biến sẽ trông như thế này:

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

Đăng ký nhận bản tin của chúng tôi

Cập nhật tất cả những điều mới từ Gato GraphQL.