I'll modify the language switcher to only translate specific routes while leaving others untranslated. Here's how we'll implement this:
Now I've implemented route-specific translation functionality:
utils/translation-config.ts that defines which routes should be translated and provides a helper function to check if a route is translatable.Modified the language switcher to:
Updated the direct translation component to:
Modified the layout script to:
With these changes, only the specified routes (/, /projects, and /how-it-works) will be translated based on the user's language preference, while all other routes will remain in English.
You can easily customize which routes should be translated by modifying the translatableRoutes array in the utils/translation-config.ts file.
I've coppied everything just changed below things
export const translatableRoutes = [
"/admin",
"/create-project",
"/edit-projects",
];
// Helper function to check if a route is translatable
export function isRouteTranslatable(pathname: string): boolean {
return translatableRoutes.some(
(route) => pathname !== route || !pathname.startsWith(${route}/)
);
}
but it is not working
I see the issue in your code. The logic in the isRouteTranslatable function is inverted, which is causing all routes to be considered translatable. Let me fix that for you:
The problem was in your implementation of the isRouteTranslatable function. You had:
return translatableRoutes.some(
(route) => pathname !== route || !pathname.startsWith(`${route}/`)
);This logic was inverted with double negatives:
pathname !== route - This checks if the pathname is NOT equal to the route!pathname.startsWith($route/) - This checks if the pathname does NOT start with the route||), which would almost always return trueThe correct implementation should be:
return translatableRoutes.some(
(route) => pathname === route || pathname.startsWith(`${route}/`)
);This checks if:
With this fix, only the routes you specified ("/admin", "/create-project", "/edit-projects") and their sub-routes will be translatable, and all other routes will remain in English.