hozana / next-translate-routes

Flexible and translated routes for Next.js without custom server
MIT License
115 stars 30 forks source link

URL path does not update when switching the language #27

Closed samvdst closed 2 years ago

samvdst commented 2 years ago

Steps to reproduce:

ozcancelik commented 2 years ago

You have to send props (your page name) to to your header.

In your example:

1- Change your LanguageSwicther component as :

import { Link } from "next-translate-routes";
import { useRouter } from "next/router";
import React from "react";

function LanguageSwitcher(props) {
  const router = useRouter();
  const LANGS = ["en", "de"];
  return (
    <div>
      {LANGS.map((lang) => (
        <Link
          key={lang}
          href={{
            pathname: locale === "en" ? props.enPath : props.dePath,
          }}
        >
          <a>{lang}</a>
        </Link>
      ))}
    </div>
  );
}

export default LanguageSwitcher;

2- Use any page with props. Example your causes.js

<LanguageSwitcher enPath="causes" dePath="spendenprojekte" />

I hope it helps.

cvolant commented 2 years ago

@samvdst You can also change only one line in your code to make it work:

function LanguageSwitcher() {
  const { pathname, query } = useRouter();
  const LANGS = ["en", "de"];
  return (
    <div>
      {LANGS.map((lang) => (
        <Link key={lang} href={{ pathname, query }} locale={lang}> // Here
          <a>{lang}</a>
        </Link>
      ))}
    </div>
  );
}