poly-lexis 0.1.0 → 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.d.ts CHANGED
@@ -33,11 +33,112 @@ interface ValidationResult {
33
33
  declare const DEFAULT_CONFIG: Required<TranslationConfig>;
34
34
  declare const DEFAULT_LANGUAGES: readonly ["en", "fr", "it", "pl", "es", "pt", "de", "de_at", "nl", "sv", "hu", "cs", "ja", "zh_hk"];
35
35
 
36
+ interface AddKeyOptions {
37
+ /** Namespace for the translation key */
38
+ namespace: string;
39
+ /** Translation key */
40
+ key: string;
41
+ /** Translation value (English) */
42
+ value: string;
43
+ /** Auto-translate to all languages */
44
+ autoTranslate?: boolean;
45
+ /** Google Translate API key */
46
+ apiKey?: string;
47
+ }
48
+ /**
49
+ * Add a new translation key to all languages
50
+ */
51
+ declare function addTranslationKey(projectRoot: string, options: AddKeyOptions): Promise<void>;
52
+ /**
53
+ * Add multiple translation keys at once
54
+ */
55
+ declare function addTranslationKeys(projectRoot: string, entries: TranslationEntry[], autoTranslate?: boolean, apiKey?: string): Promise<void>;
56
+
57
+ interface AutoFillOptions {
58
+ /** Language to fill translations for */
59
+ language?: string;
60
+ /** Google Translate API key */
61
+ apiKey?: string;
62
+ /** Maximum number of translations to process */
63
+ limit?: number;
64
+ /** Delay between translations in milliseconds */
65
+ delayMs?: number;
66
+ /** Dry run - don't actually write translations */
67
+ dryRun?: boolean;
68
+ }
69
+ /**
70
+ * Automatically fill empty or missing translations for a language
71
+ */
72
+ declare function autoFillTranslations(projectRoot?: string, options?: AutoFillOptions): Promise<void>;
73
+ /**
74
+ * Fill translations for a specific namespace and language
75
+ */
76
+ declare function fillNamespace(projectRoot: string, language: string, namespace: string, apiKey: string): Promise<void>;
77
+
78
+ /**
79
+ * Generate TypeScript types from translation files
80
+ */
81
+ declare function generateTranslationTypes(projectRoot?: string): void;
82
+
83
+ /**
84
+ * Detect existing translation structure in common locations
85
+ */
86
+ declare function detectExistingTranslations(projectRoot: string): {
87
+ path: string | null;
88
+ languages: string[];
89
+ };
90
+ /**
91
+ * Initialize translation structure for a project
92
+ */
93
+ declare function initTranslations(projectRoot: string, config?: TranslationConfig): void;
94
+ /**
95
+ * Load translation configuration from .translationsrc.json
96
+ */
97
+ declare function loadConfig(projectRoot: string): Required<TranslationConfig>;
98
+
99
+ /**
100
+ * Interactive initialization of translation structure
101
+ */
102
+ declare function initTranslationsInteractive(projectRoot?: string): Promise<void>;
103
+
104
+ interface ManageTranslationsOptions {
105
+ /** Auto-fill missing translations */
106
+ autoFill?: boolean;
107
+ /** Google Translate API key for auto-fill */
108
+ apiKey?: string;
109
+ /** Maximum translations to fill */
110
+ limit?: number;
111
+ /** Specific language to process */
112
+ language?: string;
113
+ /** Skip type generation */
114
+ skipTypes?: boolean;
115
+ /** Dry run mode */
116
+ dryRun?: boolean;
117
+ }
118
+ /**
119
+ * Smart translation management - handles init, validation, auto-fill, and type generation
120
+ * based on the current state of the project
121
+ */
122
+ declare function manageTranslations(projectRoot?: string, options?: ManageTranslationsOptions): Promise<void>;
123
+
124
+ /**
125
+ * Validate all translations against the source language
126
+ * Checks for missing keys and empty values
127
+ */
128
+ declare function validateTranslations(projectRoot?: string): ValidationResult;
129
+ /**
130
+ * Get all missing or empty translations for a specific language
131
+ */
132
+ declare function getMissingForLanguage(projectRoot: string, language: string): Array<MissingTranslation & {
133
+ type: 'missing' | 'empty';
134
+ }>;
135
+
36
136
  /**
37
137
  * Supported language codes for translations
38
- * Based on ISO 639-1 and regional variants
138
+ * Based on Google Translate API supported languages (ISO 639-1 and regional variants)
139
+ * Complete list of all languages supported by Google Cloud Translation API
39
140
  */
40
- declare const SUPPORTED_LANGUAGES: readonly ["en", "fr", "it", "pl", "es", "pt", "de", "de_at", "nl", "sv", "hu", "cs", "ja", "zh_hk", "zh_cn", "ko", "ru", "ar", "he", "tr", "da", "fi", "no", "pt_br"];
141
+ declare const SUPPORTED_LANGUAGES: readonly ["af", "sq", "am", "ar", "hy", "as", "ay", "az", "bm", "eu", "be", "bn", "bho", "bs", "bg", "ca", "ceb", "ny", "zh", "zh_cn", "zh_tw", "co", "hr", "cs", "da", "dv", "doi", "nl", "en", "eo", "et", "ee", "tl", "fi", "fr", "gl", "ka", "de", "el", "gn", "gu", "ht", "ha", "haw", "iw", "he", "hi", "hmn", "hu", "is", "ig", "ilo", "id", "ga", "it", "ja", "jw", "kn", "kk", "km", "rw", "gom", "ko", "kri", "ku", "ckb", "ky", "lo", "la", "lv", "ln", "lt", "lg", "lb", "mk", "mai", "mg", "ms", "ml", "mt", "mi", "mr", "mni", "lus", "mn", "my", "ne", "no", "or", "om", "ps", "fa", "pl", "pt", "pt_br", "pa", "qu", "ro", "ru", "sm", "sa", "gd", "sr", "st", "sn", "sd", "si", "sk", "sl", "so", "es", "su", "sw", "sv", "tg", "ta", "tt", "te", "th", "ti", "ts", "tr", "tk", "ak", "uk", "ur", "ug", "uz", "vi", "cy", "xh", "yi", "yo", "zu"];
41
142
  type SupportedLanguage = (typeof SUPPORTED_LANGUAGES)[number];
42
143
  /**
43
144
  * JSON Schema for .translationsrc.json
@@ -58,7 +159,7 @@ declare const TRANSLATION_CONFIG_SCHEMA: {
58
159
  description: string;
59
160
  items: {
60
161
  type: string;
61
- enum: readonly ["en", "fr", "it", "pl", "es", "pt", "de", "de_at", "nl", "sv", "hu", "cs", "ja", "zh_hk", "zh_cn", "ko", "ru", "ar", "he", "tr", "da", "fi", "no", "pt_br"];
162
+ enum: readonly ["af", "sq", "am", "ar", "hy", "as", "ay", "az", "bm", "eu", "be", "bn", "bho", "bs", "bg", "ca", "ceb", "ny", "zh", "zh_cn", "zh_tw", "co", "hr", "cs", "da", "dv", "doi", "nl", "en", "eo", "et", "ee", "tl", "fi", "fr", "gl", "ka", "de", "el", "gn", "gu", "ht", "ha", "haw", "iw", "he", "hi", "hmn", "hu", "is", "ig", "ilo", "id", "ga", "it", "ja", "jw", "kn", "kk", "km", "rw", "gom", "ko", "kri", "ku", "ckb", "ky", "lo", "la", "lv", "ln", "lt", "lg", "lb", "mk", "mai", "mg", "ms", "ml", "mt", "mi", "mr", "mni", "lus", "mn", "my", "ne", "no", "or", "om", "ps", "fa", "pl", "pt", "pt_br", "pa", "qu", "ro", "ru", "sm", "sa", "gd", "sr", "st", "sn", "sd", "si", "sk", "sl", "so", "es", "su", "sw", "sv", "tg", "ta", "tt", "te", "th", "ti", "ts", "tr", "tk", "ak", "uk", "ur", "ug", "uz", "vi", "cy", "xh", "yi", "yo", "zu"];
62
163
  };
63
164
  minItems: number;
64
165
  uniqueItems: boolean;
@@ -66,7 +167,7 @@ declare const TRANSLATION_CONFIG_SCHEMA: {
66
167
  sourceLanguage: {
67
168
  type: string;
68
169
  description: string;
69
- enum: readonly ["en", "fr", "it", "pl", "es", "pt", "de", "de_at", "nl", "sv", "hu", "cs", "ja", "zh_hk", "zh_cn", "ko", "ru", "ar", "he", "tr", "da", "fi", "no", "pt_br"];
170
+ enum: readonly ["af", "sq", "am", "ar", "hy", "as", "ay", "az", "bm", "eu", "be", "bn", "bho", "bs", "bg", "ca", "ceb", "ny", "zh", "zh_cn", "zh_tw", "co", "hr", "cs", "da", "dv", "doi", "nl", "en", "eo", "et", "ee", "tl", "fi", "fr", "gl", "ka", "de", "el", "gn", "gu", "ht", "ha", "haw", "iw", "he", "hi", "hmn", "hu", "is", "ig", "ilo", "id", "ga", "it", "ja", "jw", "kn", "kk", "km", "rw", "gom", "ko", "kri", "ku", "ckb", "ky", "lo", "la", "lv", "ln", "lt", "lg", "lb", "mk", "mai", "mg", "ms", "ml", "mt", "mi", "mr", "mni", "lus", "mn", "my", "ne", "no", "or", "om", "ps", "fa", "pl", "pt", "pt_br", "pa", "qu", "ro", "ru", "sm", "sa", "gd", "sr", "st", "sn", "sd", "si", "sk", "sl", "so", "es", "su", "sw", "sv", "tg", "ta", "tt", "te", "th", "ti", "ts", "tr", "tk", "ak", "uk", "ur", "ug", "uz", "vi", "cy", "xh", "yi", "yo", "zu"];
70
171
  default: string;
71
172
  };
72
173
  typesOutputPath: {
@@ -91,154 +192,156 @@ declare function validateLanguages(languages: string[]): {
91
192
  };
92
193
 
93
194
  /**
94
- * Read all translation files for a specific language
195
+ * Translation provider interface
196
+ * Implement this interface to create custom translation providers
95
197
  */
96
- declare function readTranslations(translationsPath: string, language: string): TranslationFiles;
97
198
  /**
98
- * Write translation file for a specific language and namespace
99
- */
100
- declare function writeTranslation(translationsPath: string, language: string, namespace: string, translations: TranslationFile): void;
101
- /**
102
- * Get all available languages from the translations directory
103
- */
104
- declare function getAvailableLanguages(translationsPath: string): string[];
105
- /**
106
- * Get all namespaces for a specific language
199
+ * Options for translating text
107
200
  */
108
- declare function getNamespaces(translationsPath: string, language: string): string[];
201
+ interface TranslateOptions {
202
+ /** The text to translate */
203
+ text: string;
204
+ /** Source language code (e.g., 'en', 'fr') */
205
+ sourceLang: string;
206
+ /** Target language code (e.g., 'es', 'pt_BR') */
207
+ targetLang: string;
208
+ /** API key or credentials (optional, depending on provider) */
209
+ apiKey?: string;
210
+ /** Additional provider-specific options */
211
+ [key: string]: unknown;
212
+ }
109
213
  /**
110
- * Check if a string contains interpolation variables (e.g., {{variable}})
214
+ * Result of a translation operation
111
215
  */
112
- declare function hasInterpolation(text: string): boolean;
216
+ interface TranslationResult {
217
+ /** The translated text */
218
+ translatedText: string;
219
+ /** Optional metadata from the translation service */
220
+ metadata?: Record<string, unknown>;
221
+ }
113
222
  /**
114
- * Extract interpolation variable names from a string
223
+ * Translation provider interface
224
+ * All custom translation providers must implement this interface
115
225
  */
116
- declare function extractVariables(text: string): string[];
226
+ interface TranslationProvider {
227
+ /**
228
+ * Translate a single text string
229
+ * @param options - Translation options
230
+ * @returns Promise resolving to the translated text
231
+ */
232
+ translate(options: TranslateOptions): Promise<string>;
233
+ /**
234
+ * Translate multiple texts in batch
235
+ * @param texts - Array of texts to translate
236
+ * @param sourceLang - Source language code
237
+ * @param targetLang - Target language code
238
+ * @param apiKey - API key or credentials (optional)
239
+ * @param delayMs - Optional delay between requests to avoid rate limiting
240
+ * @returns Promise resolving to array of translated texts
241
+ */
242
+ translateBatch(texts: string[], sourceLang: string, targetLang: string, apiKey?: string, delayMs?: number): Promise<string[]>;
243
+ /**
244
+ * Optional method to validate the provider configuration
245
+ * @returns Promise resolving to true if configuration is valid
246
+ */
247
+ validateConfig?(): Promise<boolean>;
248
+ }
117
249
  /**
118
- * Validate that translated text has the same variables as source text
250
+ * Type for a function that creates a translation provider
119
251
  */
120
- declare function validateVariables(sourceText: string, translatedText: string): boolean;
252
+ type TranslationProviderFactory = () => TranslationProvider;
253
+
121
254
  /**
122
- * Sort object keys alphabetically
255
+ * Google Translate provider implementation
256
+ * Uses Google Cloud Translation API v2
123
257
  */
124
- declare function sortKeys<T extends Record<string, unknown>>(obj: T): T;
258
+
125
259
  /**
126
- * Ensure translations path exists and has proper structure
260
+ * Google Translate provider
261
+ * Implements the TranslationProvider interface
127
262
  */
128
- declare function ensureTranslationsStructure(translationsPath: string, languages: string[]): void;
263
+ declare class GoogleTranslateProvider implements TranslationProvider {
264
+ translate(options: TranslateOptions): Promise<string>;
265
+ translateBatch(texts: string[], sourceLang: string, targetLang: string, apiKey?: string, delayMs?: number): Promise<string[]>;
266
+ validateConfig(): Promise<boolean>;
267
+ }
129
268
 
130
269
  /**
131
- * Translation utilities for translating text using Google Translate API
270
+ * Translation utilities with support for custom translation providers
132
271
  * Only translates content outside of {{variable}} interpolations
133
272
  */
273
+
134
274
  /**
135
- * Translate text using Google Translate API
136
- * Preserves {{variable}} interpolations by temporarily replacing them
275
+ * Set a custom translation provider
276
+ * @param provider - Custom translation provider implementing TranslationProvider interface
137
277
  */
138
- declare function translateText(text: string, targetLang: string, sourceLang?: string, apiKey?: string): Promise<string>;
278
+ declare function setTranslationProvider(provider: TranslationProvider): void;
139
279
  /**
140
- * Translate multiple texts in batch
280
+ * Get the active translation provider (custom or default)
141
281
  */
142
- declare function translateBatch(texts: string[], targetLang: string, sourceLang?: string, apiKey?: string, delayMs?: number): Promise<string[]>;
143
-
282
+ declare function getTranslationProvider(): TranslationProvider;
144
283
  /**
145
- * Detect existing translation structure in common locations
284
+ * Reset to the default Google Translate provider
146
285
  */
147
- declare function detectExistingTranslations(projectRoot: string): {
148
- path: string | null;
149
- languages: string[];
150
- };
286
+ declare function resetTranslationProvider(): void;
151
287
  /**
152
- * Initialize translation structure for a project
288
+ * Translate text using the active translation provider
289
+ * Preserves {{variable}} interpolations by temporarily replacing them
290
+ *
291
+ * @param text - Text to translate
292
+ * @param targetLang - Target language code
293
+ * @param sourceLang - Source language code (default: "en")
294
+ * @param apiKey - API key for the translation service
295
+ * @returns Promise resolving to translated text
153
296
  */
154
- declare function initTranslations(projectRoot: string, config?: TranslationConfig): void;
297
+ declare function translateText(text: string, targetLang: string, sourceLang?: string, apiKey?: string): Promise<string>;
155
298
  /**
156
- * Load translation configuration from .translationsrc.json
299
+ * Translate multiple texts in batch
300
+ *
301
+ * @param texts - Array of texts to translate
302
+ * @param targetLang - Target language code
303
+ * @param sourceLang - Source language code (default: "en")
304
+ * @param apiKey - API key for the translation service
305
+ * @param delayMs - Delay between requests in milliseconds (default: 100)
306
+ * @returns Promise resolving to array of translated texts
157
307
  */
158
- declare function loadConfig(projectRoot: string): Required<TranslationConfig>;
308
+ declare function translateBatch(texts: string[], targetLang: string, sourceLang?: string, apiKey?: string, delayMs?: number): Promise<string[]>;
159
309
 
160
310
  /**
161
- * Interactive initialization of translation structure
311
+ * Read all translation files for a specific language
162
312
  */
163
- declare function initTranslationsInteractive(projectRoot?: string): Promise<void>;
164
-
165
- interface AddKeyOptions {
166
- /** Namespace for the translation key */
167
- namespace: string;
168
- /** Translation key */
169
- key: string;
170
- /** Translation value (English) */
171
- value: string;
172
- /** Auto-translate to all languages */
173
- autoTranslate?: boolean;
174
- /** Google Translate API key */
175
- apiKey?: string;
176
- }
313
+ declare function readTranslations(translationsPath: string, language: string): TranslationFiles;
177
314
  /**
178
- * Add a new translation key to all languages
315
+ * Write translation file for a specific language and namespace
179
316
  */
180
- declare function addTranslationKey(projectRoot: string, options: AddKeyOptions): Promise<void>;
317
+ declare function writeTranslation(translationsPath: string, language: string, namespace: string, translations: TranslationFile): void;
181
318
  /**
182
- * Add multiple translation keys at once
319
+ * Get all available languages from the translations directory
183
320
  */
184
- declare function addTranslationKeys(projectRoot: string, entries: TranslationEntry[], autoTranslate?: boolean, apiKey?: string): Promise<void>;
185
-
321
+ declare function getAvailableLanguages(translationsPath: string): string[];
186
322
  /**
187
- * Validate all translations against the source language
188
- * Checks for missing keys and empty values
323
+ * Get all namespaces for a specific language
189
324
  */
190
- declare function validateTranslations(projectRoot?: string): ValidationResult;
325
+ declare function getNamespaces(translationsPath: string, language: string): string[];
191
326
  /**
192
- * Get all missing or empty translations for a specific language
327
+ * Check if a string contains interpolation variables (e.g., {{variable}})
193
328
  */
194
- declare function getMissingForLanguage(projectRoot: string, language: string): Array<MissingTranslation & {
195
- type: 'missing' | 'empty';
196
- }>;
197
-
198
- interface AutoFillOptions {
199
- /** Language to fill translations for */
200
- language?: string;
201
- /** Google Translate API key */
202
- apiKey?: string;
203
- /** Maximum number of translations to process */
204
- limit?: number;
205
- /** Delay between translations in milliseconds */
206
- delayMs?: number;
207
- /** Dry run - don't actually write translations */
208
- dryRun?: boolean;
209
- }
329
+ declare function hasInterpolation(text: string): boolean;
210
330
  /**
211
- * Automatically fill empty or missing translations for a language
331
+ * Extract interpolation variable names from a string
212
332
  */
213
- declare function autoFillTranslations(projectRoot?: string, options?: AutoFillOptions): Promise<void>;
333
+ declare function extractVariables(text: string): string[];
214
334
  /**
215
- * Fill translations for a specific namespace and language
335
+ * Validate that translated text has the same variables as source text
216
336
  */
217
- declare function fillNamespace(projectRoot: string, language: string, namespace: string, apiKey: string): Promise<void>;
218
-
337
+ declare function validateVariables(sourceText: string, translatedText: string): boolean;
219
338
  /**
220
- * Generate TypeScript types from translation files
339
+ * Sort object keys alphabetically
221
340
  */
222
- declare function generateTranslationTypes(projectRoot?: string): void;
223
-
224
- interface ManageTranslationsOptions {
225
- /** Auto-fill missing translations */
226
- autoFill?: boolean;
227
- /** Google Translate API key for auto-fill */
228
- apiKey?: string;
229
- /** Maximum translations to fill */
230
- limit?: number;
231
- /** Specific language to process */
232
- language?: string;
233
- /** Skip type generation */
234
- skipTypes?: boolean;
235
- /** Dry run mode */
236
- dryRun?: boolean;
237
- }
341
+ declare function sortKeys<T extends Record<string, unknown>>(obj: T): T;
238
342
  /**
239
- * Smart translation management - handles init, validation, auto-fill, and type generation
240
- * based on the current state of the project
343
+ * Ensure translations path exists and has proper structure
241
344
  */
242
- declare function manageTranslations(projectRoot?: string, options?: ManageTranslationsOptions): Promise<void>;
345
+ declare function ensureTranslationsStructure(translationsPath: string, languages: string[]): void;
243
346
 
244
- export { DEFAULT_CONFIG, DEFAULT_LANGUAGES, type ManageTranslationsOptions, type MissingTranslation, SUPPORTED_LANGUAGES, type SupportedLanguage, TRANSLATION_CONFIG_SCHEMA, type TranslationConfig, type TranslationEntry, type TranslationFile, type TranslationFiles, type ValidationResult, addTranslationKey, addTranslationKeys, autoFillTranslations, detectExistingTranslations, ensureTranslationsStructure, extractVariables, fillNamespace, generateTranslationTypes, getAvailableLanguages, getMissingForLanguage, getNamespaces, hasInterpolation, initTranslations, initTranslationsInteractive, isValidLanguage, loadConfig, manageTranslations, readTranslations, sortKeys, translateBatch, translateText, validateLanguages, validateTranslations, validateVariables, writeTranslation };
347
+ export { DEFAULT_CONFIG, DEFAULT_LANGUAGES, GoogleTranslateProvider, type ManageTranslationsOptions, type MissingTranslation, SUPPORTED_LANGUAGES, type SupportedLanguage, TRANSLATION_CONFIG_SCHEMA, type TranslateOptions, type TranslationConfig, type TranslationEntry, type TranslationFile, type TranslationFiles, type TranslationProvider, type TranslationProviderFactory, type TranslationResult, type ValidationResult, addTranslationKey, addTranslationKeys, autoFillTranslations, detectExistingTranslations, ensureTranslationsStructure, extractVariables, fillNamespace, generateTranslationTypes, getAvailableLanguages, getMissingForLanguage, getNamespaces, getTranslationProvider, hasInterpolation, initTranslations, initTranslationsInteractive, isValidLanguage, loadConfig, manageTranslations, readTranslations, resetTranslationProvider, setTranslationProvider, sortKeys, translateBatch, translateText, validateLanguages, validateTranslations, validateVariables, writeTranslation };