Sanity Library Reference Docs
    Preparing search index...
    • Alpha

      Translates documents or fields using Sanity Agent Actions.

      Parameters

      Returns (options: TranslateDocument) => Subscribable<unknown>

      A stable callback that triggers the action and yields a Subscribable stream.

      This hook provides a stable callback to translate document content between languages using AI.

      Features:

      • Configure fromLanguage/toLanguage, optional styleGuide, and protectedPhrases.
      • Can write into a different targetDocument, and/or store language in a field.
      • Optional temperature, async, noWrite, conditionalPaths.
      import {useAgentTranslate} from '@sanity/sdk-react'

      function TranslateArticle({documentId}: {documentId: string}) {
      const translate = useAgentTranslate()

      const handleTranslate = () => {
      translate({
      documentId,
      fromLanguage: 'en',
      toLanguage: 'es',
      targetPaths: ['title', 'body'],
      }).subscribe({
      complete: () => console.log('Translation complete'),
      error: (err) => console.error('Translation failed:', err),
      })
      }

      return <button onClick={handleTranslate}>Translate to Spanish</button>
      }
      import {useAgentTranslate} from '@sanity/sdk-react'

      function TranslateWithBrandTerms({documentId}: {documentId: string}) {
      const translate = useAgentTranslate()

      const handleTranslate = () => {
      translate({
      documentId,
      fromLanguage: 'en',
      toLanguage: 'fr',
      styleGuide: 'Use formal French appropriate for business communication.',
      protectedPhrases: ['Acme Corp', 'PowerWidget Pro'],
      targetPaths: ['title', 'description'],
      }).subscribe({
      complete: () => console.log('Translation complete'),
      })
      }

      return <button onClick={handleTranslate}>Translate to French</button>
      }
      import {useAgentTranslate} from '@sanity/sdk-react'

      function CreateTranslatedCopy({documentId}: {documentId: string}) {
      const translate = useAgentTranslate()

      const handleCreateTranslation = () => {
      translate({
      documentId,
      fromLanguage: 'en',
      toLanguage: 'de',
      targetDocument: {
      operation: 'create',
      _type: 'article',
      },
      languageFieldPath: 'language',
      }).subscribe({
      next: (result) => console.log('New translated document:', result),
      complete: () => console.log('Translated copy created'),
      })
      }

      return <button onClick={handleCreateTranslation}>Create German Copy</button>
      }