Node.js
Instructions for adding the Azbox Node.js package to your application using the azbox-node package.
Requirements
- Node.js 14+ installed on your system
- npm or yarn as package manager
- Azbox plan (Starter or higher)
- Configured Azbox Project
- API Key and Project ID from your Azbox project
Installation
Install the azbox-node package from npm:
npm install azbox-node
Or if you prefer using yarn:
yarn add azbox-node
Configuration
First, you need to initialize Azbox in your application. Create a configuration file or initialize directly in your code:
const Azbox = require('azbox-node');
// Initialize Azbox with your credentials
const azbox = new Azbox({
apiKey: 'Your API Key',
projectId: 'Your Project ID'
});
If you're using ES6 modules:
import Azbox from 'azbox-node';
const azbox = new Azbox({
apiKey: 'Your API Key',
projectId: 'Your Project ID'
});
Basic Usage
Once configured, you can start using the translation functions:
Get translations
// Get a simple translation
const translation = await azbox.translate('keyword', 'es');
console.log(translation); // Shows the translation in Spanish
// Get translation with default language
const title = await azbox.translate('title');
console.log(title);
Get multiple translations
// Get all translations for a language
const translations = await azbox.getTranslations('es');
console.log(translations);
// Get translations for multiple keywords
const keywords = ['title', 'subtitle', 'description'];
const results = await azbox.getMultipleTranslations(keywords, 'es');
console.log(results);
Translations with parameters
// Translation with positional arguments
const message = await azbox.translate('welcome_message', 'es', {
args: ['User', 'Azbox']
});
console.log(message); // "Welcome User to Azbox"
// Translation with named arguments
const greeting = await azbox.translate('greeting', 'es', {
namedArgs: {
name: 'John',
time: 'morning'
}
});
console.log(greeting);
Complete Example
Here's a complete example of how to use the package in a Node.js application:
const Azbox = require('azbox-node');
const express = require('express');
const app = express();
const azbox = new Azbox({
apiKey: process.env.AZBOX_API_KEY,
projectId: process.env.AZBOX_PROJECT_ID
});
// Middleware to add translations to context
app.use(async (req, res, next) => {
const lang = req.headers['accept-language'] || 'es';
req.translations = await azbox.getTranslations(lang);
next();
});
// Example route
app.get('/api/translate/:keyword', async (req, res) => {
try {
const { keyword } = req.params;
const lang = req.query.lang || 'es';
const translation = await azbox.translate(keyword, lang);
res.json({ keyword, lang, translation });
} catch (error) {
res.status(500).json({ error: error.message });
}
});
app.listen(3000, () => {
console.log('Server running on port 3000');
});
Error Handling
It's important to handle errors properly:
try {
const translation = await azbox.translate('keyword', 'es');
console.log(translation);
} catch (error) {
if (error.code === 'KEYWORD_NOT_FOUND') {
console.error('The keyword does not exist in the project');
} else if (error.code === 'LANGUAGE_NOT_FOUND') {
console.error('The requested language is not available');
} else {
console.error('Error getting translation:', error.message);
}
}
Caching and Optimization
To improve performance, you can implement a caching system:
const NodeCache = require('node-cache');
const cache = new NodeCache({ stdTTL: 3600 }); // Cache for 1 hour
async function getCachedTranslation(keyword, lang) {
const cacheKey = `${keyword}_${lang}`;
let translation = cache.get(cacheKey);
if (!translation) {
translation = await azbox.translate(keyword, lang);
cache.set(cacheKey, translation);
}
return translation;
}
Environment Variables
To keep your credentials secure, use environment variables:
# .env
AZBOX_API_KEY=your_api_key_here
AZBOX_PROJECT_ID=your_project_id_here
require('dotenv').config();
const azbox = new Azbox({
apiKey: process.env.AZBOX_API_KEY,
projectId: process.env.AZBOX_PROJECT_ID
});
Next Steps
- Check the complete package documentation on npm
- Explore all available functions in the package
- Implement caching to improve your application's performance
- Consider using TypeScript for better typing and autocomplete