[I18N] Translating path segments #3143
Replies: 3 comments 12 replies
-
I'm actually working on adding support for route namespaces, which adds a prefix to the route ids. This will enable support for different paths mapping to the same route file. However, it will probably require patching remix core itself... I'm not 100% sure. |
Beta Was this translation helpful? Give feedback.
-
I'm trying to do exactly the same (in french too :D) Did you manage to do it "properly" ? |
Beta Was this translation helpful? Give feedback.
-
For now, what I've done is :
And a
which produce theses routes :
Which is nice and working. Then in the But I'm struggling with the Links as it needs to be generated. |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
I have been investigating the feasibility of using Remix for an upcoming project and have ran into a few issues around Internationalisation. Most if not all of the issues are mentioned here: #2877. However, I wanted to create a separate discussion specifically focused on translating URLs and path segments.
Goal
The goal here is to have a folder structure like:
Then given we have 2 supported locales
en-US
andfr-FR
, anden-US
is the default, the following routes would be set up:/en-us/about
would matchroutes/:locale/about.tsx
/fr-fr/qui-sommes-nous
would matchroutes/:locale/about.tsx
/en-us/qui-sommes-nous
would redirect to/en-us/about
/fr-fr/about
would redirect to/fr-fr/qui-sommes-nous
This would work for any number of path segment and should use the default locale when no translation exists. For example
/fr-fr/qui-sommes-nous/contact/map
if we only have translations forabout
andmap
we would redirect to/fr-fr/qui-sommes-nous/contact/carte
leavingcontact
as is assuming it is a valid route.Attempted solutions
Reading through the Remix docs and playing around with a prototype I came across 2 potential solutions:
Rewriting
req.url
req.url
in Express can be changed asreq.originalUrl
keeps the actual request url, allowing routing to be customised. So we could rewrite it in middleware to theen-US
version of the current request path. So for the 2nd example above we would rewrite it to/fr-fr/about
after any redirecting. Remix can now route to theabout.tsx
as usual even though the url does not actually match.This is specific to the Express adapter of course, but the main problem with this approach is that the client has no idea about the rewritten
req.url
, as it is using (I assume)window.location.href
and we can't change that because it will redirect the page. So Remix throws a bunch of strange errors because there is no matching route.Routes config option in
remix.config.js
The
routes
option inremix.config.js
allows you to define custom routes and point them at a module on the file system. However, it does not expose the current file based routes already registered. Meaning you cannot loop over the existing routes and add translated versions pointing to the same files. Furthermore, if you did have said route information it still doesn't work because of this bug: #1898The closest I could get was using https://github.com/kiliman/remix-mount-routes which re-implements the file based route logic and writing some complicated mapping to duplicate routes with translations in any segment. Even then it wouldn't work because of the aforementioned bug.
Potential Solutions
There are likely lots of ways to solve this problem, here are a few thoughts I have had so far:
routes
remix.config.js
option and fix the duplicate file id bug. I am not sure this would scale overly well, as it would attempt to translate every possible path segment and create a lot of permutations.:id
, it could be something like~about
and modules labelled as such could export a function with the available translations. Because this solution only flags some routes as translatable it reduces the amount of permutations and each route can handle its own translation sourcing like a loader does for data.slugs
option inremix.config.js
which gets given a slug and returns translations for the available locales. Like the first option this would have to be run for every path segment so likely won't scale well.To me option 2 seems like it would be the most remix-y option, so I will flesh out some pseudo code:
Beta Was this translation helpful? Give feedback.
All reactions