@braze/sanity-plugin-ai-translation 0.1.0 β†’ 0.1.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (2) hide show
  1. package/README.md +258 -0
  2. package/package.json +3 -3
package/README.md ADDED
@@ -0,0 +1,258 @@
1
+ # @braze/sanity-plugin-ai-translation
2
+
3
+ > A Sanity Studio v3 plugin that provides AI-powered translation capabilities for internationalized content using Sanity Agent Actions.
4
+
5
+ ## Features
6
+
7
+ - πŸ€– **AI-Powered Translation** - Uses Sanity Agent Actions to translate documents between languages
8
+ - πŸ”„ **Smart Translation Strategies** - Automatically detects translation freshness (fresh/stale/drifted) and applies appropriate strategy
9
+ - πŸ”— **Reference Translation** - Optionally translate referenced documents recursively with configurable depth
10
+ - 🎯 **Custom Translation Prompts** - Configure global and locale-specific translation instructions
11
+ - πŸ“š **Translation Glossary** - Define term-specific translations to ensure consistency
12
+ - πŸ›‘οΈ **Protected Phrases** - Specify terms that should never be translated (e.g., brand names, product names)
13
+ - πŸ’¬ **Feedback System** - AI-powered feedback analysis that automatically improves translation settings
14
+ - βš™οΈ **Flexible Configuration** - Control concurrent translation jobs, reference depth, and translation behavior
15
+ - πŸ” **Debug Mode** - Comprehensive logging for troubleshooting translation issues
16
+
17
+ ## Installation
18
+
19
+ ```sh
20
+ pnpm add @braze/sanity-plugin-ai-translation
21
+ ```
22
+
23
+ ## Prerequisites
24
+
25
+ This plugin requires:
26
+
27
+ 1. **Sanity Studio v4** - Version 4.9.0 or later
28
+ 2. **@sanity/client** - Version 7.1.0 or later (for Agent Actions)
29
+ 3. **@sanity/document-internationalization** - Plugin must be installed and configured
30
+ 4. **sanity-plugin-internationalized-array** - Version 3.1.5 or later
31
+
32
+ ## Configuration
33
+
34
+ ### Basic Setup
35
+
36
+ Add the plugin to your `sanity.config.ts` file:
37
+
38
+ ```typescript
39
+ import { defineConfig } from 'sanity'
40
+ import { documentInternationalization } from '@sanity/document-internationalization'
41
+ import { aiTranslationPlugin } from '@braze/sanity-plugin-ai-translation'
42
+
43
+ const SUPPORTED_LANGUAGES = [
44
+ { id: 'en', title: 'English' },
45
+ { id: 'es', title: 'Spanish' },
46
+ { id: 'fr', title: 'French' },
47
+ { id: 'de', title: 'German' },
48
+ ]
49
+
50
+ export default defineConfig({
51
+ // ... other config
52
+ plugins: [
53
+ // Document internationalization must be configured first
54
+ documentInternationalization({
55
+ supportedLanguages: SUPPORTED_LANGUAGES,
56
+ schemaTypes: ['post', 'page', 'product'],
57
+ }),
58
+
59
+ // Then add the AI translation plugin
60
+ aiTranslationPlugin({
61
+ // Required: Array of supported languages
62
+ supportedLanguages: SUPPORTED_LANGUAGES,
63
+
64
+ // Required: Document types that can be translated
65
+ translatableSchemaTypes: ['post', 'page', 'product'],
66
+
67
+ // Required: Field name containing the language identifier
68
+ languageFieldName: 'language',
69
+
70
+ // Required: Schema ID for Agent Actions (usually '_.schemas.default')
71
+ schemaId: '_.schemas.default',
72
+
73
+ // Optional: Maximum concurrent translation jobs (default: 5)
74
+ maxConcurrentTranslationJobs: 5,
75
+ }),
76
+ ],
77
+ })
78
+ ```
79
+
80
+ ### Configuration Options
81
+
82
+ | Option | Type | Required | Description |
83
+ |--------|------|----------|-------------|
84
+ | `supportedLanguages` | `Array<{id: string, title: string}>` | **Yes** | List of supported languages for translation |
85
+ | `translatableSchemaTypes` | `string[]` | **Yes** | Document types that should have translation actions |
86
+ | `languageFieldName` | `string` | **Yes** | Field name containing the language identifier in documents |
87
+ | `schemaId` | `string` | **Yes** | Schema ID for Sanity Agent Actions (typically `'_.schemas.default'`) |
88
+ | `maxConcurrentTranslationJobs` | `number` | No | Maximum number of concurrent translations (default: 5) |
89
+
90
+ ### Schema Setup
91
+
92
+ The plugin requires you to add the `translationHistory` schema to your document internationalization configuration:
93
+
94
+ ```typescript
95
+ import { documentInternationalization } from '@sanity/document-internationalization'
96
+ import { translationHistorySchema } from '@braze/sanity-plugin-ai-translation'
97
+
98
+ // In your sanity.config.ts
99
+ documentInternationalization({
100
+ supportedLanguages: SUPPORTED_LANGUAGES,
101
+ schemaTypes: ['post', 'page'],
102
+ // Add translation history to metadata
103
+ metadataFields: [translationHistorySchema],
104
+ })
105
+ ```
106
+
107
+ ### Deploy Schema
108
+
109
+ After configuration, deploy your schema so Agent Actions can access it:
110
+
111
+ ```bash
112
+ cd your-sanity-project
113
+ npx sanity deploy
114
+ ```
115
+
116
+ ## Usage
117
+
118
+ ### Translation Action
119
+
120
+ Once configured, a "Translate" action will appear on all documents of types listed in `translatableSchemaTypes`.
121
+
122
+ 1. Open a document in Sanity Studio
123
+ 2. Click the "Translate" action in the document menu
124
+ 3. Select target languages
125
+ 4. Configure translation options:
126
+ - **Overwrite fresh translations** - Re-translate even if translation is up-to-date
127
+ - **Translate only diffs** - For stale translations, only translate changed fields
128
+ - **Translate references** - Include referenced documents in translation
129
+ - **Max reference depth** - How deep to traverse references (if enabled)
130
+ 5. Click "Translate" to start
131
+
132
+ ### Translation States
133
+
134
+ The plugin tracks three translation states:
135
+
136
+ - **Fresh** βœ… - Translation is up-to-date with source
137
+ - **Stale** ⚠️ - Source document revision changed (trivial changes)
138
+ - **Drifted** πŸ”΄ - Source document content changed significantly
139
+
140
+ Based on state:
141
+ - **Fresh** translations are skipped (unless "Overwrite fresh" is enabled)
142
+ - **Stale** translations can use diff-based translation (if "Translate only diffs" is enabled)
143
+ - **Drifted** translations are fully re-translated
144
+
145
+ ### Translation Settings
146
+
147
+ Create a `translationSettings` document to customize translation behavior:
148
+
149
+ ```typescript
150
+ {
151
+ _type: 'translationSettings',
152
+ _id: 'translation-settings',
153
+
154
+ // Global prompt applied to all translations
155
+ globalPrompt: 'Maintain a professional tone. Preserve formatting and links.',
156
+
157
+ // Locale-specific prompts
158
+ localePrompts: [
159
+ {
160
+ locale: 'es',
161
+ prompt: 'Use formal Spanish (usted). Avoid Latin American regionalisms.'
162
+ },
163
+ {
164
+ locale: 'fr',
165
+ prompt: 'Use formal French. Maintain gender agreement for all terms.'
166
+ }
167
+ ],
168
+
169
+ // Terms that should never be translated
170
+ protectedPhrases: ['Braze', 'iOS', 'Android', 'SDK']
171
+ }
172
+ ```
173
+
174
+ ### Translation Glossary
175
+
176
+ Create `translationGlossaryTerm` documents for specific term mappings:
177
+
178
+ ```typescript
179
+ {
180
+ _type: 'translationGlossaryTerm',
181
+ translations: [
182
+ { locale: 'en', value: 'dashboard' },
183
+ { locale: 'es', value: 'panel de control' },
184
+ { locale: 'fr', value: 'tableau de bord' },
185
+ { locale: 'de', value: 'Dashboard' }
186
+ ]
187
+ }
188
+ ```
189
+
190
+ ### Give Translation Feedback
191
+
192
+ The plugin includes an AI-powered feedback system that learns from corrections:
193
+
194
+ 1. Open a translated document
195
+ 2. Click "Give Translation Feedback" action
196
+ 3. Provide feedback and/or enable "Include edits" to analyze changes
197
+ 4. Review AI-generated suggestions:
198
+ - Global prompt updates
199
+ - Locale-specific prompt updates
200
+ - Protected phrases to add
201
+ - Glossary terms to add
202
+ 5. Approve suggestions to apply
203
+
204
+ The AI analyzes your feedback and automatically updates translation settings to improve future translations.
205
+
206
+ ## Translation Workflow
207
+
208
+ ### How It Works
209
+
210
+ 1. **Fetch Metadata** - Retrieves translation history and existing translations
211
+ 2. **Determine Strategy** - Checks freshness state (fresh/stale/drifted)
212
+ 3. **Prepare Translation**:
213
+ - For **drifted** or **fresh (overwrite)**: Full document translation
214
+ - For **stale (translate diffs)**: Diff-based translation of changed fields only
215
+ 4. **Translate** - Uses Sanity Agent Actions with configured prompts
216
+ 5. **Localize References** - Optionally translates referenced documents
217
+ 6. **Update Metadata** - Stores translation history with snapshots
218
+
219
+ ### Reference Translation
220
+
221
+ When "Translate references" is enabled:
222
+
223
+ ```typescript
224
+ // Example: Post with author reference
225
+ {
226
+ _type: 'post',
227
+ title: 'My Post',
228
+ author: { _ref: 'author-en' } // Original English author
229
+ }
230
+
231
+ // After translation with references enabled:
232
+ {
233
+ _type: 'post',
234
+ language: 'es',
235
+ title: 'Mi PublicaciΓ³n',
236
+ author: { _ref: 'author-es' } // Now points to Spanish author
237
+ }
238
+ ```
239
+
240
+ The plugin:
241
+ - Recursively traverses references up to configured depth
242
+ - Translates referenced documents if they don't exist
243
+ - Updates references to point to localized versions
244
+ - Uses semaphore to control concurrent jobs
245
+
246
+ ## Publishing
247
+
248
+ This package is configured for internal publishing to the Braze npm registry.
249
+
250
+ ```bash
251
+ # Login to registry
252
+ npm login --scope=@braze --registry=https://registry.npmjs.org/
253
+
254
+ # Build and publish
255
+ cd package
256
+ pnpm build
257
+ npm publish
258
+ ```
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@braze/sanity-plugin-ai-translation",
3
- "version": "0.1.0",
3
+ "version": "0.1.1",
4
4
  "description": "A Sanity Studio v3 plugin that provides AI translation for translations fields.",
5
5
  "private": false,
6
6
  "keywords": [
@@ -41,7 +41,7 @@
41
41
  ],
42
42
  "browserslist": "extends @sanity/browserslist-config",
43
43
  "scripts": {
44
- "build": "plugin-kit verify-package --silent && pkg-utils build --strict --check --clean",
44
+ "build": "bash copy-readme.sh && plugin-kit verify-package --silent && pkg-utils build --strict --check --clean",
45
45
  "dev": "plugin-kit link-watch",
46
46
  "prepublishOnly": "pnpm run build",
47
47
  "test": "vitest run",
@@ -60,7 +60,7 @@
60
60
  "@types/react": "^18.3.0",
61
61
  "@types/react-dom": "^18.3.0",
62
62
  "lodash.defaults": "4.2.0",
63
- "postcss": "8.4.0",
63
+ "postcss": "8.4.31",
64
64
  "react-fast-compare": "^3.2.2",
65
65
  "rxjs": "^7.8.2",
66
66
  "sanity-plugin-utils": "^1.6.8"