JavaScript SDK
Add real-time AI-powered translations to your JavaScript applications with our easy-to-use SDK.
Perfect For
- Chat Applications: Translate messages instantly
- Email Clients: Localize incoming emails on-the-fly
- Comment Systems: Make user content accessible in any language
- Social Media Tools: Create multilingual experiences for global audiences
Installation
bash
npm install lingo.dev
Quick Setup
javascript
import { LingoDotDevEngine } from "lingo.dev/sdk";
const lingoDotDev = new LingoDotDevEngine({
apiKey: "your-api-key-here",
});
Translation Methods
1. Translate Simple Text
javascript
const localizedText = await lingoDotDev.localizeText("Hello, world!", {
sourceLocale: "en",
targetLocale: "es",
});
// Result: "¡Hola Mundo!"
2. Translate to Multiple Languages at Once
javascript
const localizedTexts = await lingoDotDev.batchLocalizeText("Hello, world!", {
sourceLocale: "en",
targetLocales: ["es", "fr", "de"],
});
// Result: ['¡Hola Mundo!', 'Bonjour le monde!', 'Hallo Welt!']
3. Translate Objects
javascript
const localizedContent = await lingoDotDev.localizeObject(
{ greeting: "Hello", farewell: "Goodbye" },
{ sourceLocale: "en", targetLocale: "es" },
);
// Result: { greeting: "Hola", farewell: "Adiós" }
4. Translate Chat Conversations
javascript
const localizedChat = await lingoDotDev.localizeChat(
[
{ name: "Alice", text: "Hello!" },
{ name: "Bob", text: "Hi there!" },
],
{ sourceLocale: "en", targetLocale: "es" },
);
// Result: [{ name: "Alice", text: "¡Hola!" }, { name: "Bob", text: "¡Hola!" }]
5. Translate HTML Content
javascript
const localizedHtml = await lingoDotDev.localizeHtml(
"<div>Hello <strong>world</strong></div>",
{ sourceLocale: "en", targetLocale: "es" },
);
// Result: "<div>Hola <strong>mundo</strong></div>"
6. Detect Language
javascript
const locale = await lingoDotDev.recognizeLocale("Bonjour le monde");
// Result: 'fr'
Configuration Options
Parameter | Description | Default | Max |
---|---|---|---|
apiKey | Your Lingo.dev API key | Required | - |
batchSize | Max items per API request | 50 | 250 |
idealBatchItemSize | Target word count per batch | 500 | 2500 |
javascript
const lingoDotDev = new LingoDotDevEngine({
apiKey: "your-api-key-here",
batchSize: 100,
idealBatchItemSize: 1000,
});
Translation Parameters
Parameter | Description | Required |
---|---|---|
sourceLocale | Original language code (e.g., 'en') | Yes* |
targetLocale | Desired language code (e.g., 'es') | Yes |
fast | Enable faster translations (may reduce quality) | No |
*Can be set to null
for automatic language detection
Speed vs. Quality Option
javascript
const quickTranslation = await lingoDotDev.localizeText("Hello world", {
sourceLocale: "en",
targetLocale: "es",
fast: true, // Prioritize speed over quality
});
Automatic Language Detection
javascript
const localizedText = await lingoDotDev.localizeText("Hello, world!", {
sourceLocale: null, // Auto-detect language
targetLocale: "es",
});
Note: Auto-detection increases processing time. Use only when the source language is unknown (e.g., user-generated content).
Progress Tracking for Large Jobs
javascript
await lingoDotDev.localizeObject(
largeObject,
{ sourceLocale: "en", targetLocale: "es" },
(progress) => console.log(`Translation progress: ${progress}%`),
);
How It Works
The SDK optimizes translation performance through:
- Smart Chunking: Breaks content into optimal sizes for processing
- Progress Monitoring: Tracks completion percentage for user feedback
- Error Management: Handles API issues with automatic retries
- Response Assembly: Seamlessly recombines translated chunks
Best Practices
- Reuse Engine Instances: Create one instance and reuse it throughout your app
- Implement Error Handling: Add try/catch blocks around SDK calls
- Show Progress: Use callbacks for user feedback during large translations
- Optimize Content Structure: Flat objects perform better than deeply nested ones
With the Lingo.dev SDK, you can easily add powerful AI-driven translations to any JavaScript application, creating seamless multilingual experiences with minimal development effort.