npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

lang-provider

v1.0.5

Published

A flexible React provider for managing multilingual applications with **TypeScript** support and **automatic HTML lang attribute management**.

Readme

lang-provider

A flexible React provider for managing multilingual applications with TypeScript support and automatic HTML lang attribute management.

Features

  • 🌍 Multi-language support
  • 🎯 Automatic HTML lang attribute updates
  • 🔧 Flexible configuration with custom translation loading
  • 📱 SSR-safe - works in both browser and server environments
  • 🎨 Zero dependencies (except React)
  • 🔥 Lightweight - minimal bundle size

Installation

npm install lang-provider

Quick Start

Follow these steps to get started with using lang-provider:

// Define your translations
const translations = {
  sv: {
    welcome: "Välkommen",
    portfolio: "Min portfölj"
  },
  en: {
    welcome: "Welcome",
    portfolio: "My Portfolio"
  }
};

// Create typed config
const config = {
  defaultLanguage: "sv" as const,  // Type assertion for literal type
  languages: ["sv", "en"] as const,  // Type assertion for tuple type
  translations
};

// Infer types from config
type AppLanguages = typeof config.languages[number];  // Results in "sv" | "en"
type TextKeys = keyof typeof config.translations.sv & string;  // Results in "welcome" | "portfolio"

const App = () => (
  <LangProvider<AppLanguages, TextKeys> config={config}>
    <TextProvider<AppLanguages, TextKeys> config={config}>
      <YourApp />
    </TextProvider>
  </LangProvider>
);

// 🎯 HTML lang attribute is automatically set to "sv" when LangProvider mounts
// 🔄 Use switchLanguage('en') to change both state and <html lang="en">

Configuration

Translation File Structure

{
    "sv": {
        "welcome": "Välkommen",
        "portfolio": "Min portfölj"
    },
    "en": {
        "welcome": "Welcome", 
        "portfolio": "My Portfolio"
    }
}

Config Interface

interface LanguageConfig<L extends string, K extends string> {
  defaultLanguage: L;              // The fallback language
  languages: readonly L[] | L[];   // Array of supported languages
  translations: Record<L, Record<K, string>>; // Translations map
}

Hooks

useLang

Returns language control functions and state:

const { 
  language,           // Current active language
  switchLanguage,     // Function to change language: (lang: L) => void
  availableLanguages  // Array of supported languages: L[]
} = useLang();

useText

Returns text management functions:

const { 
  getText,    // Get translated text: (key: K) => string
  setTexts    // Update translations: (texts: Record<L, Record<K, string>>) => void
} = useText();

Usage Examples

Switching Languages

import { useLang } from "lang-provider";

const LanguageSelector = () => {
  const { switchLanguage, availableLanguages, language } = useLang();
  
  return (
    <div>
      {availableLanguages.map(lang => (
        <button 
          key={lang}
          onClick={() => switchLanguage(lang)}
          disabled={lang === language}
        >
          {lang.toUpperCase()}
        </button>
      ))}
    </div>
  );
};

Displaying Translated Text

import { useText } from "lang-provider";

const Welcome = () => {
  const { getText } = useText();
  return <h1>{getText('welcome')}</h1>;
};

Update Translations

The setTexts function from useText allows you to update translations at runtime. Here's a complete example:

import { useText } from "lang-provider";
import type { LanguageConfig } from "lang-provider";

// Define your types
type Languages = "en" | "sv";
type TextKeys = "welcome" | "portfolio";

const TranslationUpdater = () => {
  const { setTexts } = useText();

  const updateTranslations = async () => {
    try {
      // Fetch new translations from your API/source
      const response = await fetch('https://api.example.com/translations');
      const newTranslations: LanguageConfig<Languages, TextKeys>['translations'] = await response.json();
      
      // Validate the structure matches your types
      if (isValidTranslation(newTranslations)) {
        setTexts(newTranslations);
      }
    } catch (error) {
      console.error('Failed to update translations:', error);
    }
  };

  // Type guard to validate translation structure
  const isValidTranslation = (
    data: unknown
  ): data is Record<Languages, Record<TextKeys, string>> => {
    if (!data || typeof data !== 'object') return false;
    
    const requiredLanguages: Languages[] = ['en', 'sv'];
    const requiredKeys: TextKeys[] = ['welcome', 'portfolio'];
    
    return requiredLanguages.every(lang => 
      typeof data[lang] === 'object' && 
      requiredKeys.every(key => 
        typeof data[lang][key] === 'string'
      )
    );
  };

  return <button onClick={updateTranslations}>Update Translations</button>;
};

Your translation API should return data in this format:

{
    "sv": {
        "welcome": "Välkommen",
        "portfolio": "Min portfölj"
    },
    "en": {
        "welcome": "Welcome", 
        "portfolio": "My Portfolio"
    }
}

The setTexts function will:

  1. Update the internal translation state
  2. Trigger a re-render of components using getText
  3. Preserve type safety with the provided generic types

Automatic HTML Lang Attribute

The lang-provider automatically updates the HTML document's lang attribute when the language changes.

How it Works

When you use switchLanguage(), the provider automatically:

  1. Updates the component state with the new language
  2. Sets <html lang="..."> attribute to match the selected language
  3. Validates the language is in the allowed list before changing
const { switchLanguage } = useLang();

// This will:
// 1. Change the internal language state to 'en'
// 2. Set <html lang="en"> on the page
switchLanguage('en');

// This will:
// 1. Change the internal language state to 'sv'  
// 2. Set <html lang="sv"> on the page
switchLanguage('sv');

// This will do nothing (invalid language)
switchLanguage('fr'); // Not in supported languages

Benefits

  • 🎯 Accessibility: Screen readers and assistive technologies get correct language information
  • 🌐 Browser Features: Form validation, spell-check, and autocomplete use the correct language
  • 🔍 SEO: Search engines receive proper language metadata
  • 🔄 Translation: Browser translation features work more accurately

Server-Side Rendering (SSR)

The HTML lang functionality is SSR-safe:

  • Works in browser environments (sets HTML lang attribute)
  • Safe in Node.js/SSR (gracefully skips DOM operations)
  • No errors or warnings in server-side rendering

Initial Setup

The HTML lang attribute is set automatically when the LangProvider mounts:

// This will set <html lang="sv"> when the component first renders
<LangProvider config={{ 
  defaultLanguage: 'sv',
  languages: ['sv', 'en'],
  translations: {...}
}}>
  <App />
</LangProvider>

TypeScript Support

Language Type

type AppLanguages = "en" | "sv"; // Your supported languages

Text Keys

type TextKeys = "welcome" | "portfolio"; // Your translation keys

Provider Types

<LangProvider<AppLanguages, TextKeys> config={config}>
    <TextProvider<AppLanguages, TextKeys> config={config}>

Best Practices

  1. Keep translations in separate JSON files
  2. Use TypeScript for type safety
  3. Use namespaced keys (e.g., common.welcome)
  4. Always provide fallback texts in default language
  5. Load translations asynchronously for large applications
  6. Use constant assertions for better type inference

API Reference

LangProvider Props

  • config: LanguageConfig object
  • children: React nodes

TextProvider Props

  • config: LanguageConfig object
  • children: React nodes

LanguageConfig

  • defaultLanguage: Default language code
  • languages: Array of supported language codes
  • translations: Translation key-value pairs

Hook Returns

useLang

  • language: Current language code
  • switchLanguage: (lang: L) => void
  • availableLanguages: L[]

useText

  • getText: (key: K) => string
  • setTexts: (texts: Record<L, Record<K, string>>) => void