40 lines
1.2 KiB
TypeScript
40 lines
1.2 KiB
TypeScript
import { Translator } from "../i18n/api";
|
|
|
|
export type Speaker = {
|
|
id: "host" | "guest",
|
|
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,
|
|
public s1 : Speaker,
|
|
public 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`);
|
|
const otherSpeaker = this[i].speaker.id === this.s1.id ? this.s2 : this.s1
|
|
console.log(`Translating sentence to %s: %s`, otherSpeaker.language, this[i].text)
|
|
await this[i].translate(this.translator, otherSpeaker.language);
|
|
}
|
|
|
|
public async translateLast() {
|
|
return await this.translateMessage(this.length-1);
|
|
}
|
|
} |