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 🙏

© 2024 – Pkg Stats / Ryan Hefner

language-switcher2

v1.0.12

Published

A lightweight, reusable React package for adding multilingual support to your applications. This package uses `react-i18next` for translation management and provides a context provider for easy state management and language switching.

Downloads

631

Readme

Language Switcher Package

A lightweight, reusable React package for adding multilingual support to your applications. This package uses react-i18next for translation management and provides a context provider for easy state management and language switching.

Features

  • Dynamic Language Switching: Effortlessly switch between multiple languages.
  • Custom Language Files: Support for loading language JSON files specific to different apps.
  • Reusable Provider: Easily integrate the language switching provider across your apps.
  • Fallback Language Support: Automatically falls back to a default language if no translation is available.
  • Scalable Design: Compatible with any React application structure.
  • Minimal Configuration: Easy setup with minimal code.

Installation

To install the package from npm, run the following command:

npm install language-switcher2

If you're using yarn:

yarn add language-switcher2

Getting Started

  1. Wrap Your Application with the LanguageProvider To enable the language switcher functionality, wrap your entire application in the LanguageProvider component. This allows the app to be aware of the current language and switch between languages dynamically.

Example usage in index.js or App.js:

import React from 'react';
import ReactDOM from 'react-dom';
import { LanguageProvider } from '@yourusername/language-switcher';

const App = () => {
  return (
    <LanguageProvider
      defaultLanguage="en"
      supportedLanguages={{
        en: "English",
        ml: "Malayalam",
        ar: "Arabic",
      }}
      resources={{
        en: require('./locales/en.json'),
        ml: require('./locales/ml.json'),
        ar: require('./locales/ar.json'),
      }}
    >
      <YourApplication />
    </LanguageProvider>
  );
};
ReactDOM.render(<App />, document.getElementById('root'));

2. Using the useLanguage Hook

You can use the useLanguage hook inside your components to access the current language and switch between languages.

Example usage in a language switcher component:

import React from 'react';
import { useLanguage } from '@yourusername/language-switcher';

const LanguageSwitcher = () => {
  const { language, switchLanguage } = useLanguage();

  return (
    <div>
      <p>Current Language: {language}</p>
      <button onClick={() => switchLanguage('en')}>English</button>
      <button onClick={() => switchLanguage('ml')}>Malayalam</button>
      <button onClick={() => switchLanguage('ar')}>Arabic</button>
    </div>
  );
};

export default LanguageSwitcher;

3. Translating Content Using useTranslation Hook

Use the useTranslation hook from react-i18next to access translations and display them in your components.

Example:

import React from 'react';
import { useTranslation } from 'react-i18next';

const Home = () => {
  const { t } = useTranslation();

  return (
    <div>
      <h1>{t('WELCOME')}</h1>
      <button>{t('SUBMIT')}</button>
    </div>
  );
};
export default Home;

Language Files Structure

Your project should include separate JSON files for each language. These files should be placed in a locales/ folder, or you can place them wherever suits your project structure.

Example Folder Structure:

src/
├── locales/
│   ├── en.json
│   ├── ml.json
│   ├── ar.json
├── App.js
├── index.js

Example en.json:

{
  "WELCOME": "Welcome",
  "SUBMIT": "Submit",
  "LOGIN_TEXT": "Login",
  "USERNAME": "Username",
  "PASSWORD": "Password"
}

Example ar.json:

{
  "WELCOME": "أهلا بك",
  "SUBMIT": "إرسال",
  "LOGIN_TEXT": "تسجيل الدخول",
  "USERNAME": "اسم المستخدم",
  "PASSWORD": "كلمة المرور"
}

Example Usage Here's a simple example of how you might use the package in your app:

import React from 'react';
import { useLanguage } from '@yourusername/language-switcher';
import { useTranslation } from 'react-i18next';

const HomePage = () => {
  const { t } = useTranslation();
  const { switchLanguage } = useLanguage();

  return (
    <div>
      <h1>{t('WELCOME')}</h1>
      <button onClick={() => switchLanguage('ml')}>{t('SUBMIT')}</button>
      <button onClick={() => switchLanguage('en')}>English</button>
      <button onClick={() => switchLanguage('ar')}>Arabic</button>
    </div>
  );
};

export default HomePage;