#Accessing current locale

8 messages · Page 1 of 1 (latest)

ionic bramble
#

Is there a way to access what locale the payload UI is currently using? In some cases I can grab it from the query param in the url, but that's not always present.

dark runeBOT
whole pebble
#

I'm not exactly sure what your context is for this (server or client) and my only experience as of now with payload is for an in-house customer management system, so I haven't had to deal with client Locale at all while using payload. However, as far as JavaScript goes you can use navigator.language or navigator.languages https://developer.mozilla.org/en-US/docs/Web/API/Navigator/languages

Server side you can use req.locale, it should be available in any hook or anywhere else you have access to the req object.

MDN Web Docs

The Navigator.languages read-only property
returns an array of strings representing the user's preferred
languages. The language is described using language tags according to
RFC 5646: Tags for Identifying Languages (also known as BCP 47). In the returned
array they are ordered by preference with the most preferred language first.

ionic bramble
#

Ah, thank you for the help! I'm on the client, and navigator.language doesn't get me what Payload is set to.

Context is that I'm managing transactional emails for multiple tenants, and so I have a collection with default emails, and a collection for overrides.

When the user chooses an email to override, I am prepopulating the value of the override with the default. The issue is that I don't know what locale the user is working with in Payload, so I don't know which locale to grab from in the default.

#

navigator.language gets me the locale that the browser is using, but I want the locale that payload is editing.

#

I'm populating the value in an onChange, so I don't have access to req or anything. :/

#

I assumed there'd be a hook I could use, but I haven't had luck finding the locale in any of the hook responses.

#

⭐ OK wow, I found it!

import { useLocale } from 'payload/components/utilities'

I didn't know about /components/utilities, but that had just what I needed. Thanks for the help @whole pebble ❤️