language improvements

This commit is contained in:
artie 2025-02-12 00:13:36 +01:00
parent 1d628e5722
commit 6d10dc2a00
4 changed files with 20 additions and 24 deletions

View File

@ -11,12 +11,11 @@ import {
getTargetLanguages,
isSourceLanguageSupported,
isTargetLanguageSupported,
languageCodeToName,
translate,
} from "../../utils/deepl";
import { abort } from "../../utils/error";
import type { OCRResult } from "../../types/ocr";
import { capitalize } from "../../utils/functions";
import { capitalize, languageCodeToName } from "../../utils/functions";
export async function translateAutocompleteImpl(
interaction: AutocompleteInteraction
@ -55,8 +54,8 @@ export async function translateImpl(
target,
});
const displaySource = await languageCodeToName(detectedSourceLang);
const displayTarget = await languageCodeToName(target);
const displaySource = languageCodeToName(detectedSourceLang);
const displayTarget = languageCodeToName(target);
if (translatedText.length > 4096) {
return {

View File

@ -12,6 +12,7 @@ import sharp from "sharp";
import {
capitalize,
getImageFromAttachmentOrString,
languageCodeToName,
run,
} from "../../utils/functions";
import { lensOcr } from "../../utils/lens";
@ -23,16 +24,7 @@ export function buildOcrPayload(
model: OCRResult["model"],
imageUrl?: string
): InteractionEditReplyOptions {
const languageName = run(() => {
try {
return (
new Intl.DisplayNames(["en"], { type: "language" }).of(language) ??
"Unknown"
);
} catch {
return "Unknown";
}
});
const languageName = languageCodeToName(language) ?? "Unknown";
if (text.length > 4096) {
return {

View File

@ -36,22 +36,18 @@ export async function getLanguages() {
return (await getSourceLanguages()).concat(await getTargetLanguages());
}
export async function languageCodeToName(code: string) {
return (await getLanguages()).find((l) => l.code === code)?.name;
}
export async function isSourceLanguageSupported(code: string) {
const sourceLanguages = await getSourceLanguages();
return (
(await getSourceLanguages()).find(
(l) => l.code.toLowerCase() === code.toLowerCase()
) !== undefined
sourceLanguages.find((l) => l.code.toLowerCase() === code.toLowerCase()) !==
undefined
);
}
export async function isTargetLanguageSupported(code: string) {
const targetLanguages = await getTargetLanguages();
return (
(await getTargetLanguages()).find(
(l) => l.code.toLowerCase() === code.toLowerCase()
) !== undefined
targetLanguages.find((l) => l.code.toLowerCase() === code.toLowerCase()) !==
undefined
);
}

View File

@ -7,6 +7,7 @@ import { abort } from "./error";
export const nanoid = customAlphabet("1234567890abcdef");
export const shell = execa({ reject: false });
const languageNames = new Intl.DisplayNames(["en"], { type: "language" });
export function noop() {}
@ -96,3 +97,11 @@ export function getImageFromAttachmentOrString(
abort("You must provide an image or an image URL!");
}
}
export function languageCodeToName(code: string) {
try {
return languageNames.of(code);
} catch {
return undefined;
}
}