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.
You can install the Localization Library via npm:
npm install @your-username/localization
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');
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'
});
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)
-
defaultLanguage
(optional): Default language code (default is'en'
)
-
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.
This project is licensed under the MIT License - see the LICENSE file for details.