node-language-translate
Version:
🚀 Next-gen translation engine for Node.js | Free unlimited language conversion | AI-powered context-aware translations | Developer-friendly API | Zero dependencies | Built for modern cloud-native applications | Compatible with Express, NestJS & serverles
24 lines (20 loc) • 749 B
text/typescript
import { MyMemoryProvider } from "./providers/my-memory";
import { TranslationProvider, TranslationOptions } from "./types";
class Translator {
private provider: TranslationProvider;
constructor(provider: TranslationProvider = new MyMemoryProvider()) {
this.provider = provider;
}
async translate(text: string, from: string, to: string): Promise<string> {
try {
const options: TranslationOptions = { from, to };
return await this.provider.translate(text, options);
} catch (error) {
if (error instanceof Error) {
throw new Error(`Translation failed: ${error.message}`);
}
throw new Error("Translation failed: Unknown error");
}
}
}
export = new Translator();