| 11 | const languageNames = new Intl.DisplayNames(["en"], { type: "language" }); |
| 12 | |
| 13 | export async function getLanguage(text: string): Promise<string | null> { |
| 14 | // TODO: This tool isn't open source. We should use one that is open source |
| 15 | // According to SO, the best is FB's @smodin/fast-text-language-detection, but requires 128MB |
| 16 | // & python. Tried langdetect, cld, franc & languagedetect and all got low accuracy on |
| 17 | // simple tests made up on the spot. |
| 18 | const res = await fetch("https://ws.detectlanguage.com/0.2/detect", { |
| 19 | method: "POST", |
| 20 | headers: { |
| 21 | "Content-Type": "application/json", |
| 22 | Authorization: `Bearer ${process.env.NEXT_PUBLIC_DETECT_LANGUAGE_KEY}`, |
| 23 | }, |
| 24 | body: JSON.stringify({ |
| 25 | q: text, |
| 26 | }), |
| 27 | }); |
| 28 | if (!res.ok) { |
| 29 | console.error("Failed to detect language"); |
| 30 | return null; |
| 31 | } |
| 32 | const json: DetectLanguageResponse = await res.json(); |
| 33 | const bestGuess = json.data.detections[0]; |
| 34 | if (bestGuess.isReliable) { |
| 35 | console.log( |
| 36 | "Language from detectlanguage.com:" + JSON.stringify(json, undefined, 2), |
| 37 | ); |
| 38 | // This converts from "en" to "English" etc |
| 39 | return languageNames.of(bestGuess.language) ?? null; |
| 40 | } |
| 41 | return null; |
| 42 | } |