Skip to content

Instantly share code, notes, and snippets.

@ashour
Created February 28, 2020 16:35
Show Gist options
  • Select an option

  • Save ashour/11de47fd0fedec212b795bb5816cb2ca to your computer and use it in GitHub Desktop.

Select an option

Save ashour/11de47fd0fedec212b795bb5816cb2ca to your computer and use it in GitHub Desktop.
import React from "react";
import { useIntl } from "react-intl";
import { supportedLangs } from "./i18n";
export default function LangSwitcher() {
const { locale: lang } = useIntl();
return (
<div className="navbar-item has-dropdown is-hoverable">
<a className="navbar-link">{supportedLangs[lang]}</a>
<div className="navbar-dropdown is-right">
{Object.keys(supportedLangs).map(code => (
// We want absolute URLs here, e.g. /en, so that our app
// will reload and switch to the selected language. So we
// use <a> instead of React Router's <Link>, since the
// latter will always prefix its URLs with the basename
// (the current language).
<a
key={code}
href={`/${code}`}
className={`navbar-item ${code === lang ? "is-active" : ""}`}
>
{supportedLangs[code]}
</a>
))}
</div>
</div>
);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment