You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
This repository was archived by the owner on Feb 3, 2026. It is now read-only.
Copy file name to clipboardExpand all lines: README.md
+15-83Lines changed: 15 additions & 83 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -4,7 +4,7 @@
4
4
5
5
# sanity-plugin-internationalized-array
6
6
7
-
A plugin to register array fields with a custom input component to store field values in multiple languages, queryable by the `language` field.
7
+
A plugin to register array fields with a custom input component to store field values in multiple languages, queryable by using the language ID as an array `_key`.
8
8
9
9

10
10
@@ -19,7 +19,6 @@ A plugin to register array fields with a custom input component to store field v
19
19
-[Usage with @sanity/language-filter](#usage-with-sanitylanguage-filter)
20
20
-[Shape of stored data](#shape-of-stored-data)
21
21
-[Querying data](#querying-data)
22
-
-[Migrate from v3 to v4](#migrate-from-v3-to-v4)
23
22
-[Migrate from objects to arrays](#migrate-from-objects-to-arrays)
24
23
-[Why store localized field data like this?](#why-store-localized-field-data-like-this)
The custom input contains buttons which will add new array items with a `language` field identifying the language. Data returned from this array will look like this:
342
+
The custom input contains buttons which will add new array items with the language as the `_key` value. Data returned from this array will look like this:
> **Note:** In versions prior to v4, the language ID was stored in the `_key` field. See [Migrate from v3 to v4](#migrate-from-v3-to-v4) if you're upgrading from an earlier version.
352
-
353
351
## Querying data
354
352
355
-
Using GROQ filters you can query for a specific language like so:
353
+
Using GROQ filters you can query for a specific language key like so:
356
354
357
355
```js
358
356
*[_type =="person"] {
359
-
"greeting": greeting[language=="en"][0].value
357
+
"greeting": greeting[_key=="en"][0].value
360
358
}
361
359
```
362
360
363
-
> **Migrating queries from v3:** If upgrading from v3, replace `_key == "en"` with `language == "en"` in your GROQ queries.
364
-
365
-
## Migrate from v3 to v4
366
-
367
-
Version 4 changes how language identification is stored. Previously, the language ID was stored in the array item's `_key` field. Now, a dedicated `language` field is used, and `_key` contains a random identifier.
The `_key` field in Sanity arrays is meant for tracking item identity across edits, not for storing semantic data. Using it for language IDs caused issues with:
382
-
- Array reordering and diffing in the Studio
383
-
- Portable Text operations that rely on stable keys
384
-
- Edge cases when copying/pasting between documents
385
-
386
-
### Migration steps
387
-
388
-
1.**Take a backup first!**
389
-
```bash
390
-
npx sanity@latest dataset export
391
-
```
392
-
393
-
2.**Update the plugin** to v4
394
-
395
-
3.**Update your GROQ queries** to use `language` instead of `_key`:
396
-
```js
397
-
// Before
398
-
greeting[_key =="en"][0].value
399
-
400
-
// After
401
-
greeting[language =="en"][0].value
402
-
```
403
-
404
-
4.**Run the migration script** to update existing documents:
405
-
406
-
Edit `migrations/keyToLanguage.ts` to configure your document types and field names:
407
-
```ts
408
-
const DOCUMENT_TYPES = ['post', 'page'] // Your document types
409
-
const FIELD_NAMES = ['title', 'description'] // Your internationalized fields
Then set `DRY_RUN = false` and run again to apply changes.
418
-
419
-
5.**Handle drafts and published documents** - The migration script processes all documents. Run it twice if needed: once for production, once after publishing any pending drafts.
420
-
421
-
### Migration script details
422
-
423
-
The migration script (`migrations/keyToLanguage.ts`):
424
-
- Processes documents in batches of 100
425
-
- Uses optimistic locking (`ifRevisionID`) for safe concurrent execution
426
-
- Is idempotent - safe to run multiple times
427
-
- Skips items that already have a `language` field
428
-
429
361
## Migrate from objects to arrays
430
362
431
363
[See the migration script](https://github.com/sanity-io/sanity-plugin-internationalized-array/blob/main/migrations/transformObjectToArray.ts) inside `./migrations/transformObjectToArray.ts` of this Repo.
0 commit comments