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
Maintainers
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
- 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;