#Retrieve current locale used in field hooks

59 messages ยท Page 1 of 1 (latest)

alpine adder
#

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.

iron ridgeBOT
thick patrol
alpine adder
#

I was looking for a way to fill my frontend url with the locale, do you know another way to do it?

#

but actually all my links are without locale

thick patrol
#

hmmmmm

#

I feel like you could use rewrites to do this

alpine adder
#

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

thick patrol
#

Im wondering if this could be done with path segments, i.e. /[locale]/[slug]

#

if you are using file based routing

alpine adder
#

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

thick patrol
#

yeah so you might have to serve fr from /fr/page-slug

alpine adder
#

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

thick patrol
#

ok so whats the snag?

alpine adder
#

ok then I created a function to fill the <a> with relation page info

thick patrol
#

you will want to read the current [lang] var from NextJS, are you?

alpine adder
#

nop, its already done

#

just fill all my website links with the good locale prefix

thick patrol
#

ok this sounds like it would work? What does not work for ya?

alpine adder
#

after this yes haha

thick patrol
#

after what? haha trying to follow

alpine adder
#

I try to explain mine haha

thick patrol
#

for sure, but I don't think you need the locale field in payload to make this work?

alpine adder
#

๐Ÿคทโ€โ™‚๏ธ

#

If you know another way, I listen to you ๐Ÿ™‚

thick patrol
#

haha, but it sounds like you have all the pieces, without the custom locale field I feel like your setup would work!

alpine adder
#

yeah but how to generate urls, If I cant know what the locale's page?

thick patrol
#

but you do know the locale, from NextJS

#

and then you just fetch data from the API, using the locale

alpine adder
#

ok I detect, my current locale, and add this to urls with a prefix

thick patrol
#

yes 100%

#

only on your frontend though

alpine adder
#

hmm

#

then sitemap can't be generated

#

all urls would be with the current locale

thick patrol
#

how are you generating the sitemap? You could just loop over all of your locales and then create all urls

alpine adder
#

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 ๐Ÿ™

thick patrol
#

You could fetch 1 time, and then just loop over locale array and build up all page urls for all locales

iron ridgeBOT
alpine adder
thick patrol
#

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?

alpine adder
thick patrol
#

Oh I meant on the frontend I would use context to avoid prop drilling

alpine adder