translation-terrace/app/lib/conversation.ts

38 lines
1001 B
TypeScript

import { Translator } from "../i18n/api";
export type Speaker = {
id: string,
language : string,
}
export class Message {
public translation? : string;
constructor (public text : string, public speaker : Speaker) {}
public async translate(translator : Translator, language? : string) {
this.translation = await translator.translate(this.text, language);
}
}
export class Conversation extends Array<Message> {
constructor (
private translator : Translator,
s1 : Speaker,
s2 : Speaker,
) {
super();
}
public addMessage(speaker : Speaker, text : string) {
this.push(new Message(text, speaker));
}
public async translateMessage(i : number) {
if (!this[i]) throw new Error(`${i} is not a valid message number`);
await this[i].translate(this.translator, this[i].speaker.language);
}
public async translateLast() {
return await this.translateMessage(this.length-1);
}
}