Const AlphaA 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:
fromLanguage/toLanguage, optional styleGuide, and protectedPhrases.targetDocument, and/or store language in a field.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>
}
Translates documents or fields using Sanity Agent Actions.