Hi, I need to save the locale inside the page data. Also I created a Field "Locale" on it, and I'd like to create a hook to automaticaly set the field with the current Payload locale selected.
I started with this, and looking for the info inside "req", but I din't find it.
#Retrieve current locale used in field hooks
59 messages ยท Page 1 of 1 (latest)
Help is on the way! To mark it as solved, use the /solve command. In the meantime, here are some existing threads that may help you:
Documentation:
Community-Help:
You will need to create a custom component that uses the useLocale hook to read the current locale and then set that in state. Curious, what is the use case?
Damn there is already a hook, thanks ๐
I was looking for a way to fill my frontend url with the locale, do you know another way to do it?
my-domain.fr (should be FR index)
my-domain.fr/en (EN index)
but actually all my links are without locale
I can switch locale when I query Payload, all is working, but now my english version should have '/en' everywhere
I could serve 2 versions with differents locales with the same url, but its weird for SEO
Im wondering if this could be done with path segments, i.e. /[locale]/[slug]
if you are using file based routing
Im using /[lang]/[...slug] as you say, then I catch the current lang with Next.js params and use it to fetch Payload
I use a middleware to detect the browser favorite lang to redirect to the good locale
yeah so you might have to serve fr from /fr/page-slug
my defaultLocale is "FR", then I rewrite every /fr/slug, to stay at root domain -> /slug
when I want to turn to english version, I have add the "/en" after root domain
ok so whats the snag?
you will want to read the current [lang] var from NextJS, are you?
ok this sounds like it would work? What does not work for ya?
after this yes haha
after what? haha trying to follow
nothing, you just asked for the use case ๐ฅฒ
I try to explain mine haha
for sure, but I don't think you need the locale field in payload to make this work?
haha, but it sounds like you have all the pieces, without the custom locale field I feel like your setup would work!
yeah but how to generate urls, If I cant know what the locale's page?
but you do know the locale, from NextJS
and then you just fetch data from the API, using the locale
ok I detect, my current locale, and add this to urls with a prefix
how are you generating the sitemap? You could just loop over all of your locales and then create all urls
I can only have one sitemap for the whole site, then I have to fetch every pages, and articles
hmmmm
yeah I could fetch multiple times with a locales array
I try the way you do, I think you are more senior than me ๐
Thks ๐
You could fetch 1 time, and then just loop over locale array and build up all page urls for all locales
Glad your issue was resolved! :tada: If you want to help make payload better, please give us a :star: on GitHub and review us - It helps us a lot.
๐ Star Us on GitHub
๐ Review Us
Hi, I'm going to create my "local" field anyway. It's getting too complicated for me to manage only with NextJs (drill props are too important for some parts).
I've just created my custom field, I just wanted to know, by default I only get "Option" type props in my custom field? Can't I retrieve the rest of the content (name, description, options, etc.)?
I feel like you would use context and access the value from that? And then the only places you would need to know it are in your custom link component and your language selector?
Hi, unless I'm mistaken, custom fields only receive "Options", which contains a limited amount of information. I've added a second argument to retrieve the information from my field, but this creates unnecessary duplicates, which is not great. I've checked for the context, but it's normally only present in hooks?
Oh I meant on the frontend I would use context to avoid prop drilling
yep, but I think context is only available on 'client component'
, for the moment its working well, I'll see later If I can optimize more ๐