Language Switcher

In Methanol, multi-language support is file-system based. Any directory containing an index.mdx with a lang property in its frontmatter is identified as a language entry point.

Your theme template receives the following language-related properties:

Each entry includes routePath, routeHref, label, and code.

A standard implementation using a <select> element:

const LanguageSwitch = ({ ctx }) => {
	const languages = ctx.languages || []
	if (languages.length === 0) return null

	const currentHref = ctx.language?.routeHref
	return (
		<select
			aria-label="Select language"
			onchange="location.href=this.value"
			value={currentHref || undefined}
		>
			{languages.map((lang) => (
				<option value={lang.routeHref} selected={lang.routeHref === currentHref ? true : null}>
					{lang.label}
				</option>
			))}
		</select>
	)
}

A list-based implementation suitable for footers or headers:

const LanguageLinks = ({ ctx }) => {
	const languages = ctx.languages || []
	if (languages.length === 0) return null

	const currentHref = ctx.language?.routeHref
	return (
		<nav aria-label="Language selection">
			{languages.map((lang) => (
				<a href={lang.routeHref} aria-current={lang.routeHref === currentHref ? 'page' : null}>
					{lang.label}
				</a>
			))}
		</nav>
	)
}

Implementation Notes