I'm building a multilingual site in React and I'm using react router for my routing. Right now I have it setup where the prefix has to be present in order to transition to the route.
What I'm trying to do is the following: When I go to localhost:3000 I want my app to transition to the home component. And when I go to localhost:3000/jp I still want to transition to the home component except now my language prefix would be jp.
I want English to be the default language and for other languages they have to be present in the prefix. Right now it only transitions to the home component if I enter localhost:3000/en.
Is there a way to accomplish this?
import React, { Component } from 'react';
import { Route, Switch } from "react-router-dom";
import { Home } from '../containers/home';
import { About } from '../containers/about';
import { Contact } from '../containers/contact';
export default class Routes extends Component {
render(){
return(
<Switch>
<Route path="/:lang/about" component={About} />
<Route path="/:lang/contact" component={Contact} />
<Route path="/:lang/" component={Home} />
</Switch>
);
}
}
basename: stringThe base URL for all locations. If your app is served from a sub-directory on your server, you'll want to set this to the sub-directory.
The use of history and useHistory is deprecated and should be replaced with the useNavigate hook.
Thus, we found that a lot of the packages were deprecated and needed to be replaced ASAP. One of the biggest surprises for us was the deprecation of react-router-redux .
BrowserRouter: BrowserRouter is a router implementation that uses the HTML5 history API(pushState, replaceState and the popstate event) to keep your UI in sync with the URL. It is the parent component that is used to store all of the other components.
To create the first route using React Router library, open src/App.js file and add the following import statement: // after other import statements import { BrowserRouter as Router } from 'react-router-dom'; This is the first component to import from the react-router-dom library. It is used to wrap different routes.
The <Route> component is the most important component in React Router. It renders some UI if the current location matches the route’s path. Ideally, a <Route> component should have a prop named path, and if the path name matches the current location, it gets rendered.
It is used to wrap different routes. It uses the HTML5 history API to keep track of routes history in the React app. The Router part in the above snippet is the alias that makes it easier to write. It is recommended to import and use it at the top level component in a React app’s component hierarchy:
Using React Router, when you want to create a Route that uses a URL parameter, you do so by including a : in front of the value you pass to Route 's path prop. Finally, to access the value of the URL parameter from inside of the component that is rendered by React Router, you can use React Router's useParams Hook. ... Want to learn more?
Just add a Redirect
at the end which will be matched when nothing else does and it will redirect to the /en
import React, { Component } from 'react';
import { Route, Switch, Redirect } from "react-router-dom";
import { Home } from '../containers/home';
import { About } from '../containers/about';
import { Contact } from '../containers/contact';
export default class Routes extends Component {
render(){
return(
<Switch>
<Route path="/:lang/about" component={About} />
<Route path="/:lang/contact" component={Contact} />
<Route path="/:lang/" component={Home} />
<Redirect to="/en" />
</Switch>
);
}
}
Demo at https://codesandbox.io/s/18rm8k82lj
Updated answer (due to comment)
The problem is that the /:lang/
will match /about
and the lang will be set to about
.
A solution is to use the render
prop of the route and decide what you want to do there
export default class Routes extends Component {
render() {
const supportedLanguages = ["en", "jp", "de", "es"];
return (
<Switch>
<Route path="/:lang/about" component={About} />
<Route path="/:lang/contact" component={Contact} />
<Route
path="/:lang/"
render={props =>
supportedLanguages.includes(props.match.params.lang) ? (
<Home {...props} />
) : (
<Redirect to={`/en/${props.match.params.lang}`} />
)
}
/>
</Switch>
);
}
}
Demo at https://codesandbox.io/s/k2n9997345
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With