sanity-io / language-filter

A Sanity plugin that supports filtering localized fields by language
MIT License
17 stars 2 forks source link
team-ecosystem

@sanity/language-filter

This is a Sanity Studio v3 plugin. For the v2 version, please refer to the v2 version.

Field-level translation filter Plugin for Sanity.io

A Sanity plugin that supports filtering localized fields by language

Language Filter UI

What this plugin solves

There are two popular methods of internationalization in Sanity Studio:

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.

Installation

npm install --save @sanity/language-filter

or

yarn add @sanity/language-filter

Usage

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:

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.

Changes in V3

documentTypes

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,
  },
}

License

MIT-licensed. See LICENSE.

Develop & test

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.

Release new version

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.