Language switch implemented

This commit is contained in:
David
2021-03-10 21:31:50 +01:00
parent 8f33cb92f2
commit 27ffaa425c
5 changed files with 89 additions and 25 deletions

View File

@@ -1,7 +1,5 @@
import cheerio from "cheerio";
import { mappings } from "./languages.json";
const isKeyOf = <T extends object>(obj: T) => (key: keyof any): key is keyof T => key in obj;
import { replaceBoth } from "./language";
export async function googleScrape(
source: string,
@@ -11,10 +9,8 @@ export async function googleScrape(
translation?: string,
error?: number
}> {
const newTarget = isKeyOf(mappings.target)(target)
? mappings.target[target]
: target;
const res = await fetch(`https://translate.google.com/m?sl=${source}&tl=${newTarget}&q=${encodeURI(query)}`);
const parsed = replaceBoth("mapping", { source, target });
const res = await fetch(`https://translate.google.com/m?sl=${parsed.source}&tl=${parsed.target}&q=${encodeURI(query)}`);
if (!res.ok)
return {