How to Translate Content and Use Different Images Across Languages

3 replies
Last updated: Dec 9, 2021
Hello, how should I create translation on the content blog sanity?
Under the content category, I tried to add the EN and FR following the video, but doesn't seem to work.

Also if I want to have 2 images en and fr, how should I add it in my schema?

please advice
🙏

{
          name: 'content',
          type: 'array',
          title: 'Content',
          of:[
            {
              type: 'block'
            },
            {
              name: 'test',
              type: 'object',
              title: 'test',
              fields: [
                {
                  title: 'English',
                  name: 'en',
                  type: 'string'
                },
                {
                  title: 'French',
                  name: 'fr',
                  type: 'string'
                }
              ]
            },
            {
              type: 'image',
              fields: [
                {
                  type: 'text',
                  name: 'alt',
                  title: 'Description',
                  options: {
                    isHighlighted: true
                  }
                }
              ],
              options: {
                hotspot: true
              }
            },
            {
              type: 'code'
            }
          ]
        }
Dec 9, 2021, 6:09 PM
I can use separate editor or same one, all I want is to have the content to have en and fr.
I tried to add this, but doesn't work:

{
              name: 'test',
              type: 'object',
              title: 'test',
              fields: [
                {
                  title: 'English',
                  name: 'en',
                  type: 'string'
                },
                {
                  title: 'French',
                  name: 'fr',
                  type: 'string'
                }
              ]
            },
Dec 9, 2021, 7:01 PM
Got it. First off, I'd pull your block content array out into its own reusable schema, like:
export default {
  name: 'blockContent',
  title: 'Block Content',
  type: 'array',
  of: [
    {
      type: 'block',
      marks: {
        annotations: [
          {
            name: "link",
            type: "object",
            title: "URL",
            fields: [
              {
                title: "URL",
                name: "href",
                type: "url",
              },
            ],
          }
        ]
      }
    },
    {
      type: 'image'
    },
  ]
}
Then, in your document you can set up an object that contains both an
en
and
fr
content schema. Like this:
{
      name: 'content',
      title: 'Content',
      type: 'object', 
      fields: [
        {
          title: 'English',
          name: 'en',
          type: 'blockContent'
        },
        {
          title: 'French',
          name: 'fr',
          type: 'blockContent'
        }
      ]
 },
Dec 9, 2021, 7:22 PM
okay thanks a lot
Dec 9, 2021, 7:51 PM

Sanity– build remarkable experiences at scale

The Sanity Composable Content Cloud is the headless CMS that treats content as data to power your digital business. Free to get started, and pay-as-you-go on all plans.

Was this answer helpful?

Categorized in