Skip to content

How to use URL path for language detection? #135

Closed Answered by sergiodxa
TimoWestland asked this question in Q&A
Discussion options

You must be logged in to vote

You can access that from the params you get in your loaders and actions, or using useParams hook in the UI.

In the root loader:

export async function loader({ request, params }: DataFunctionArgs) {
  // use params.locale, if it's undefined (optional segments) fallback to i18next.getLocale
  let locale = params.locale ?? await i18next.getLocale(request);
  return json({ locale })
}

Or if you don't want to use i18next.getLocale you can access it direct in the root component.

export default function Component() {
  let locale = useParams().locale ?? "en" // fallback in case of optional segments
  // more code
}

Replies: 3 comments 3 replies

Comment options

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

You must be logged in to vote
1 reply
@sergiodxa
Comment options

Comment options

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

@TimoWestland
Comment options

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants