Lang Provider

Context provider for language and internationalization.

Report a bug

Preview

Switch between light and dark to inspect the embedded Storybook preview.

Installation

pnpm dlx shadcn@latest add https://ui.vllnt.ai/r/lang-provider.json
bash

Storybook

Explore all variants, controls, and accessibility checks in the interactive Storybook playground.

View in Storybook

Code

"use client";

import { useEffect } from "react";

import { usePathname } from "next/navigation";

import type { SupportedLanguage } from "../../lib/types";

type LangProviderProps = {
  defaultLanguage?: SupportedLanguage;
  supportedLanguages?: SupportedLanguage[];
};

export function LangProvider({
  defaultLanguage = "en",
  supportedLanguages = ["en", "fr"],
}: LangProviderProps) {
  const pathname = usePathname();

  useEffect(() => {
    // Extract language from pathname - matches /en, /fr, /en/, /fr/, etc.
    const langMatch = /^\/([a-z]{2})(?:\/|$)/.exec(pathname);
    const lang =
      langMatch &&
      supportedLanguages.includes(langMatch[1] as SupportedLanguage)
        ? (langMatch[1] as SupportedLanguage)
        : defaultLanguage;

    // Update the HTML lang attribute
    document.documentElement.setAttribute("lang", lang);
  }, [pathname, defaultLanguage, supportedLanguages]);

  return null;
}
typescript

Dependencies

  • @vllnt/ui@^0.2.1