Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to translate react-router Route paths

My application has more than one locale (it, en).

I need to translate all the routes. For example I have the terms and condition page that has to paths (one per locale):

  • it/termini
  • en/terms

I need than to do something like:

// routes.js

const routes = (
  <Route path="/" component={App}>
    <IndexRoute component={HomePage} />
    <Route path="(it/termini)(en/terms)" component={TermsPage} />
    <Route path="*" component={NotFoundPage} />
  </Route>
)

As you can see this funky solution is not so good for the scalability of the application.

like image 794
cl0udw4lk3r Avatar asked Aug 22 '16 12:08

cl0udw4lk3r


People also ask

How do I get the current route in react router?

Getting current route using hooks With the react router v5, we can use the useLocation () hook to get the current route path in a router component.

Are routes objects or props in react router 4?

Previous versions of React Router represented routes as objects; while that syntax is gone with React Router 4, it’s pretty easy to throw together something similar. You probably noticed that routes tend to take the same kind of props. Let’s represent them as objects:

What are nested routes in react router?

To recap, nested routes allow you to, at the route level, have a parent component control the rendering of a child component. Twitter's /messages route is the perfect example of this. With React Router, you have two options for creating nested routes.

What is the difference between previous versions of react router?

Previous versions of React Router represented routes as objects; while that syntax is gone with React Router 4, it’s pretty easy to throw together something similar. You probably noticed that routes tend to take the same kind of props. Let’s represent them as objects: Using this object, you can simplify App:


1 Answers

My current approach with routes localization is to deal with them as I do with any localized content. In your case I would do:

// routes.js

function createRoutes(language) {
   /*
     You'll probably have more work to do here, 
     such as sub-routes initialization
     component's type selection logic, etc.

     @note: _t(key, language) is your 
            translation function
   */

   return (
       <Route 
          key={language} 
          path={_t("it/termini", language)} 
          component={TermsPage} 
       />
   )
}

let localizedRoutes = supportedLanguages.map(createRoutes)

const routes = (
  <Route path="/" component={App}>
    <IndexRoute component={HomePage} />
    {localizedRoutes}
    <Route path="*" component={NotFoundPage} />
  </Route>
)

Then you can specify them in your translation files just as any other string, including any parameter:

// en.js

module.exports = {
//...
  "it/termini" : "en/terms",
  "it/profilo/:userId" : "en/profile/:userId"
//...
}

You can also assemble them on the fly before your routes are defined, associating them to the corresponding translation key.

In this way it/termini becomes just the key of your translated URL, you could also use something not resembling the underlying URL like terms-page-url.

This method also allows you to differentiate route components and/or sub routes per language, which is an added bonus. Just implement the logic inside your mapping function (or where it's appropriate for your application).

like image 69
Daniele Rapagnani Avatar answered Oct 01 '22 14:10

Daniele Rapagnani