nigeria-translator
Version:
A lightweight and developer-friendly Node.js + TypeScript package for translating between English and major Nigerian languages — Igbo, Yoruba, and Hausa. Provides simple bidirectional translation functions (English ↔ Igbo, Yoruba, Hausa) powered by the Bi
48 lines (36 loc) • 1.14 kB
text/typescript
import { translate } from "bing-translate-api";
interface BingResponse {
translation: string;
sourceLanguage?: string;
targetLanguage?: string;
}
export async function translateText(
text: string,
from: string,
to: string
): Promise<string> {
try {
const result = (await translate(text, from, to)) as BingResponse;
if (!result || !result.translation) {
throw new Error("Invalid response from translation API");
}
return result.translation;
} catch (error) {
console.error("Translation error:", error);
throw error;
}
}
// English → Nigerian languages
export const englishToIgbo = (text: string) =>
translateText(text, "en", "ig");
export const englishToYoruba = (text: string) =>
translateText(text, "en", "yo");
export const englishToHausa = (text: string) =>
translateText(text, "en", "ha");
// Nigerian languages → English
export const igboToEnglish = (text: string) =>
translateText(text, "ig", "en");
export const yorubaToEnglish = (text: string) =>
translateText(text, "yo", "en");
export const hausaToEnglish = (text: string) =>
translateText(text, "ha", "en");