Accessing currentLocale in Studio/Document when dropdown lang is changed

8 replies
Last updated: Apr 11, 2023
Hi everyone, I have a case where I need to access the
currentLocale
from document-internationalization plugin inside the `Studio`/`Document`, is there away to get the local when the
dropdown
lang is change in document?
Apr 9, 2023, 11:54 AM
I do use
custom component
for my link (react-select) for the link field. Here is my query where I want to get the
currentLocale


  React.useEffect(() => {
    const referenceTypes = (referenceField?.type as ReferenceSchemaType)?.to?.map((t) => t.name)

    // query for all pages that are not drafts
    const query = `*[_type in $referenceTypes && uri != null && !(_id in path("drafts.**")) ] | order(uri.current asc)`

    sanityClient
      .fetch(query, { referenceTypes, baseLocale: 'en-us' }) // here its static from , I want it more dynamic.
      .then((results: PageQueryResult[]) => {
        const ids = results.map((result) => ({
          label: Array.isArray(result.title) ? result.name : result.title,
          value: result._id,
          subLabel: result?._type,
        }))
        setOptions(ids)
      })
  }, [referenceField?.type])
Apr 9, 2023, 11:56 AM
You can get the document values using the useFormValue hook :)
Apr 9, 2023, 2:51 PM
Seems this is only available in v3, I want it in v2 🙂
user A
Apr 10, 2023, 11:28 AM
Well avoiding upgrading to v3 only causes you more pain in the future imo. Anyways you can use the
withDocument
function: https://www.sanity.io/docs/migration-cheat-sheet#76d45626ec04
Apr 10, 2023, 11:35 AM
v2 has end of life in december btw https://www.sanity.io/help/studio-v2-vs-v3
Apr 10, 2023, 11:36 AM
Thanks a lot for the advice 🙂 I did refactor and moved to v3 ❤️
Apr 11, 2023, 7:45 AM
but got two questions:• I want inside studio when I do a query, I want to get the translated value of a field (using Language FIlter plugin), I found this
https://github.com/sanity-io/language-filter#state-management but how can I get access to the
langs url-param state
?• last question, I have a
page
document with
i18n
enabled, I want to access
__i18n_refs.length
inside the preview, before I upgrade it was working in v2 but now seems
i18n_refs
seems undefined_
Apr 11, 2023, 7:49 AM

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?