Lightweight JavaScript library for African language translations
Hello
Thanks
Goodbye
Welcome
Contact
Search
<script src="https://your-cdn.com/african-translate.min.js"></script>
npm install african-translate-js
Download african-translate.js and include it in your project
<!-- 1. Include the library -->
<script src="african-translate.js"></script>
<!-- 2. Add data-translate attributes to elements -->
<h1 data-translate="welcome">Welcome</h1>
<p data-translate="hello">Hello</p>
<button data-translate="login">Login</button>
<!-- 3. Initialize the translator -->
<script>
const translator = new AfricanTranslate({
language: 'sw', // Swahili
autoTranslate: true
});
</script>
<div id="lang-selector"></div>
<script>
const translator = new AfricanTranslate({
language: 'en',
autoTranslate: true,
persistLanguage: true
});
// Create language switcher
translator.createLanguageSwitcher('lang-selector', {
className: 'my-language-selector',
defaultLabel: 'Choose Language'
});
</script>
const translator = new AfricanTranslate({
language: 'sw',
customTranslations: {
sw: {
'my-custom-key': 'Tafsiri yangu maalum',
'app-name': 'Jina la Programu'
}
}
});
// Get translation programmatically
const greeting = translator.t('hello'); // Returns "Halo" in Swahili
// With variable replacement
translator.addLanguage('sw', {
'greeting': 'Habari {{name}}!'
});
const msg = translator.t('greeting', { name: 'John' }); // "Habari John!"
<input
type="text"
data-translate-placeholder="search"
placeholder="Search"
>
<img
src="logo.png"
data-translate-alt="logo"
alt="Logo"
>
const translator = new AfricanTranslate({
detectLanguage: true, // Auto-detect browser language
autoTranslate: true
});
const translator = new AfricanTranslate({
language: 'sw',
onLanguageChange: (lang) => {
console.log('Language changed to:', lang);
// Update UI, analytics, etc.
}
});
const translator = new AfricanTranslate({
language: 'sw', // Initial language (default: 'en')
autoTranslate: true, // Auto-translate on init (default: true)
detectLanguage: false, // Auto-detect browser language (default: false)
storageKey: 'app-lang', // LocalStorage key (default: 'african-translate-lang')
excludeClasses: ['notranslate'], // Classes to exclude (default: ['notranslate'])
customTranslations: {}, // Add your own translations
fallbackLanguage: 'en', // Fallback if translation missing
onLanguageChange: null, // Callback function
translateAttributes: ['placeholder', 'title', 'alt'], // Attributes to translate
persistLanguage: true // Save language preference (default: true)
});
translate(langCode) - Translate entire page to specified languaget(key, replacements) - Get translation for a specific keyaddLanguage(langCode, translations) - Add new language dynamicallygetAvailableLanguages() - Get array of available language codesgetCurrentLanguage() - Get current active languagecreateLanguageSwitcher(containerId, options) - Create UI language selectorreset() - Reset to original language (English)<nav>
<a data-translate="home">Home</a>
<a data-translate="products">Products</a>
<a data-translate="contact">Contact</a>
</nav>
<button data-translate="login">Login</button>
<input data-translate-placeholder="search" placeholder="Search">
<aside>
<h3 data-translate="settings">Settings</h3>
<button data-translate="save">Save</button>
<button data-translate="cancel">Cancel</button>
<button data-translate="delete">Delete</button>
</aside>
Want to add more African languages or improve translations? Contributions are welcome!