Setting Up Button Routing to Use Dynamic Localized Pathnames in Next.js
Unanswered
Sun bear posted this in #help-forum
Sun bearOP
I have a "My Profile" button which needs to link towards the correct localized route, as I have defined it in my pathnames and rewrites.
Currently the button works like this:
- For the English locale:
http://localhost:3000/en/user/id
- For the Romanian locale:
http://localhost:3000/ro/user/id
I want it to work like this instead:
- For the English locale:
http://localhost:3000/en/user/id
- For the Romanian locale:
http://localhost:3000/ro/utilizator/id
Here my set-up/code:
Currently the button works like this:
- For the English locale:
http://localhost:3000/en/user/id
- For the Romanian locale:
http://localhost:3000/ro/user/id
I want it to work like this instead:
- For the English locale:
http://localhost:3000/en/user/id
- For the Romanian locale:
http://localhost:3000/ro/utilizator/id
Here my set-up/code:
//Sidebar & Button related code
import React from 'react';
import { useRouter, usePathname, pathnames } from '@/src/navigation';
import { useLocale, useTranslations } from 'next-intl';
const JobSeekerSidebar: React.FC<JobSeekerSidebarProps> = () => {
const router = useRouter();
const pathname = usePathname();
const handleButtonClick = (path: keyof typeof pathnames) => {
router.push(path);
};
..//
return (
<IconButton
onClick={() => handleButtonClick(`/user/${user?.id}` as keyof typeof pathnames)}
isActive={pathname === `/user/${user?.id}`}
/>// navigation.ts
import { createLocalizedPathnamesNavigation, Pathnames } from "next-intl/navigation";
export const locales = ["en", "ro"] as const;
export type Locale = typeof locales;
export const localePrefix = "always";
export const pathnames = {
"/user/:userId": {
en: "/en/user/:userId",
ro: "/ro/utilizator/:userId",
},
} satisfies Pathnames<typeof locales>;
export const {
Link,
redirect,
usePathname,
useRouter,
getPathname,
} = createLocalizedPathnamesNavigation({ locales, pathnames });//next config:
import createNextIntlPlugin from 'next-intl/plugin';
const withNextIntl = createNextIntlPlugin();
/** @type {import('next').NextConfig} */
const nextConfig = {
async rewrites() {
return [
{
source: '/ro/utilizator/:userId',
destination: '/ro/user/:userId',
},
{
source: '/en/user/:userId',
destination: '/en/user/:userId',
},
];
},
};
export default withNextIntl(nextConfig);10 Replies
Sun bearOP
I'm almost there solving this one too, the only issue left is that the locale gets duplicated now http://localhost:3000/ro/ro/utilizator/3a969cff-0284-4043-b97b-dfc2eaad8a2a
Sun bearOP
I solved it, finally.
Mugger Crocodile
Hey, I currently assume the same issue with next-intl. What was your solution? 😄
@Mugger Crocodile Hey, I currently assume the same issue with next-intl. What was your solution? 😄
Sun bearOP
Hello! My solution was to not use a dynamic path when navigating to the My Profile page 😁
Because I decided that I wanted the url to look like this:
Instead of
Because I decided that I wanted the url to look like this:
domain.com/en/my-profileInstead of
domain.com/en/my-profile/idHowever, I did make it work like I wanted it to work initially, as in
So let me know which method would you be interested in in your case and I'll do my best to share how I did it. Keep in mind I'm not a programmer so I'll not be able to explain exactly how it works, I pretty much solved this stuff using Chat GPT and Claude 😁
domain.com/en/my-profile/id and I kept a copy of the code because I think I'll have to use that method for when someone else wants to visit another user's profile.So let me know which method would you be interested in in your case and I'll do my best to share how I did it. Keep in mind I'm not a programmer so I'll not be able to explain exactly how it works, I pretty much solved this stuff using Chat GPT and Claude 😁
Mugger Crocodile
Oh I need this for a dynamic route. I got an product detail page in
So in my pathnames in the
But my next-route seems to overwrite it when I go to the german product page and it doesn't recognize this. Nextjs just routes to my [...slug] page, which is provided if no page file fits the current url.
/[locale]/products/[id] and I want to reach it by /de_DE/produkte/123 and /en_GB/products/123.So in my pathnames in the
routing.ts for next-intl I added this: "/product-detail/[id]": {
en_GB: "/product-detail/[id]",
de_DE: "/produkt-detail/[id]",
}, But my next-route seems to overwrite it when I go to the german product page and it doesn't recognize this. Nextjs just routes to my [...slug] page, which is provided if no page file fits the current url.
Sun bearOP
//removed