mirror of
https://gitlab.com/soapbox-pub/ditto.git
synced 2025-12-06 11:29:46 +00:00
Make @ditto/translators its own package
This commit is contained in:
parent
e163fcfa80
commit
990646da26
12 changed files with 50 additions and 43 deletions
|
|
@ -7,6 +7,7 @@
|
|||
"./packages/ditto",
|
||||
"./packages/lang",
|
||||
"./packages/metrics",
|
||||
"./packages/translators",
|
||||
"./packages/uploaders"
|
||||
],
|
||||
"tasks": {
|
||||
|
|
|
|||
|
|
@ -1,6 +1,7 @@
|
|||
import { confMw } from '@ditto/api/middleware';
|
||||
import { type DittoConf } from '@ditto/conf';
|
||||
import { DittoTables } from '@ditto/db';
|
||||
import { type DittoTranslator } from '@ditto/translators';
|
||||
import { type Context, Env as HonoEnv, Handler, Hono, Input as HonoInput, MiddlewareHandler } from '@hono/hono';
|
||||
import { every } from '@hono/hono/combine';
|
||||
import { cors } from '@hono/hono/cors';
|
||||
|
|
@ -134,7 +135,6 @@ import { metricsController } from '@/controllers/metrics.ts';
|
|||
import { manifestController } from '@/controllers/manifest.ts';
|
||||
import { nodeInfoController, nodeInfoSchemaController } from '@/controllers/well-known/nodeinfo.ts';
|
||||
import { nostrController } from '@/controllers/well-known/nostr.ts';
|
||||
import { DittoTranslator } from '@/interfaces/DittoTranslator.ts';
|
||||
import { auth98Middleware, requireProof, requireRole } from '@/middleware/auth98Middleware.ts';
|
||||
import { cacheControlMiddleware } from '@/middleware/cacheControlMiddleware.ts';
|
||||
import { cspMiddleware } from '@/middleware/cspMiddleware.ts';
|
||||
|
|
|
|||
|
|
@ -1,8 +1,7 @@
|
|||
import { DeepLTranslator, LibreTranslateTranslator } from '@ditto/translators';
|
||||
import { safeFetch } from '@soapbox/safe-fetch';
|
||||
|
||||
import { AppMiddleware } from '@/app.ts';
|
||||
import { DeepLTranslator } from '@/translators/DeepLTranslator.ts';
|
||||
import { LibreTranslateTranslator } from '@/translators/LibreTranslateTranslator.ts';
|
||||
|
||||
/** Set the translator used for translating posts. */
|
||||
export const translatorMiddleware: AppMiddleware = async (c, next) => {
|
||||
|
|
|
|||
|
|
@ -1,6 +1,4 @@
|
|||
import { DittoDB } from '@ditto/db';
|
||||
import ISO6391, { LanguageCode } from 'iso-639-1';
|
||||
import lande from 'lande';
|
||||
import { NostrEvent } from '@nostrify/nostrify';
|
||||
import { finalizeEvent, generateSecretKey } from 'nostr-tools';
|
||||
|
||||
|
|
@ -68,15 +66,3 @@ export async function createTestDB(opts?: { pure?: boolean }) {
|
|||
export function sleep(ms: number): Promise<void> {
|
||||
return new Promise((resolve) => setTimeout(resolve, ms));
|
||||
}
|
||||
|
||||
export function getLanguage(text: string): LanguageCode | undefined {
|
||||
const [topResult] = lande(text);
|
||||
if (topResult) {
|
||||
const [iso6393] = topResult;
|
||||
const locale = new Intl.Locale(iso6393);
|
||||
if (ISO6391.validate(locale.language)) {
|
||||
return locale.language;
|
||||
}
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,14 +1,14 @@
|
|||
import { DittoConf } from '@ditto/conf';
|
||||
import { detectLanguage } from '@ditto/lang';
|
||||
import { assert, assertEquals } from '@std/assert';
|
||||
|
||||
import { Conf } from '@/config.ts';
|
||||
import { DeepLTranslator } from '@/translators/DeepLTranslator.ts';
|
||||
import { getLanguage } from '@/test.ts';
|
||||
import { DeepLTranslator } from './DeepLTranslator.ts';
|
||||
|
||||
const {
|
||||
deeplBaseUrl: baseUrl,
|
||||
deeplApiKey: apiKey,
|
||||
translationProvider,
|
||||
} = Conf;
|
||||
} = new DittoConf(Deno.env);
|
||||
|
||||
const deepl = 'deepl';
|
||||
|
||||
|
|
@ -28,9 +28,9 @@ Deno.test('DeepL translation with source language omitted', {
|
|||
);
|
||||
|
||||
assertEquals(data.source_lang, 'pt');
|
||||
assertEquals(getLanguage(data.results[0]), 'en');
|
||||
assertEquals(getLanguage(data.results[1]), 'en');
|
||||
assertEquals(getLanguage(data.results[2]), 'en');
|
||||
assertEquals(detectLanguage(data.results[0], 0), 'en');
|
||||
assertEquals(detectLanguage(data.results[1], 0), 'en');
|
||||
assertEquals(detectLanguage(data.results[2], 0), 'en');
|
||||
});
|
||||
|
||||
Deno.test('DeepL translation with source language set', {
|
||||
|
|
@ -49,9 +49,9 @@ Deno.test('DeepL translation with source language set', {
|
|||
);
|
||||
|
||||
assertEquals(data.source_lang, 'pt');
|
||||
assertEquals(getLanguage(data.results[0]), 'en');
|
||||
assertEquals(getLanguage(data.results[1]), 'en');
|
||||
assertEquals(getLanguage(data.results[2]), 'en');
|
||||
assertEquals(detectLanguage(data.results[0], 0), 'en');
|
||||
assertEquals(detectLanguage(data.results[1], 0), 'en');
|
||||
assertEquals(detectLanguage(data.results[2], 0), 'en');
|
||||
});
|
||||
|
||||
Deno.test("DeepL translation doesn't alter Nostr URIs", {
|
||||
|
|
@ -1,8 +1,9 @@
|
|||
import { LanguageCode } from 'iso-639-1';
|
||||
import { z } from 'zod';
|
||||
|
||||
import { DittoTranslator } from '@/interfaces/DittoTranslator.ts';
|
||||
import { languageSchema } from '@/schema.ts';
|
||||
import { languageSchema } from './schema.ts';
|
||||
|
||||
import type { LanguageCode } from 'iso-639-1';
|
||||
import type { DittoTranslator } from './DittoTranslator.ts';
|
||||
|
||||
interface DeepLTranslatorOpts {
|
||||
/** DeepL base URL to use. Default: 'https://api.deepl.com' */
|
||||
|
|
@ -1,14 +1,14 @@
|
|||
import { DittoConf } from '@ditto/conf';
|
||||
import { detectLanguage } from '@ditto/lang';
|
||||
import { assertEquals } from '@std/assert';
|
||||
|
||||
import { Conf } from '@/config.ts';
|
||||
import { LibreTranslateTranslator } from '@/translators/LibreTranslateTranslator.ts';
|
||||
import { getLanguage } from '@/test.ts';
|
||||
import { LibreTranslateTranslator } from './LibreTranslateTranslator.ts';
|
||||
|
||||
const {
|
||||
libretranslateBaseUrl: baseUrl,
|
||||
libretranslateApiKey: apiKey,
|
||||
translationProvider,
|
||||
} = Conf;
|
||||
} = new DittoConf(Deno.env);
|
||||
|
||||
const libretranslate = 'libretranslate';
|
||||
|
||||
|
|
@ -28,9 +28,9 @@ Deno.test('LibreTranslate translation with source language omitted', {
|
|||
);
|
||||
|
||||
assertEquals(data.source_lang, 'pt');
|
||||
assertEquals(getLanguage(data.results[0]), 'ca');
|
||||
assertEquals(getLanguage(data.results[1]), 'ca');
|
||||
assertEquals(getLanguage(data.results[2]), 'ca');
|
||||
assertEquals(detectLanguage(data.results[0], 0), 'ca');
|
||||
assertEquals(detectLanguage(data.results[1], 0), 'ca');
|
||||
assertEquals(detectLanguage(data.results[2], 0), 'ca');
|
||||
});
|
||||
|
||||
Deno.test('LibreTranslate translation with source language set', {
|
||||
|
|
@ -49,7 +49,7 @@ Deno.test('LibreTranslate translation with source language set', {
|
|||
);
|
||||
|
||||
assertEquals(data.source_lang, 'pt');
|
||||
assertEquals(getLanguage(data.results[0]), 'ca');
|
||||
assertEquals(getLanguage(data.results[1]), 'ca');
|
||||
assertEquals(getLanguage(data.results[2]), 'ca');
|
||||
assertEquals(detectLanguage(data.results[0], 0), 'ca');
|
||||
assertEquals(detectLanguage(data.results[1], 0), 'ca');
|
||||
assertEquals(detectLanguage(data.results[2], 0), 'ca');
|
||||
});
|
||||
|
|
@ -1,8 +1,9 @@
|
|||
import { LanguageCode } from 'iso-639-1';
|
||||
import { z } from 'zod';
|
||||
|
||||
import { DittoTranslator } from '@/interfaces/DittoTranslator.ts';
|
||||
import { languageSchema } from '@/schema.ts';
|
||||
import { languageSchema } from './schema.ts';
|
||||
|
||||
import type { LanguageCode } from 'iso-639-1';
|
||||
import type { DittoTranslator } from './DittoTranslator.ts';
|
||||
|
||||
interface LibreTranslateTranslatorOpts {
|
||||
/** Libretranslate endpoint to use. Default: 'https://libretranslate.com' */
|
||||
7
packages/translators/deno.json
Normal file
7
packages/translators/deno.json
Normal file
|
|
@ -0,0 +1,7 @@
|
|||
{
|
||||
"name": "@ditto/translators",
|
||||
"version": "1.1.0",
|
||||
"exports": {
|
||||
".": "./mod.ts"
|
||||
}
|
||||
}
|
||||
4
packages/translators/mod.ts
Normal file
4
packages/translators/mod.ts
Normal file
|
|
@ -0,0 +1,4 @@
|
|||
export { DeepLTranslator } from './DeepLTranslator.ts';
|
||||
export { LibreTranslateTranslator } from './LibreTranslateTranslator.ts';
|
||||
|
||||
export type { DittoTranslator } from './DittoTranslator.ts';
|
||||
8
packages/translators/schema.ts
Normal file
8
packages/translators/schema.ts
Normal file
|
|
@ -0,0 +1,8 @@
|
|||
import ISO6391 from 'iso-639-1';
|
||||
import z from 'zod';
|
||||
|
||||
/** Value is a ISO-639-1 language code. */
|
||||
export const languageSchema = z.string().refine(
|
||||
(val) => ISO6391.validate(val),
|
||||
{ message: 'Not a valid language in ISO-639-1 format' },
|
||||
);
|
||||
Loading…
Add table
Reference in a new issue