Security News
Introducing the Socket Python SDK
The initial version of the Socket Python SDK is now on PyPI, enabling developers to more easily interact with the Socket REST API in Python projects.
@sanity/language-filter
Advanced tools
A Sanity plugin that supports filtering localized fields by language
This is a Sanity Studio v3 plugin. For the v2 version, please refer to the v2 version.
A Sanity plugin that supports filtering localized fields by language
There are two popular methods of internationalization in Sanity Studio:
_id
This plugin adds features to the Studio to improve handling field-level translations.
For document-level translations you should use the @sanity/document-internationalization plugin.
npm install --save @sanity/language-filter
or
yarn add @sanity/language-filter
Add it as a plugin in sanity.config.ts (or .js), and configure it:
import {defineConfig} from 'sanity'
import {languageFilter} from '@sanity/language-filter'
export const defineConfig({
//...
plugins: [
languageFilter({
supportedLanguages: [
{id: 'nb', title: 'Norwegian (Bokmål)'},
{id: 'nn', title: 'Norwegian (Nynorsk)'},
{id: 'en', title: 'English'},
{id: 'es', title: 'Spanish'},
{id: 'arb', title: 'Arabic'},
{id: 'pt', title: 'Portuguese'},
//...
],
// Select Norwegian (Bokmål) by default
defaultLanguages: ['nb'],
// Only show language filter for document type `page` (schemaType.name)
documentTypes: ['page'],
filterField: (enclosingType, member, selectedLanguageIds) =>
!enclosingType.name.startsWith('locale') || selectedLanguageIds.includes(member.name),
})
]
})
Config properties:
supportedLanguages
can be either:
-- An static array of language objects with id
and title
. If your localized fields are defined using our recommended way described here (https://www.sanity.io/docs/localization), you probably want to share this list of supported languages between this config and your schema.
-- A function that returns a promise resolving to an array of language objects with id
and title
. This is useful if you want to fetch the list of supported languages from an external source. See Loading languages for more details.defaultLanguages
(optional) is an array of strings where each entry must match an id
from the supportedLanguages
array. These languages will be listed by default and will not be possible to unselect. If no defaultLanguages
is configured, all localized fields will be selected by default.documentTypes
(optional) is an array of strings where each entry must match a name
from your document schemas. If defined, this property will be used to conditionally show the language filter on specific document schema types. If undefined, the language filter will show on all document schema types.filterField
(optional) is a function that must return true if the field should be displayed. It is passed the enclosing type (e.g the object type containing the localized fields, the field, and an array of the currently selected language ids.
This function is called for all fields and in objects for documents that have language filter enabled.
Default: !enclosingType.name.startsWith('locale') || selectedLanguageIds.includes(field.name)
apiVersion
(optional) used for the Sanity Client when asynchronously loading languages.Languages must be an array of objects with an id
and title
.
languages: [
{id: 'en', title: 'English'},
{id: 'fr', title: 'French'}
],
Or an asynchronous function that returns an array of objects with an id
and title
.
languages: async () => {
const response = await fetch('https://example.com/languages')
return response.json()
}
The async function contains a configured Sanity Client in the first parameter, allowing you to store Language options as documents. Your query should return an array of objects with an id
and title
.
languages: async (client) => {
const response = await client.fetch(`*[_type == "language"]{ id, title }`)
return response
},
@sanity/language-filter
's asynchronous language loading does not currently support modifying the query based on a value in the current document.
Language filter can now be enabled/disabled directly from a schema, using options.languageFilter: boolean
.
When documentTypes
is omitted from plugin config, use options.languageFilter: false
in a document-definition to hide the filter button.
When documentTypes
is provided in plugin config, use options.languageFilter: true
in a document-definition to show the filter button.
Example:
export const myDocumentSchema = {
type: 'document',
name: 'my-enabled-language-filter-document',
/** ... */
options: {
// show language filter for this document type, regardless of how documentTypes for the plugin is configured
languageFilter: true,
},
}
MIT-licensed. See LICENSE.
This plugin uses @sanity/plugin-kit with default configuration for build & watch scripts.
See Testing a plugin in Sanity Studio on how to run this plugin with hotreload in the studio.
Run "CI & Release" workflow. Make sure to select the main branch and check "Release new version".
Semantic release will only release on configured branches, so it is safe to run release on any branch.
FAQs
A Sanity plugin that supports filtering localized fields by language
The npm package @sanity/language-filter receives a total of 20,363 weekly downloads. As such, @sanity/language-filter popularity was classified as popular.
We found that @sanity/language-filter demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 47 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
The initial version of the Socket Python SDK is now on PyPI, enabling developers to more easily interact with the Socket REST API in Python projects.
Security News
Floating dependency ranges in npm can introduce instability and security risks into your project by allowing unverified or incompatible versions to be installed automatically, leading to unpredictable behavior and potential conflicts.
Security News
A new Rust RFC proposes "Trusted Publishing" for Crates.io, introducing short-lived access tokens via OIDC to improve security and reduce risks associated with long-lived API tokens.