bb-deepl-translator
v1.2.0
Published
Reusable DeepL Translator package for translating text and deeply nested JSON
Maintainers
Readme
bb-deepl-translator
Reusable DeepL Translator package for translating plain text and deeply nested JSON using the DeepL API and XLIFF.
Overview
This project provides a simple interface to:
- Translate plain string text.
- Translate nested JSON objects.
- Check your DeepL API usage.
It leverages the deepl-node package for translation and xliff utilities for converting translations between JSON and XLIFF formats.
Prerequisites
- Node.js version 12 or higher.
- A valid DeepL API key.
Usage
Using as a Cloned Repository
- Clone the repository:
git clone https://github.com/raviraj-bugge-bbi/bb-translator.git - Navigate to the project directory:
cd bb-translator - Install the dependencies:
npm install
import Translator from './src/index.js';
// Initialize with your DeepL API key
const translator = new Translator('YOUR_DEEPL_API_KEY');
// Translate Plain Text
const translatedText = await translator.translatePlainText('Hello World!', 'en', 'de');
console.log('Translated Text:', translatedText);
// Translate JSON
const jsonData = {
user: {
profile: {
name: "Alice",
bio: "Software Engineer"
}
}
};
const translatedJson = await translator.translateJson(jsonData, 'en', 'de');
console.log('Translated JSON:', JSON.stringify(translatedJson, null, 2));
// Check API Usage
const usage = await translator.getUsage();
console.log('Usage:', usage);Using as an Installed npm Package
- Install the package:
npm install bb-deepl-translator - Import and use in your project:
import Translator from 'bb-deepl-translator'; // Initialize with your DeepL API key const translator = new Translator('YOUR_DEEPL_API_KEY'); // Usage similar to the cloned repository example above.
Available Methods
translatePlainText(text, sourceLang, targetLang)
Translates a plain string from the source to the target language.
// Example:
const translated = await translator.translatePlainText('Hello', 'en', 'de');
console.log(translated);translateJson(jsonData, sourceLang, targetLang)
Translates a deeply nested JSON object.
// Example:
const jsonData = { greeting: "Hello", farewell: "Goodbye" };
const translatedJson = await translator.translateJson(jsonData, 'en', 'de');
console.log(translatedJson);getUsage()
Retrieves the current DeepL API usage statistics.
// Example:
const usage = await translator.getUsage();
console.log(usage);