1.0.1 • Published 1 year ago
mongo-translation-manager v1.0.1
mongo-translation-manager
mongo-translation-manager
is a Node.js package for managing translations with MongoDB and caching.
Features
- MongoDB Connection: Connects to MongoDB to store and retrieve translations.
- Translation Retrieval: Retrieves translations for keywords in selected and backup languages.
- Caching: Utilizes
localStorage
for caching translations to improve performance.
Installation
To install mongo-translation-manager
, use npm or yarn:
npm install mongo-translation-manager
# or
yarn add mongo-translation-manager
Usage
Connecting to MongoDB
Before using mongo-translation-manager
, you need to connect to your MongoDB database:
const { connectMongoDB } = require('mongo-translation-manager');
(async () => {
const mongoUri = 'mongodb://localhost:27017/mydb';
await connectMongoDB(mongoUri);
})();
Retrieving Translations
You can retrieve translations for a specific keyword in your desired language:
const { getTranslationsForKeyword } = require('mongo-translation-manager');
(async () => {
try {
const keyword = 'Helloword';
const translations = await getTranslationsForKeyword(keyword, 'sv');
console.log('Translations:', translations);
} catch (error) {
console.error('Error:', error.message);
}
})();
Example Output
For the keyword 'Helloword' and language code 'sv' (Swedish), the output might look like:
{
"sv": {
"Helloword": "Hello world"
},
"en": {
"Helloword": "Hello world"
}
}
API
connectMongoDB(uri)
- Description: Connects to a MongoDB database using the provided URI.
- Parameters:
uri
(string): MongoDB connection URI.
getTranslationsForKeyword(keyword, selectedLanguageCode)
- Description: Retrieves translations for a keyword in the specified language and fallback to English ('en') if necessary.
- Parameters:
keyword
(string): Keyword to retrieve translations for.selectedLanguageCode
(string): Language code (e.g., 'sv' for Swedish).
License
This package is licensed under the MIT License. See the LICENSE file for details.
Author
- Author: Hamza Zaheer