Skip to content

Routing and i18n #10510

Closed Answered by dgrcode
dgrcode asked this question in Q&A
May 19, 2023 · 3 comments · 2 replies
Discussion options

You must be logged in to vote

I ended up coding a custom solution. It's a bit more complex than I was hoping for, but hopefully straightforward enough.

Please, comment on this discussion if you found a better approach.

Solution

There are a few changes involved. First, declare two constants to make references to the default and supported languages.

// constants.js

export const DEFAULT_LANG = "en";
export const SUPPORTED_LANGS = ["es", "fr"];

Then create a custom hook to find if we were in a default-language url –i.e. /one–, or a supported-language url –i.e. /es/one–.

// useUrlLang.jsx

import { useRouteMatch } from "react-router-dom";
import { DEFAULT_LANG, SUPPORTED_LANGS } from "../helpers/constants";

const useUrlLang

Replies: 3 comments 2 replies

Comment options

You must be logged in to vote
0 replies
Answer selected by dgrcode
Comment options

You must be logged in to vote
2 replies
@dgrcode
Comment options

@huijiewei
Comment options

Comment options

You must be logged in to vote
0 replies
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
4 participants