Skip to content

Programmatic API

All public types and classes are exported from the package root. Example: running the translate-UI step from Node.js without the CLI:

ts
import { loadI18nConfigFromFile, runTranslateUI } from 'ai-i18n-tools';

// Config must have features.translateUIStrings: true (and valid targetLocales, etc.).
const config = loadI18nConfigFromFile('ai-i18n-tools.config.json');

const summary = await runTranslateUI(config, {
  cwd: process.cwd(),
  locales: config.targetLocales,
  force: false,
  dryRun: false,
  verbose: false,
});
console.log(
  `Updated ${summary.stringsUpdated} string(s); locales touched: ${summary.localesTouched.join(', ')}`
);

Scaffold a config from Node.js (optional fourth argument selects the built-in preset; defaults to openrouter):

ts
import { writeInitConfigFile } from 'ai-i18n-tools';

writeInitConfigFile('ai-i18n-tools.config.json', 'uiMarkdown', process.cwd(), 'anthropic');

Key exports (commonly used — see src/index.ts for the full public surface):

ExportDescription
loadI18nConfigFromFileLoad, merge, validate config from a JSON file.
parseI18nConfigValidate a raw config object.
TranslationCacheSQLite cache - instantiate with a cacheDir path.
UIStringExtractorExtract t("…") strings from JS/TS source.
collectHtmlI18nStrings / markHtmlContentScan / insert data-i18n* markers in HTML (powers extract for .html and the mark-html command).
MarkdownExtractorExtract translatable segments from markdown.
JsonExtractorExtract from Docusaurus JSON label files (UI catalogs, not MDX body).
SvgExtractorExtract from SVG files.
LlmClientMake translation requests to the active LLM provider (OpenRouterClient is a deprecated alias).
PlaceholderHandlerProtect/restore markdown syntax around translation (HTML tags, admonitions, anchors, MDX comments/JSX/braces, URLs, inline code, emphasis).
protectMdx / restoreMdxProtect/restore MDX comments, JSX tags, brace expressions, and JSX string attributes (called by PlaceholderHandler; also exported for direct use).
splitTranslatableIntoBatchesGroup segments into LLM-sized batches.
validateTranslationStructural checks after translation (async — must be awaited).
resolveDocumentationOutputPathResolve output file path for a translated document.
Glossary / GlossaryMatcherLoad and apply translation glossaries.
runTranslateUIProgrammatic translate-UI entry point.
writeInitConfigFileWrite a starter config JSON (template, optional providerKey defaulting to openrouter).
DEFAULT_INIT_MODELS_BY_PROVIDERStarter translationModels per built-in preset used by init -P.
PROVIDER_PRESETSBuilt-in provider preset map (baseUrl, apiKeyEnv).

Released under the MIT License.