question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Singleton with translations

See original GitHub issue

I’m trying to combine this plugin with the setup for singleton document described here. When I do this the translation tab disappears across all documents. I’m using document level translations.

Assuming the settings document needs to be translated too, how do I rewrite this to make it work?

import S from "@sanity/desk-tool/structure-builder";
 
export default () =>
  S.list()
    .title('Content')
    .items([
      S.listItem()
        .title('Site settings')
        .child(
          S.document()
            .schemaType('siteSettings')
            .documentId('siteSettings')
        ),
      // Add a visual divider (optional)
      S.divider(),
      // List out the rest of the document types, but filter out the config type
      ...S.documentTypeListItems()
        .filter(listItem => !['siteSettings'].includes(listItem.getId()))
    ])

Issue Analytics

  • State:open
  • Created 3 years ago
  • Reactions:2
  • Comments:18 (8 by maintainers)

github_iconTop GitHub Comments

3reactions
LiamMartenscommented, Jul 14, 2020

@jdwillemse No you can still only create the one with the fixed ID (aka singleton) because the “Create new” action is fixed to the ID with this structure. However I am aware it is slightly more cumbersome. I am talking to some guys at Sanity regarding conflicts with custom desk structure but have not been able to find a resolution yet.

1reaction
hacknugcommented, Jul 1, 2021

Good to know @saulhardman that it works well for you! Can you share your solution in this thread please?

This isn’t exactly what you asked for but here’s what my deskStructure.js looks like after setting i18n up a couple weeks ago:

// deskStructure.js

import S from '@sanity/desk-tool/structure-builder'
import * as Structure from 'sanity-plugin-intl-input/lib/structure'
import SocialPreview from 'part:social-preview/component'
// import { toPlainText } from 'part:social-preview/utils'
import { BiCookie } from '@hacknug/react-icons/bi'
import {
  RiTranslate, RiFileLine, RiFolderShieldLine, RiFileDownloadLine,
  RiBriefcaseLine, RiArticleLine, RiUserLine, RiPriceTagLine, RiLayoutBottomLine, RiSettingsLine,
} from '@hacknug/react-icons/ri'

const pagesItems = [
  { title: 'Home', id: 'frontpage', schema: 'frontpage' },
  { title: 'About Us', id: 'about', schema: 'about' },
  { title: 'Blog', id: 'blog', schema: 'blog' },
  { title: 'Contact', id: 'contact', schema: 'contact' },
]

export const getDefaultDocumentNode = (props) => {
  return (![
    ...pagesItems.map(({ schema }) => schema),
    'blogPost', 'blogAuthor', 'blogTag',
  ].includes(props.schemaType))
    ? S.document()
    : S.document().views([
      ...Structure.getDocumentNodeViewsForSchemaType(props.schemaType),
      S.view.component(SocialPreview({
        prepareFunction: ({ seo }) => ({
          title: seo?.seo_text,
          description: seo?.seo_desc,
          ogImage: seo?.seo_image,
          siteUrl: 'https://www.example.com',
        }),
      })).title('Social & SEO'),
    ])
}

export default () => {
  const items = Structure.getFilteredDocumentTypeListItems()
  const getLocalizedPosts = (type) => items.find((item) => [type].includes(item.spec.id)).getChild()

  return S.list().title('Content').items([
    ...pagesItems.map(({ id, title, schema, icon = RiFileLine }) => {
      return S.listItem().title(`${title} Page`).icon(icon).child(getLocalizedPosts(schema))
    }),

    S.divider(),

    S.listItem().title('Blog Articles').icon(RiArticleLine).child(getLocalizedPosts('blogPost')),
    S.listItem().title('Blog Authors').icon(RiUserLine).child(getLocalizedPosts('blogAuthor')),
    S.listItem().title('Blog Tags').icon(RiPriceTagLine).child(getLocalizedPosts('blogTag')),

    S.divider(),

    S.listItem().title('Site Settings').icon(RiSettingsLine)
      .child(S.document().schemaType('siteSettings').documentId('siteSettings').title('Site Settings')),

    S.divider(),

    items[0].title('Translation Manager').icon(RiTranslate),
  ])
}

It doesn’t keep the whole singleton implementation but it is okay in my case. I also ended up leaving these pageItems ungrouped (they were all previously grouped under a Pages folder).

Only reason for this is I didn’t manage to make everything work while keeping the same structure. The current implementation made it easy to keep everything I had in place pretty much the same so that’s a win if you ask me.

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to use Singleton for translation in java - Stack Overflow
I want to translate a small existing java project. I should put this code in a singleton class. Now I have created a...
Read more >
i18n'em all with Singleton - 50 Shades of Pink by KBDev
We can now call something like Translator.get('YES') from anywhere in our code by importing the module. Analyze logic behind the translation.
Read more >
Translate Pipe - Singleton - VMware
Use the translate pipe in your HTML templates to retrieve translations from the singleton service. API. L10n pipe. Get translation by key, will ......
Read more >
SINGLETON - Translation in English - bab.la
The singleton pattern is implemented by creating a class with a method that creates a new instance of the class if one does...
Read more >
Charles S. Singleton - Princeton University Press
Singleton's translation of The Divine Comedy, this work provides the English-speaking reader with everything he needs to read and understand the Paradiso. This ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found