npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

sanity-plugin-intl-input-manual-structure

v3.1.2

Published

[![npm version](https://img.shields.io/npm/v/sanity-plugin-intl-input.svg?style=flat)](https://www.npmjs.com/package/sanity-plugin-intl-input)

Downloads

6

Readme

Intl plugin for Sanity

npm version

What is this branch?

This is to solve a problem where you have conflicting desk structures.

How to implement

const {
    default: defaultStructure,
    getDefaultDocumentNode,
    getDocumentNodeViewsForSchemaType,
    getFilteredDocumentTypeListItems
} = require('sanity-plugin-intl-input/lib/structure');

// simpy re-exporting
module.exports.getDefaultDocumentNode = getDefaultDocumentNode;
module.exports.default = defaultStructure;

// or manual implementation
module.exports.getDefaultDocumentNode = (props) => {
    if (props.schemaType === 'myschema') {
        return S.document().views(getDocumentNodeViewsForSchemaType(props.schemaType));
    }
    return S.document();
};

module.exports.default = () => {
    const items = getFilteredDocumentTypeListItems();
    return S.list()
        .id('__root__')
        .title('Content')
        .items(items);
}

Default solution

When you want to create translations in Sanity they suggest following approach.
This definitely works, but makes the UI very clunky as you get more fields that require translations.

Default Solution

With intl-plugin

With the intl plugin you will get a cleaner UI for creating translatable documents as the translation is managed across multiple fields and it is even possible to manage document wide translations.

Simple translated object field

Intl Plugin Input

Document wide translation

Intl Plugin Document Translation

How to install

  1. Install the plugin using npm, yarn or the sanity cli yarn add sanity-plugin-intl-input sanity install intl-input
  2. Add the plugin in the sanity.json of your project
{
  "root": true,
  "project": {
    "name": "..."
  },
  "plugins": [
    "...",
    "intl-input" // <--
  ]
}

How to use

Intl object input

Enable translations for your object type field which will contain all the translatable fields in your document type.

export default {
    type: 'document',
    name: '...',
    title: '...',
    fields: [{
        name: '...',
        title: '...',
        type: 'object',
        options: {
            i18n: true, // enables localization
            base: '', // (OPTIONAL) id of the base language (if not passed the first one is considered base language)
            languages: ['..', '..', ...], // <-- eg. ['en', 'nl']
            css: (classNames) => ``, // (OPTIONAL) function to apply additional CSS for theming purposes. The classNames argument is an object with the css module classnames.
            messages: { // (OPTIONAL) You can pass a messages object to override the default messsages shown
              loading: 'Loading languages...',
              missingTranslations: 'Missing translations message...',
            },
        },
        fields: []
    }]
}

Intl for complete document

You can simply enable translations for your document type using the i18n parameter.

export default {
    type: 'document',
    name: '...',
    title: '...',
    i18n: {
      base: '', // (OPTIONAL) id of the base language (if not passed the first one is considered base language)
      languages: ['..', '..', ...], // <-- eg. ['en', 'nl']
      messages: { // (OPTIONAL) You can pass a messages object to override the default messsages shown
        loading: 'Loading languages...',
        missing: 'Missing',
        draft: 'Draft',
        publishing: 'Publishing...',
        publish: 'Publish'
      },
    },
    fields: []
}

Resulting data structure

Intl object input

Your resulting object will not look much different than before apart from the fact that it will now group it's content within the langauge keys. eg:

{
  "en": {
    "...": "..."
  },
  "fr": {
    "...": "..."
  }
}

Document wide translations

Each document will have a new property called __i18n_lang which contains the name of the language of this document. Secondly, the id will also contain a suffix as follows xxx-xxx-xx__i18n_{lang}.

Advanced languages

You can also pass a language objects or a GROQ query to the languages option for more advanced language handling.

Language objects

If you pass an object with name and title to the languages array you can separate the data key and the visualized language name in the UI. Eg:

languages: [
  { name: 'en_us', title: 'English (US)' },
  { name: 'en_gb', title: 'English (UK)' }
]

GROQ query

It is also possible to pass a GROQ query to the languages option to dynamically fetch the available languages. Eg:

languages: {
  query: '*[_type=="language"]{_id,name}',
  // these are the object paths to get out of the results to use for name and title
  // it is also possible to pass a simple string which will then be used for both title and name
  value: {
    title: 'name',
    name: '_id'
  }
}