Why are field-level translations not recommended for PortableText?
field-level translationsis not recommended for content using
PortableText. Has anyone tried / knows why this is the case?Source:
https://www.sanity.io/plugins/document-internationalization
Portable Text has a deep data structure. When you translate it at the field level, every one of its paths is copied once for each language, so the number of attributes in your dataset grows quickly toward the attribute limit. Sanity's docs recommend document-level localization for content that uses Portable Text.
Why Portable Text and field-level objects don't mix well
An attribute is a unique combination of path and data type in your dataset. Each path counts once, however many documents use it. The attribute limit depends on your plan: 2,000 on Free, and 10,000 on Growth and Enterprise. There's currently no pay-as-you-go option for extra attributes.
A block content field already creates a lot of paths:
- the array itself
- the block objects inside it
- the
markDefsandchildrenarrays in each block - the
spanobjects inchildren, each withmarksandtext - the annotation objects in
markDefs, plus any custom types you add
If you wrap fields in a language object (body.en, body.fr, body.es …), each language copies that whole set of paths. The attribute-limit docs say: "This multiplies the number of attributes by the number of languages added." Nesting makes it worse, for example Portable Text inside custom objects or page builders.
Recommended: document-level translation
Create one document per language and link the versions with references. A new language reuses the same fields, so it doesn't add attributes. You can also publish each language on its own schedule. The localization docs list this approach as "best for translating content using Portable Text."
import {defineType, defineField} from 'sanity'
export const articleType = defineType({
name: 'article',
title: 'Article',
type: 'document',
fields: [
defineField({
name: 'language',
type: 'string',
options: {list: [{title: 'English', value: 'en'}, {title: 'Spanish', value: 'es'}]},
}),
defineField({name: 'title', type: 'string'}),
defineField({name: 'body', type: 'array', of: [{type: 'block'}]}),
],
})The @sanity/document-internationalization plugin sets the language field for you. It also creates a translation.metadata document that references all the translations. To query a language and its translations:
*[_type == "lesson" && language == $language]{
title, slug, language,
"_translations": *[_type == "translation.metadata" && references(^._id)].translations[].value->{title, slug, language}
}If you need field-level translation
You can choose a strategy per schema, so a project can use both. For field-level translation, use the internationalized-array plugin rather than objects. Each array item stores language and value, so the paths stay fixed (title, title[], title[]._key, title[].language, title[].value) however many languages you add. The plugin supports Portable Text through fieldTypes. The Portable Text structure itself still counts once toward the limit.
Since v5, the plugin stores the language in the language field, and _key is a random stable key. Update older queries from title[_key == "en"][0].value to:
"title": title[language == $language][0].valueSending documents to a translation service
Document-level translation fits external translation workflows. The localization docs list these options:
- GROQ-powered webhooks to send a document to a translation service's API. When the translation is done, the service writes it back to your dataset, for example through a webhook it triggers.
- Document actions to duplicate a document into another locale, or to send its JSON to a service.
- Existing adapters for Transifex (
sanity-plugin-transifex) and Smartling (sanity-plugin-studio-smartling). For any other provider, build your own integration using these as a model. - AI Assist's "Translate document" action for translation by large language models (LLMs).
Show original thread10 replies
Was this answer helpful?
Sanity – Build the way you think, not the way your CMS thinks
Sanity is the developer-first content operating system that gives you complete control. Schema-as-code, GROQ queries, and real-time APIs mean no more workarounds or waiting for deployments. Free to start, scale as you grow.