@kaliber/sanity-plugin-multi-language
v4.4.0
Published
Document level translations.
Readme
Kaliber Sanity Multi Language Plugin
Document level translations.
Installation
> yarn add @kaliber/sanity-plugin-multi-languageUsage
sanity.config.js
import { addFields as addMultiLanguageFields } from '@kaliber/sanity-plugin-multi-language'
defineConfig({
plugins: [
deskTool({
defaultDocumentNode: (S, context) => {
const getClient = context.getClient.bind(context)
const views = [
S.view.form(),
typeHasLanguage(context) && S.view
.component(Translations)
.options({ multiLanguage, reportError })
.title('Translations'),
].filter(Boolean)
return S.document().views(views)
}
}),
],
schema: {
types: (prev, context) => prev
.concat(schemaTypes)
.map(addMultiLanguageFields({ multiLanguage: clientConfig.multiLanguage, reportError })),
},
})schema/documents/page.js
export const page = {
type: 'document',
name: 'page',
title: 'Page',
options: {
kaliber: {
multiLanguage: true,
...
},
...
},
...
}Custom Document ID Generator
By default, new translations are created with a random UUID (drafts.{uuid}). You can customize this behavior by providing a multiLanguageNewDocumentId function in your schema options:
export const page = {
type: 'document',
name: 'page',
title: 'Page',
options: {
kaliber: {
multiLanguage: true,
multiLanguageNewDocumentId: ({ currentId, currentLanguage, newLanguage }) => {
// Example: Replace language code in the document ID
return currentId.replace(new RegExp(`_${currentLanguage}$`), `_${newLanguage}`)
}
}
},
...
}Parameters:
currentId- The ID of the source documentcurrentLanguage- The language code of the source documentnewLanguage- The language code for the new translation
Returns: A string to be used as the new document ID
This is useful when you want predictable IDs based on language codes, making it easier to find related translations programmatically.
Config
config.multiLanguage
{
defaultLanguage: 'nl',
languages: {
nl: {
title: 'Dutch',
adjective: 'dutch',
language: 'nl',
icu: 'nl_NL'
},
en: {
title: 'English',
adjective: 'english',
language: 'en',
icu: 'en_US'
}
},
}Development
In this plugin:
> yarn
> yarn linkIn your project:
> yarn link @kaliber/sanity-plugin-multi-languagePublish
yarn publish
git push
git push --tags
