Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Translate: Resolvers don't work with "blocks" field type #100

Open
PP-Tom opened this issue Nov 26, 2024 · 1 comment
Open

Translate: Resolvers don't work with "blocks" field type #100

PP-Tom opened this issue Nov 26, 2024 · 1 comment

Comments

@PP-Tom
Copy link

PP-Tom commented Nov 26, 2024

Both Copy Locale and Open AI Translate do not work with a blocks type field, in my case it's inside a non named tab. It does add the block but doesn't add the content.

Edit:
Payload Version 3.1.0

@BM-Buschmann
Copy link

To fix it, edit the traverse rich text with the following to also traverse block types for text.

// src/translate/traverseRichText.ts

export const traverseRichText = ({
  onText,
  root,
  siblingData,
}: {
  onText: (siblingData: Record<string, unknown>) => void
  root: Record<string, unknown>
  siblingData?: Record<string, unknown>
}) => {
  siblingData = siblingData ?? root

  if (siblingData.text) {
    onText(siblingData)
  }

  if (Array.isArray(siblingData?.children)) {
    for (const child of siblingData.children) {
      traverseRichText({
        onText,
        root,
        siblingData: child,
      })
    }
  } else if (
    siblingData.type === 'block' &&
    typeof siblingData.fields === 'object' &&
    siblingData.fields !== null &&
    'content' in siblingData.fields &&
    typeof siblingData.fields.content === 'object' &&
    siblingData.fields.content !== null &&
    'root' in siblingData.fields.content &&
    typeof siblingData.fields.content.root === 'object'
  ) {
    traverseRichText({
      onText,
      root,
      siblingData: siblingData.fields.content.root as Record<string, unknown>,
    })
  }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants