poly-lexis 0.3.2 → 0.4.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
@@ -1,3 +1,156 @@
1
+ /**
2
+ * Language fallback utilities
3
+ * Provides intelligent fallback logic for unsupported language codes
4
+ */
5
+
6
+ /**
7
+ * Result of language fallback resolution
8
+ */
9
+ interface LanguageFallbackResult {
10
+ /** The resolved language code to use */
11
+ resolvedLanguage: string;
12
+ /** Whether a fallback was used */
13
+ usedFallback: boolean;
14
+ /** The original language code requested */
15
+ originalLanguage: string;
16
+ /** Chain of fallback attempts made */
17
+ fallbackChain?: string[];
18
+ }
19
+ /**
20
+ * Resolve language code with fallback logic
21
+ * Tries to find a supported language for the given provider
22
+ *
23
+ * @param language - Original language code requested
24
+ * @param provider - Translation provider type ('deepl' or 'google')
25
+ * @param enableFallback - Whether to enable fallback logic (default: true)
26
+ * @returns LanguageFallbackResult with resolved language and metadata
27
+ */
28
+ declare function resolveLanguageWithFallback(language: string, provider: TranslationProviderType, enableFallback?: boolean): LanguageFallbackResult;
29
+ /**
30
+ * Log fallback information to console
31
+ * Informs user when a language fallback is being used
32
+ */
33
+ declare function logLanguageFallback(result: LanguageFallbackResult, provider: TranslationProviderType): void;
34
+ /**
35
+ * Get all registered fallback mappings
36
+ * Useful for debugging and documentation
37
+ */
38
+ declare function getFallbackMappings(): Record<string, string[]>;
39
+
40
+ /**
41
+ * Translation provider types
42
+ */
43
+ declare const TRANSLATION_PROVIDERS: readonly ["deepl", "google"];
44
+ type TranslationProviderType = (typeof TRANSLATION_PROVIDERS)[number];
45
+ /**
46
+ * DeepL supported target languages
47
+ * Based on DeepL API v2 documentation
48
+ */
49
+ declare const DEEPL_LANGUAGES: readonly ["ar", "bg", "cs", "da", "de", "el", "en", "en_gb", "en_us", "es", "es_419", "et", "fi", "fr", "he", "hu", "id", "it", "ja", "ko", "lt", "lv", "nb", "nl", "pl", "pt", "pt_br", "pt_pt", "ro", "ru", "sk", "sl", "sv", "th", "tr", "uk", "vi", "zh", "zh_hans", "zh_hant"];
50
+ /**
51
+ * Google Translate supported languages
52
+ * Based on Google Cloud Translation API v2
53
+ * Reference: https://docs.cloud.google.com/translate/docs/languages
54
+ */
55
+ declare const GOOGLE_LANGUAGES: readonly ["af", "sq", "am", "ar", "hy", "az", "eu", "be", "bn", "bs", "bg", "ca", "ceb", "zh", "zh_cn", "zh_tw", "co", "hr", "cs", "da", "nl", "en", "eo", "et", "fi", "fr", "fy", "gl", "ka", "de", "el", "gu", "ht", "ha", "haw", "he", "hi", "hmn", "hu", "is", "ig", "id", "ga", "it", "ja", "jv", "kn", "kk", "km", "rw", "ko", "ku", "ky", "lo", "la", "lv", "lt", "lb", "mk", "mg", "ms", "ml", "mt", "mi", "mr", "mn", "my", "ne", "no", "ny", "or", "ps", "fa", "pl", "pt", "pt_br", "pa", "ro", "ru", "sm", "gd", "sr", "st", "sn", "sd", "si", "sk", "sl", "so", "es", "su", "sw", "sv", "tl", "tg", "ta", "tt", "te", "th", "tr", "tk", "uk", "ur", "ug", "uz", "vi", "cy", "xh", "yi", "yo", "zu"];
56
+ /**
57
+ * All supported language codes (union of DeepL and Google Translate)
58
+ */
59
+ declare const SUPPORTED_LANGUAGES: readonly string[];
60
+ type DeepLLanguage = (typeof DEEPL_LANGUAGES)[number];
61
+ type GoogleLanguage = (typeof GOOGLE_LANGUAGES)[number];
62
+ type SupportedLanguage = (typeof SUPPORTED_LANGUAGES)[number];
63
+ /**
64
+ * JSON Schema for .translationsrc.json
65
+ */
66
+ declare const TRANSLATION_CONFIG_SCHEMA: {
67
+ title: string;
68
+ description: string;
69
+ type: string;
70
+ properties: {
71
+ $schema: {
72
+ type: string;
73
+ description: string;
74
+ };
75
+ translationsPath: {
76
+ type: string;
77
+ description: string;
78
+ default: string;
79
+ examples: string[];
80
+ };
81
+ languages: {
82
+ type: string;
83
+ description: string;
84
+ items: {
85
+ type: string;
86
+ enum: readonly string[];
87
+ };
88
+ minItems: number;
89
+ uniqueItems: boolean;
90
+ default: string[];
91
+ };
92
+ sourceLanguage: {
93
+ type: string;
94
+ description: string;
95
+ enum: readonly string[];
96
+ default: string;
97
+ };
98
+ typesOutputPath: {
99
+ type: string;
100
+ description: string;
101
+ default: string;
102
+ examples: string[];
103
+ };
104
+ provider: {
105
+ type: string;
106
+ description: string;
107
+ enum: readonly ["deepl", "google"];
108
+ default: string;
109
+ };
110
+ useFallbackLanguages: {
111
+ type: string;
112
+ description: string;
113
+ default: boolean;
114
+ };
115
+ };
116
+ required: string[];
117
+ additionalProperties: boolean;
118
+ };
119
+ /**
120
+ * Validate if a language code is supported
121
+ */
122
+ declare function isValidLanguage(lang: string): lang is SupportedLanguage;
123
+ /**
124
+ * Validate if a language is supported by DeepL
125
+ */
126
+ declare function isValidDeepLLanguage(lang: string): lang is DeepLLanguage;
127
+ /**
128
+ * Validate if a language is supported by Google Translate
129
+ */
130
+ declare function isValidGoogleLanguage(lang: string): lang is GoogleLanguage;
131
+ /**
132
+ * Validate if a language is supported by a specific provider
133
+ */
134
+ declare function isValidLanguageForProvider(lang: string, provider: TranslationProviderType): boolean;
135
+ /**
136
+ * Validate languages array
137
+ */
138
+ declare function validateLanguages(languages: string[]): {
139
+ valid: boolean;
140
+ invalid: string[];
141
+ };
142
+ /**
143
+ * Validate languages array for a specific provider
144
+ */
145
+ declare function validateLanguagesForProvider(languages: string[], provider: TranslationProviderType): {
146
+ valid: boolean;
147
+ invalid: string[];
148
+ };
149
+ /**
150
+ * Get supported languages for a specific provider
151
+ */
152
+ declare function getSupportedLanguages(provider: TranslationProviderType): readonly string[];
153
+
1
154
  interface TranslationConfig {
2
155
  /** Path to the translations directory (default: public/static/locales) */
3
156
  translationsPath?: string;
@@ -7,6 +160,10 @@ interface TranslationConfig {
7
160
  sourceLanguage?: string;
8
161
  /** Path to output i18n types (default: src/types/i18nTypes.ts) */
9
162
  typesOutputPath?: string;
163
+ /** Translation provider to use (default: 'deepl') */
164
+ provider?: TranslationProviderType;
165
+ /** Enable automatic language fallback for unsupported regional variants (default: true) */
166
+ useFallbackLanguages?: boolean;
10
167
  }
11
168
  interface TranslationEntry {
12
169
  namespace: string;
@@ -31,7 +188,7 @@ interface ValidationResult {
31
188
  empty: MissingTranslation[];
32
189
  }
33
190
  declare const DEFAULT_CONFIG: Required<TranslationConfig>;
34
- declare const DEFAULT_LANGUAGES: readonly ["en", "fr", "it", "pl", "es", "pt", "de", "de_at", "nl", "sv", "hu", "cs", "ja", "zh_hk"];
191
+ declare const DEFAULT_LANGUAGES: readonly ["en", "fr", "it", "pl", "es", "pt", "de", "nl", "sv", "hu", "cs", "ja"];
35
192
 
36
193
  interface AddKeyOptions {
37
194
  /** Namespace for the translation key */
@@ -57,7 +214,7 @@ declare function addTranslationKeys(projectRoot: string, entries: TranslationEnt
57
214
  interface AutoFillOptions {
58
215
  /** Language to fill translations for */
59
216
  language?: string;
60
- /** Google Translate API key */
217
+ /** Translation API key (for DeepL or Google Translate) */
61
218
  apiKey?: string;
62
219
  /** Maximum number of translations to process */
63
220
  limit?: number;
@@ -134,115 +291,6 @@ declare function getMissingForLanguage(projectRoot: string, language: string): A
134
291
  type: 'missing' | 'empty';
135
292
  }>;
136
293
 
137
- /**
138
- * Translation provider types
139
- */
140
- declare const TRANSLATION_PROVIDERS: readonly ["deepl", "google"];
141
- type TranslationProviderType = (typeof TRANSLATION_PROVIDERS)[number];
142
- /**
143
- * DeepL supported target languages
144
- * Based on DeepL API v2 documentation
145
- */
146
- declare const DEEPL_LANGUAGES: readonly ["ar", "bg", "cs", "da", "de", "el", "en", "en_gb", "en_us", "es", "es_419", "et", "fi", "fr", "he", "hu", "id", "it", "ja", "ko", "lt", "lv", "nb", "nl", "pl", "pt", "pt_br", "pt_pt", "ro", "ru", "sk", "sl", "sv", "th", "tr", "uk", "vi", "zh", "zh_hans", "zh_hant"];
147
- /**
148
- * Google Translate supported languages
149
- * Based on Google Cloud Translation API v2
150
- * Reference: https://docs.cloud.google.com/translate/docs/languages
151
- */
152
- declare const GOOGLE_LANGUAGES: readonly ["af", "sq", "am", "ar", "hy", "az", "eu", "be", "bn", "bs", "bg", "ca", "ceb", "zh", "zh_cn", "zh_tw", "co", "hr", "cs", "da", "nl", "en", "eo", "et", "fi", "fr", "fy", "gl", "ka", "de", "el", "gu", "ht", "ha", "haw", "he", "hi", "hmn", "hu", "is", "ig", "id", "ga", "it", "ja", "jv", "kn", "kk", "km", "rw", "ko", "ku", "ky", "lo", "la", "lv", "lt", "lb", "mk", "mg", "ms", "ml", "mt", "mi", "mr", "mn", "my", "ne", "no", "ny", "or", "ps", "fa", "pl", "pt", "pt_br", "pa", "ro", "ru", "sm", "gd", "sr", "st", "sn", "sd", "si", "sk", "sl", "so", "es", "su", "sw", "sv", "tl", "tg", "ta", "tt", "te", "th", "tr", "tk", "uk", "ur", "ug", "uz", "vi", "cy", "xh", "yi", "yo", "zu"];
153
- /**
154
- * All supported language codes (union of DeepL and Google Translate)
155
- */
156
- declare const SUPPORTED_LANGUAGES: readonly string[];
157
- type DeepLLanguage = (typeof DEEPL_LANGUAGES)[number];
158
- type GoogleLanguage = (typeof GOOGLE_LANGUAGES)[number];
159
- type SupportedLanguage = (typeof SUPPORTED_LANGUAGES)[number];
160
- /**
161
- * JSON Schema for .translationsrc.json
162
- */
163
- declare const TRANSLATION_CONFIG_SCHEMA: {
164
- title: string;
165
- description: string;
166
- type: string;
167
- properties: {
168
- $schema: {
169
- type: string;
170
- description: string;
171
- };
172
- translationsPath: {
173
- type: string;
174
- description: string;
175
- default: string;
176
- examples: string[];
177
- };
178
- languages: {
179
- type: string;
180
- description: string;
181
- items: {
182
- type: string;
183
- enum: readonly string[];
184
- };
185
- minItems: number;
186
- uniqueItems: boolean;
187
- default: string[];
188
- };
189
- sourceLanguage: {
190
- type: string;
191
- description: string;
192
- enum: readonly string[];
193
- default: string;
194
- };
195
- typesOutputPath: {
196
- type: string;
197
- description: string;
198
- default: string;
199
- examples: string[];
200
- };
201
- provider: {
202
- type: string;
203
- description: string;
204
- enum: readonly ["deepl", "google"];
205
- default: string;
206
- };
207
- };
208
- required: string[];
209
- additionalProperties: boolean;
210
- };
211
- /**
212
- * Validate if a language code is supported
213
- */
214
- declare function isValidLanguage(lang: string): lang is SupportedLanguage;
215
- /**
216
- * Validate if a language is supported by DeepL
217
- */
218
- declare function isValidDeepLLanguage(lang: string): lang is DeepLLanguage;
219
- /**
220
- * Validate if a language is supported by Google Translate
221
- */
222
- declare function isValidGoogleLanguage(lang: string): lang is GoogleLanguage;
223
- /**
224
- * Validate if a language is supported by a specific provider
225
- */
226
- declare function isValidLanguageForProvider(lang: string, provider: TranslationProviderType): boolean;
227
- /**
228
- * Validate languages array
229
- */
230
- declare function validateLanguages(languages: string[]): {
231
- valid: boolean;
232
- invalid: string[];
233
- };
234
- /**
235
- * Validate languages array for a specific provider
236
- */
237
- declare function validateLanguagesForProvider(languages: string[], provider: TranslationProviderType): {
238
- valid: boolean;
239
- invalid: string[];
240
- };
241
- /**
242
- * Get supported languages for a specific provider
243
- */
244
- declare function getSupportedLanguages(provider: TranslationProviderType): readonly string[];
245
-
246
294
  /**
247
295
  * Translation provider interface
248
296
  * Implement this interface to create custom translation providers
@@ -259,6 +307,8 @@ interface TranslateOptions {
259
307
  targetLang: string;
260
308
  /** API key or credentials (optional, depending on provider) */
261
309
  apiKey?: string;
310
+ /** Enable automatic language fallback for unsupported regional variants (default: true) */
311
+ useFallbackLanguages?: boolean;
262
312
  /** Additional provider-specific options */
263
313
  [key: string]: unknown;
264
314
  }
@@ -344,9 +394,10 @@ declare function resetTranslationProvider(): void;
344
394
  * @param targetLang - Target language code
345
395
  * @param sourceLang - Source language code (default: "en")
346
396
  * @param apiKey - API key for the translation service
397
+ * @param useFallbackLanguages - Enable automatic language fallback (default: true)
347
398
  * @returns Promise resolving to translated text
348
399
  */
349
- declare function translateText(text: string, targetLang: string, sourceLang?: string, apiKey?: string): Promise<string>;
400
+ declare function translateText(text: string, targetLang: string, sourceLang?: string, apiKey?: string, useFallbackLanguages?: boolean): Promise<string>;
350
401
  /**
351
402
  * Translate multiple texts in batch
352
403
  *
@@ -395,5 +446,30 @@ declare function sortKeys<T extends Record<string, unknown>>(obj: T): T;
395
446
  * Ensure translations path exists and has proper structure
396
447
  */
397
448
  declare function ensureTranslationsStructure(translationsPath: string, languages: string[]): void;
449
+ /**
450
+ * Create an empty translation structure from a source translation file
451
+ * All values are set to empty strings, preserving the key structure
452
+ */
453
+ declare function createEmptyTranslationStructure(sourceFile: TranslationFile): TranslationFile;
454
+ interface SyncResult {
455
+ createdFolders: string[];
456
+ createdFiles: Array<{
457
+ language: string;
458
+ namespace: string;
459
+ path: string;
460
+ }>;
461
+ skippedFiles: Array<{
462
+ language: string;
463
+ namespace: string;
464
+ reason: string;
465
+ }>;
466
+ }
467
+ /**
468
+ * Synchronize translation structure based on source language
469
+ * - Ensures all configured languages have folders
470
+ * - Ensures all namespaces from source language exist in target languages
471
+ * - Creates files with empty values matching source structure
472
+ */
473
+ declare function syncTranslationStructure(translationsPath: string, languages: string[], sourceLanguage: string): SyncResult;
398
474
 
399
- export { DEEPL_LANGUAGES, DEFAULT_CONFIG, DEFAULT_LANGUAGES, type DeepLLanguage, GOOGLE_LANGUAGES, type GoogleLanguage, GoogleTranslateProvider, type ManageTranslationsOptions, type MissingTranslation, SUPPORTED_LANGUAGES, type SupportedLanguage, TRANSLATION_CONFIG_SCHEMA, TRANSLATION_PROVIDERS, type TranslateOptions, type TranslationConfig, type TranslationEntry, type TranslationFile, type TranslationFiles, type TranslationProvider, type TranslationProviderFactory, type TranslationProviderType, type TranslationResult, type ValidationResult, addTranslationKey, addTranslationKeys, autoFillTranslations, detectExistingTranslations, ensureTranslationsStructure, extractVariables, fillNamespace, generateTranslationTypes, getAvailableLanguages, getMissingForLanguage, getNamespaces, getSupportedLanguages, getTranslationProvider, hasInterpolation, initTranslations, initTranslationsInteractive, isValidDeepLLanguage, isValidGoogleLanguage, isValidLanguage, isValidLanguageForProvider, loadConfig, manageTranslations, readTranslations, resetTranslationProvider, setTranslationProvider, sortKeys, translateBatch, translateText, validateLanguages, validateLanguagesForProvider, validateTranslations, validateVariables, writeTranslation };
475
+ export { DEEPL_LANGUAGES, DEFAULT_CONFIG, DEFAULT_LANGUAGES, type DeepLLanguage, GOOGLE_LANGUAGES, type GoogleLanguage, GoogleTranslateProvider, type LanguageFallbackResult, type ManageTranslationsOptions, type MissingTranslation, SUPPORTED_LANGUAGES, type SupportedLanguage, type SyncResult, TRANSLATION_CONFIG_SCHEMA, TRANSLATION_PROVIDERS, type TranslateOptions, type TranslationConfig, type TranslationEntry, type TranslationFile, type TranslationFiles, type TranslationProvider, type TranslationProviderFactory, type TranslationProviderType, type TranslationResult, type ValidationResult, addTranslationKey, addTranslationKeys, autoFillTranslations, createEmptyTranslationStructure, detectExistingTranslations, ensureTranslationsStructure, extractVariables, fillNamespace, generateTranslationTypes, getAvailableLanguages, getFallbackMappings, getMissingForLanguage, getNamespaces, getSupportedLanguages, getTranslationProvider, hasInterpolation, initTranslations, initTranslationsInteractive, isValidDeepLLanguage, isValidGoogleLanguage, isValidLanguage, isValidLanguageForProvider, loadConfig, logLanguageFallback, manageTranslations, readTranslations, resetTranslationProvider, resolveLanguageWithFallback, setTranslationProvider, sortKeys, syncTranslationStructure, translateBatch, translateText, validateLanguages, validateLanguagesForProvider, validateTranslations, validateVariables, writeTranslation };