i18n-ai
Version:
AI-powered translation tool for i18n JSON files
271 lines (258 loc) • 9.24 kB
TypeScript
type SupportedLanguage = string;
type SupportedProvider = "openai" | "anthropic" | "gemini" | "deepseek" | "xai" | "custom";
type OpenAIModel = "gpt-4" | "gpt-4-turbo-preview" | "gpt-3.5-turbo" | "gpt-4o" | "chatgpt-4o-latest" | "gpt-4o-mini" | "o1" | "o1-mini" | "o3-mini";
type AnthropicModel = "claude-3-5-sonnet-latest" | "claude-3-5-haiku-latest" | "claude-3-opus-latest" | "claude-3-sonnet" | "claude-3-haiku";
type GeminiModel = "gemini-2.0-flash" | "gemini-2.0-flash-lite" | "gemini-1.5-flash" | "gemini-1.5-pro";
type DeepSeekModel = "deepseek-chat";
type XAIModel = "grok-2-1212";
type ProviderModel = OpenAIModel | AnthropicModel | GeminiModel | DeepSeekModel | XAIModel | string;
interface ModelInfo$1 {
id: string;
name: string;
maxTokens: number;
outputTokens: number;
isDeprecated?: boolean;
deprecationDate?: string;
replacedBy?: string;
}
interface LanguageFile {
path: string;
code: SupportedLanguage;
}
interface ExportConfig {
/**
* Path where the CSV file will be saved
* @default './translations-export.csv'
*/
outputPath?: string;
/**
* Character to use as CSV delimiter
* @default ','
*/
delimiter?: string;
}
interface ImportConfig {
/**
* Character to use as CSV delimiter
* @default ','
*/
delimiter?: string;
/**
* Whether to skip the first row (header)
* @default true
*/
skipHeader?: boolean;
/**
* Whether to overwrite existing translations
* @default false
*/
overwrite?: boolean;
}
interface CustomProviderConfig {
/**
* The URL endpoint for the custom translation provider
*/
url: string;
/**
* HTTP method to use (default: POST)
*/
method?: "GET" | "POST";
/**
* Custom headers to send with the request (add your own auth headers here)
*/
headers?: Record<string, string>;
/**
* Request body template as an object or JSON string
* Use "{{text}}" placeholder where the text to translate should be inserted
* Use "{{targetLang}}" placeholder where the target language code should be inserted
* Use "{{sourceLang}}" placeholder where the source language code should be inserted
* Example: {"text": "{{text}}", "to": "{{targetLang}}", "from": "{{sourceLang}}"}
*/
body?: string | object;
/**
* JSON path to extract the translated text from the response
* Example: "data.translation" or "result"
* If not provided, the entire response body will be treated as the translation
*/
responsePath?: string;
}
interface TranslationConfig {
source: LanguageFile;
targets: LanguageFile[];
provider: SupportedProvider;
model?: ProviderModel;
chunkSize?: number;
concurrency?: number;
overwrite?: boolean;
description?: string;
tone?: string;
translateAllAtOnce?: boolean;
ignoreKeys?: string[];
/**
* CSV export configuration
*/
export?: ExportConfig;
/**
* CSV import configuration
*/
import?: ImportConfig;
/**
* Custom provider configuration
* When specified, this takes precedence over the regular provider
*/
customProvider?: CustomProviderConfig;
/**
* Whether to stop the entire translation process if an error occurs
* Default: true
*/
stopOnError?: boolean;
}
interface TranslateFilesOptions {
configPath?: string;
overwrite?: boolean;
}
interface TranslationProvider {
translate(text: string, targetLang: string): Promise<string>;
name: SupportedProvider;
model: ProviderModel;
}
declare function translateFiles(options?: TranslateFilesOptions): Promise<void>;
declare class OpenAIProvider implements TranslationProvider {
private apiKey;
private config;
name: "openai";
model: OpenAIModel;
constructor(apiKey: string, model: OpenAIModel | undefined, config: TranslationConfig);
translate(text: string, targetLang: string): Promise<string>;
}
declare class AnthropicProvider implements TranslationProvider {
private apiKey;
private config;
name: "anthropic";
model: AnthropicModel;
private lastRequestTime;
private readonly minRequestInterval;
constructor(apiKey: string, model: AnthropicModel | undefined, config: TranslationConfig);
private rateLimit;
translate(text: string, targetLang: string): Promise<string>;
}
declare class GeminiProvider implements TranslationProvider {
private apiKey;
private config;
name: "gemini";
model: GeminiModel;
constructor(apiKey: string, model: GeminiModel | undefined, config: TranslationConfig);
translate(text: string, targetLang: string): Promise<string>;
}
declare class DeepSeekProvider implements TranslationProvider {
private apiKey;
private config;
name: "deepseek";
model: DeepSeekModel;
constructor(apiKey: string, model: DeepSeekModel | undefined, config: TranslationConfig);
translate(text: string, targetLang: string): Promise<string>;
}
declare class XAIProvider implements TranslationProvider {
private apiKey;
private config;
name: "xai";
model: XAIModel;
constructor(apiKey: string, model: XAIModel | undefined, config: TranslationConfig);
translate(text: string, targetLang: string): Promise<string>;
}
type ModelInfo = ModelInfo$1;
declare const OPENAI_MODELS: Record<OpenAIModel, ModelInfo>;
declare const ANTHROPIC_MODELS: Record<AnthropicModel, ModelInfo>;
declare const GEMINI_MODELS: Record<GeminiModel, ModelInfo>;
declare const DEEPSEEK_MODELS: Record<DeepSeekModel, ModelInfo>;
declare const XAI_MODELS: Record<XAIModel, ModelInfo>;
declare function isModelDeprecated(model: string): boolean;
declare function getModelInfo(model: string): ModelInfo | undefined;
declare function getDefaultModel(provider: SupportedProvider): ProviderModel;
declare function validateModel(provider: string, model: string): void;
/**
* A provider for custom translation services defined by the user
*/
declare class CustomProvider implements TranslationProvider {
private config;
private translationConfig;
name: "custom";
model: "custom-model";
static logFilePaths: {
errorLogPath?: string;
responseLogPath?: string;
}[];
constructor(config: CustomProviderConfig, translationConfig: TranslationConfig);
/**
* Logs error information to a file and saves the provider response
* @param error The error that occurred
* @param response The provider response (if available)
* @param requestData The request data sent to the provider
* @returns An object containing the paths to the log files
*/
private logErrorToFile;
/**
* Sends a request to the custom endpoint for translation
* @param text The text to translate
* @param targetLang The target language code
* @returns The translated text
*/
translate(text: string, targetLang: string): Promise<string>;
/**
* Prepares the request body with placeholders replaced
*/
private prepareRequestBody;
}
/**
* Returns information about all available providers and their models
* @returns An object containing all supported providers and their models
*/
declare function getAvailableProviders(): {
providers: SupportedProvider[];
models: {
openai: Record<OpenAIModel, ModelInfo$1>;
anthropic: Record<AnthropicModel, ModelInfo$1>;
gemini: Record<GeminiModel, ModelInfo$1>;
deepseek: Record<"deepseek-chat", ModelInfo$1>;
xai: Record<"grok-2-1212", ModelInfo$1>;
custom: {
"custom-model": {
id: string;
name: string;
maxTokens: number;
outputTokens: number;
};
};
};
defaultModels: {
openai: string;
anthropic: string;
gemini: string;
deepseek: string;
xai: string;
custom: string;
};
};
declare function createProvider(type: SupportedProvider, apiKey: string, config: TranslationConfig, model?: ProviderModel): TranslationProvider;
declare function loadConfig(configPath?: string): TranslationConfig;
declare function validateConfig(config: TranslationConfig): void;
interface ExportOptions {
/**
* The output file path for the CSV
* @default './translations-export.csv'
*/
outputPath?: string;
/**
* The delimiter to use in the CSV
* @default ','
*/
delimiter?: string;
}
/**
* Exports all translations to a CSV file
* CSV Structure:
* - First column: Translation key (flattened with dots)
* - Following columns: One column per language
*/
declare function exportTranslationsToCSV(config: TranslationConfig, options?: ExportOptions): Promise<void>;
export { ANTHROPIC_MODELS, AnthropicProvider, CustomProvider, DEEPSEEK_MODELS, DeepSeekProvider, GEMINI_MODELS, GeminiProvider, OPENAI_MODELS, OpenAIProvider, type TranslateFilesOptions, type TranslationConfig, XAIProvider, XAI_MODELS, createProvider, exportTranslationsToCSV, getAvailableProviders, getDefaultModel, getModelInfo, isModelDeprecated, loadConfig, translateFiles, validateConfig, validateModel };