Files

116 lines
4.2 KiB
TypeScript

import { IpaDict } from '@ingglish/ipa';
export { LANGUAGES, Language } from '@ingglish/ipa';
import { OutputFormat, TranslatedToken } from '@ingglish/phonemes';
export { OutputFormat, TranslatedToken } from '@ingglish/phonemes';
/**
* Foreign dictionary loader and cache.
*
* Separated from index.ts to avoid circular dependencies with translate/forward.ts.
*/
type ForeignDictLoader = (lang: string) => Promise<IpaDict>;
interface TranslateOptions {
/** Output format. @default 'ingglish' */
format?: OutputFormat;
/** Language code (omit or 'en' for English). */
lang?: string;
}
/**
* Register a function that loads IPA dictionaries for foreign languages.
* Called once at application startup (e.g. in the website's entry point).
*
* @example
* ```typescript
* setForeignDictLoader(async (lang) => {
* const resp = await fetch(`/ipa-dicts/${lang}.json`);
* const entries = await resp.json();
* return { entries, lang };
* });
* ```
*/
declare function setForeignDictLoader(loader: ForeignDictLoader): void;
/**
* Forward translation: English -> Ingglish/IPA.
*
* Pipeline: English word → routing (dictionary, initialism, contraction,
* camelCase, fallback) → ARPAbet `string[]` (the IR) → `arpabetToFormat()` → output.
*
* Every code path converges on an ARPAbet phoneme array before converting to
* the requested output format. See `translateWordInternal()` for routing.
*/
/**
* Synchronous version of {@link translate}. Dictionary must already be loaded.
*
* For foreign languages, the dictionary must have been loaded by a prior
* `await translate(text, { lang })` or `await loadForeignDict(lang)` call.
*/
declare function translateSync(text: string, options?: TranslateOptions): string;
/**
* Like {@link translate}, but returns token-by-token mappings instead of a string.
* Each token includes the original text, translation, and whether it matched
* the dictionary. Dictionary must already be loaded.
*
* For foreign languages, the dictionary must have been loaded by a prior
* `await translate(text, { lang })` or `await loadForeignDict(lang)` call.
*/
declare function translateSyncWithMapping(text: string, options?: TranslateOptions): TranslatedToken[];
/**
* Reverse translation: Ingglish/IPA -> English.
*
* Uses ARPAbet matching to find English words that would produce
* the given spelling. Handles homophones by preferring more common
* words based on frequency data.
*/
/**
* Synchronous version of {@link reverseTranslate}. Dictionary must already be loaded.
*/
declare function reverseTranslateSync(text: string, options?: TranslateOptions): string;
/**
* Like {@link reverseTranslate}, but returns token-by-token mappings instead of a string.
* Each token includes the original text, translation, and whether it matched
* the dictionary. Dictionary must already be loaded.
*/
declare function reverseTranslateSyncWithMapping(text: string, options?: TranslateOptions): TranslatedToken[];
/**
* ingglish - Phonetic English Translation Library
*
* @example
* ```typescript
* import { translate } from 'ingglish';
*
* await translate('Hello, world!'); // 'Haloh, werld!'
* await translate('Bonjour', { lang: 'fr' }); // French → Ingglish
* ```
*/
/**
* Translates Ingglish/IPA text back to English.
* Auto-loads the dictionary on first call.
* For homophones, uses the most common word.
*
* @param text - Text in Ingglish or IPA format
* @param options - Translation options (format)
* @returns English text
*/
declare function reverseTranslate(text: string, options?: TranslateOptions): Promise<string>;
/**
* Translates text to the specified format.
* Auto-loads dictionaries on first call.
*
* For English (default): loads the CMU pronunciation dictionary.
* For foreign languages: loads the IPA dictionary via the registered loader.
*
* @param text - The text to translate
* @param options - Translation options (format, lang)
* @returns The translated text
*/
declare function translate(text: string, options?: TranslateOptions): Promise<string>;
export { type TranslateOptions, reverseTranslate, reverseTranslateSync, reverseTranslateSyncWithMapping, setForeignDictLoader, translate, translateSync, translateSyncWithMapping };