Skip to Content

glost-translation

Translation plugin for GLOST documents.

Installation

pnpm add @glotblocks/glost-translation

Usage

createTranslationPlugin

Create a plugin that adds translations to words.

import { createTranslationPlugin } from '@glotblocks/glost-translation'; const translationPlugin = createTranslationPlugin({ targetLanguage: "th", nativeLanguage: "en", lookupTranslation: async (word, language) => { // Return translation for the word return "hello"; } });

Options

interface TranslationPluginOptions { targetLanguage: GlostLanguage; nativeLanguage?: GlostLanguage; lookupTranslation?: ( word: string, language: GlostLanguage ) => Promise<string | undefined>; }
  • targetLanguage - The language of the text being processed
  • nativeLanguage - The language to translate into (default: “en”)
  • lookupTranslation - Async function to fetch translations

Processing

Use with the v0.5+ processor API:

import { glost } from '@glotblocks/glost'; import { createTranslationPlugin } from '@glotblocks/glost-translation'; const translationPlugin = createTranslationPlugin({ targetLanguage: "th", nativeLanguage: "en", lookupTranslation: async (word, lang) => { const response = await fetch( `/api/dictionary/${lang}/${encodeURIComponent(word)}` ); if (!response.ok) return undefined; const data = await response.json(); return data.translation; } }); const result = await glost() .use(translationPlugin) .process(document);

What It Does

The plugin:

  1. Visits each word node in the document
  2. Calls the lookupTranslation function with the word text
  3. Adds the translation to word.extras.translations[nativeLanguage]

Result Structure

After processing, words will have translations in extras:

{ type: "WordNode", children: [{ type: "TextNode", value: "สวัสดี" }], extras: { translations: { en: "hello" } } }

Multiple Languages

You can add translations for multiple languages by using multiple plugins:

import { glost } from '@glotblocks/glost'; import { createTranslationPlugin } from '@glotblocks/glost-translation'; const toEnglish = createTranslationPlugin({ targetLanguage: "th", nativeLanguage: "en", lookupTranslation: lookupEnglish }); const toJapanese = createTranslationPlugin({ targetLanguage: "th", nativeLanguage: "ja", lookupTranslation: lookupJapanese }); const result = await glost() .use(toEnglish) .use(toJapanese) .process(document); // Result: word.extras.translations = { en: "hello", ja: "こんにちは" }

Combining with Other Extensions

import { processGLOSTWithExtensionsAsync } from '@glotblocks/glost-plugins'; import { createTranslationGeneratorExtension } from '@glotblocks/glost-translation'; import { createTranscriptionGeneratorExtension } from '@glotblocks/glost-transcription'; import { createFrequencyExtension } from '@glotblocks/glost-frequency'; // Create extensions const translation = createTranslationGeneratorExtension({ targetLanguage: 'th', nativeLanguage: 'en', provider: myTranslationProvider }); const transcription = createTranscriptionGeneratorExtension({ targetLanguage: 'th', provider: myTranscriptionProvider }); const [freqGen, freqEnh] = createFrequencyExtension({ targetLanguage: 'th', provider: myFrequencyProvider }); const result = await processGLOSTWithExtensionsAsync(document, [ translation, transcription, freqGen, freqEnh ]);
Last updated on