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

View File

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

View File

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

View File

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