How to get pathname in a server component
Answered
MarkMiklos posted this in #help-forum
I would like to use next-intl t function to translate and provide a current path prop to the translation to display the page the user is currently on.
{t('admin.navbar', {pathname})}
usePathname only available on client components, and next-intl only works with server components.
Does anyone has a solution for this case by any chance?
{t('admin.navbar', {pathname})}
usePathname only available on client components, and next-intl only works with server components.
Does anyone has a solution for this case by any chance?
Answered by joulev
Yes then as explained in the link above, it’s fundamentally impossible. You must use a client component
17 Replies
Thanks! 🙂
what if my structure looks like this? as my understanding it doesn't go through [locale], as if i get the params I only get the locale key.
@MarkMiklos what if my structure looks like this? as my understanding it doesn't go through [locale], as if i get the params I only get the locale key.
All layout.tsx and page.tsx files in the screenshot should be able to access the locale from the params prop
In your structure, app/layout.tsx if exists will not get the locale but the four page/layout files there will get the locale
Maybe I wrote my question wrong, but I would like to access the pathname lets say Im on the admin page, so I would like to get /hu/admin or /en/admin, I do have the locale.
@MarkMiklos Maybe I wrote my question wrong, but I would like to access the pathname lets say Im on the admin page, so I would like to get /hu/admin or /en/admin, I do have the locale.
Oh you can’t, you have to construct it yourself
From the locale, you use a template string or something and build the pathname string
What do you exactly mean by build the pathname string?
For your case it would be
You need to do it manually
const pathname = `/${params.locale}/admin`;You need to do it manually
I see, If i do have a navbar that renders in the layout, i cannot hardcode it as i need to know what path I'm actually on, so this wont work in my case
Yes then as explained in the link above, it’s fundamentally impossible. You must use a client component
Answer
Yeah that's what I was afraid of, Im on my way to like the previous next versions better than this 😄
Thanks for your time.
helloo ^^ i'm sorry for directly pinging you but I was wondering if you could check out my post? my whole project is waiting on that and I'm afraid I'll lose motivation if I don't figue it out soon, thanks in advance ^