T
TanStack3mo ago
sunny-green

How can I alias a route.

Hi there, I am trying to add translations to my site, and I'm struggling with the route paths. For example, I would like to do the following: - /es/contacto - /en/contact This 2 routes pointing to one specific file. for example ./src/routes/contact.tsx who exports the route. I tryied to use virtual file routes, which sounds awesome to my ears for this use case, but it complains i can't duplicate the same route for 2 paths. "The symbol "contactRoute" has already been declared" Is there a way to achieve this?
8 Replies
subsequent-cyan
subsequent-cyan3mo ago
why dont you add a variable ? $locale/$page or similar, but in case of translating segments you have to have this mapped somewhere i would recommend not translating segments as they dont contribute anything to seo this days, generals users often dont see the full url either :p
sunny-green
sunny-greenOP3mo ago
Ey @nikus thanks for the quick reply. The routing I have achieved using the basepath as some people mentioned in another thread. So /es/contact and /en/contact I already have it working just fine. I could ignore ignore the url localization and go without it as you mentioned, but I still find odd not being able to alias a route to a different path. What's the point of virtual file routes then? If I proceed with $locale/$page, then I lose part of the magic of tanstack.
subsequent-cyan
subsequent-cyan3mo ago
yeah in theory you should be able to register /$locale/contatc.tsx i dont see why it wouldnt work, make sure to wipe the routeTree.gen or delete it and restart the dev server use the $locale to forward the correct locale and validate it and pass it to the next tree :p
sunny-green
sunny-greenOP3mo ago
Yup /$locale/contact I got it working, I was trying to figure it out if I can do 2 different route paths for the same file route, but I understand not possible. So I'll take your advice and just go for /en/contact and /es/contact -> /$locale/contact.tsx for now. Is there a features upvote/downvote that you guys have?
subsequent-cyan
subsequent-cyan3mo ago
I recommend to open an issue on github with minimal repo, i believe also 2 different paths could make use of the same file indeed, i havent tested this myself.
Yeah go with that segments for now, i would do the same, i have done translated segments before in other framework and its a pain to maintain but also to work it. Say you are at es/contacto and you want to switch the language while staying in same page, you have to check your mapper always to see whats the “equivalent” for english and so on. While in 2025 its purely cosmetic, not wroth the effort in my opinion
sunny-green
sunny-greenOP3mo ago
Totally, but google ads is still bothering me with those paths or I did not find a better way just yet. I believe we are near the end of them, but still gotta maintain them for a little while longer, hopefully not too long. 😅
subsequent-cyan
subsequent-cyan3mo ago
good luck 🤞 hehe
fascinating-indigo
fascinating-indigo3mo ago
route aliasing is not supported (yet) might be a nice feature, but pretty low priority IMO

Did you find this page helpful?