stolenn1
v1.0.0
Published
Build a library for handling localization in JavaScript applications, offering support for multiple languages, dynamic language switching, and integration with popular frameworks like React, Angular, and Vue.js.
Downloads
2
Readme
Localization Library
Overview
The Localization Library is a JavaScript package that provides support for managing translations in multiple languages. It allows you to easily add translations for different languages and retrieve translated strings based on keys.
Installation
You can install the Localization Library via npm:
npm install @your-username/localization
Usage
Initialization
To use the library, you first need to initialize an instance of the Localization
class:
const Localization = require('@your-username/localization');
// Initialize with default language
const localization = new Localization('en');
Adding Translations
You can add translations for different languages using the addTranslations
method:
localization.addTranslations('en', {
'greeting': 'Hello',
'farewell': 'Goodbye'
});
localization.addTranslations('fr', {
'greeting': 'Bonjour',
'farewell': 'Au revoir'
});
Translating Strings
Once translations are added, you can use the translate
method to retrieve translated strings:
console.log(localization.translate('greeting')); // Output: Hello
console.log(localization.translate('farewell', 'fr')); // Output: Au revoir
console.log(localization.translate('missing_key')); // Output: missing_key (warning message logged)
API Reference
Localization(defaultLanguage)
defaultLanguage
(optional): Default language code (default is'en'
)
Methods
addTranslations(language, translations)
: Add translations for a specific language.translate(key, language)
: Retrieve the translated string for a given key and language. If language is not provided, the default language is used.
License
This project is licensed under the MIT License - see the LICENSE file for details.