This project is an example how to use routing with translations with react.
- React Router
- Package for dealing with routing in react
- installation
npm install react-router-dom@6
- React Intl
- package provide api to translate texts and format dates
- installation
npm i -S react-intl
- React Cookie
- Package for dealing with cookies. Last used language is stored in cookies.
- installation
npm install react-cookie
Project is divide into following folders:
- components - contains components mostly used for translations
LanguageSwitcher
- component switches languages and contains function to adjust pathnameLocalizedIntl
- extract language from URL and load intl based on itLocalizedLanguage
- component to translate and print Not Found messageDefaultNavigate
- component that navigates from base url(e.g localhost/) into localized url (localhost/en)NotFound
- component creates intl and takes language from cookie for not found component
- Localization - contains json files with language mutations - they must be in the format key:value ( should not have nested objects )
- utils - create of the Kontent delivery client object
- views
About
- about us pageHome
- home page prints datetimeMovies
- prints list of movies fetched from KontentMovie
- display fetched data from Kontent
index.tsx
contains routing and cookies setup and contains top level routing - routes to languages mutation, 404
app.tsx
renders language switcher navigation menu and contains routes between views
- in V6 now routes do not depend on position, the route is picked by best match
- language is stored in cookies in case url is changed to unknown language
useful link : https://reactrouter.com/docs/en/v6/upgrading/v5
- First install packages with
npm install
- Run the project with
npm run start