i18n-email

API Reference

Full reference for createI18nEmail and all types.

createI18nEmail(config)

Creates and returns an i18n-email client.

import { createI18nEmail } from "i18n-email";

const i18nEmail = createI18nEmail(config);

I18nEmailConfig

I18nEmailConfig is a discriminated union. Choose exactly one backend:

OpenAI (default)

OptionTypeRequiredDefaultDescription
openaiApiKeystringYesOpenAI API key
modelstringNo"gpt-4o"OpenAI model name
baseURLstringNoOverride the API base URL (Azure, Groq…)
maxRetriesnumberNo2Retries on transient errors

Vercel AI SDK

Set model to any Vercel AI SDK LanguageModel (e.g. openai("gpt-4o") from @ai-sdk/openai). openaiApiKey, baseURL, and maxRetries are not available — the provider handles authentication.

TanStack AI

Set adapter to any TanStack AI TextAdapter (e.g. createOpenaiChat(...) from @tanstack/ai-openai). openaiApiKey, baseURL, and maxRetries are not available — the adapter handles authentication.

Shared options

OptionTypeRequiredDefaultDescription
batchSizenumberNo50Max strings per API request
cacheCacheProviderNoCache adapter to skip redundant calls
onTranslate(info) => voidNoHook called after every translate call

Returns

{
  translate: (options: TranslateOptions) => Promise<TranslateResult>;
}

translate(options)

Translates an email's subject and body to the target locale.

const result = await i18nEmail.translate(options);

TranslateOptions

Exactly one of react or html must be provided.

OptionTypeRequiredDescription
localestringYesBCP 47 locale code of the target language (e.g. "fr", "ar", "zh-TW")
subjectstringYesEmail subject line to translate
reactReactElementOne ofReact Email component — rendered to HTML before translation
htmlstringOne ofRaw HTML string to translate directly

TranslateResult

interface TranslateResult {
  subject: string;
  html: string;
}
FieldDescription
subjectTranslated subject line
htmlTranslated HTML body

If the source email is already in the target locale, the original subject and html are returned unchanged.


CacheProvider

interface CacheProvider {
  prefix?: string;
  get: (key: string) => Promise<string | null>;
  set: (key: string, value: string) => Promise<void>;
}
MemberTypeDescription
prefixstringOptional string prepended to every cache key
get(key: string) => Promise<string | null>Returns the cached value, or null on a miss
set(key: string, value: string) => Promise<void>Stores a value under the given key

Cache keys are SHA-256 hashes of the concatenated HTML, subject, and locale. See Caching for adapter examples.


Exported types

All types are available as named exports from i18n-email:

import type {
  I18nEmailConfig,
  AiLanguageModel,
  TanstackAiTextAdapter,
  CacheProvider,
  TranslateOptions,
  TranslateOptionsReact,
  TranslateOptionsHtml,
  TranslateResult,
  TranslationResponse,
  TranslateCallbackInfo,
} from "i18n-email";

Errors

The library throws a plain Error with a descriptive message in these cases:

SituationMessage prefix
No openaiApiKey when using string modeli18n-email: openaiApiKey is required when...
ai package not installed for AI SDK modeli18n-email: The "ai" package is required...
@tanstack/ai not installed for TanStack modeli18n-email: The "@tanstack/ai" package is required...
React component fails to renderi18n-email: Failed to render React component:
OpenAI returns an empty responsei18n-email: OpenAI returned an empty response
OpenAI returns malformed JSONi18n-email: OpenAI returned malformed JSON:
Response shape is unexpectedi18n-email: Unexpected response shape from OpenAI:
Translation count doesn't match inputi18n-email: Translation count mismatch.

On this page