11import { bucketTypeSchema , I18nConfig , localeCodeSchema , resolveOverriddenLocale } from "@lingo.dev/_spec" ;
2- import { LingoDotDevEngine } from "@lingo.dev/_sdk" ;
32import { Command } from "interactive-commander" ;
43import Z from "zod" ;
54import _ from "lodash" ;
@@ -17,6 +16,9 @@ import inquirer from "inquirer";
1716import externalEditor from "external-editor" ;
1817import { cacheChunk , deleteCache , getNormalizedCache } from "../utils/cache" ;
1918import updateGitignore from "../utils/update-gitignore" ;
19+ import createProcessor from "../processor" ;
20+ import { withExponentialBackoff } from "../utils/exp-backoff" ;
21+ import trackEvent from "../utils/observability" ;
2022
2123export default new Command ( )
2224 . command ( "i18n" )
@@ -60,6 +62,7 @@ export default new Command()
6062 }
6163
6264 let hasErrors = false ;
65+ let authId : string | null = null ;
6366 try {
6467 ora . start ( "Loading configuration..." ) ;
6568 const i18nConfig = getConfig ( ) ;
@@ -72,8 +75,14 @@ export default new Command()
7275
7376 ora . start ( "Connecting to Lingo.dev Localization Engine..." ) ;
7477 const auth = await validateAuth ( settings ) ;
78+ authId = auth . id ;
7579 ora . succeed ( `Authenticated as ${ auth . email } ` ) ;
7680
81+ trackEvent ( authId , "cmd.i18n.start" , {
82+ i18nConfig,
83+ flags,
84+ } ) ;
85+
7786 let buckets = getBuckets ( i18nConfig ! ) ;
7887 if ( flags . bucket ?. length ) {
7988 buckets = buckets . filter ( ( bucket : any ) => flags . bucket ! . includes ( bucket . type ) ) ;
@@ -299,11 +308,13 @@ export default new Command()
299308 bucketOra . start (
300309 `[${ sourceLocale } -> ${ targetLocale } ] [${ Object . keys ( processableData ) . length } entries] (0%) AI localization in progress...` ,
301310 ) ;
302- const localizationEngine = createLocalizationEngineConnection ( {
311+ let processPayload = createProcessor ( i18nConfig ! . provider , {
303312 apiKey : settings . auth . apiKey ,
304313 apiUrl : settings . auth . apiUrl ,
305314 } ) ;
306- const processedTargetData = await localizationEngine . process (
315+ processPayload = withExponentialBackoff ( processPayload , 3 , 1000 ) ;
316+
317+ const processedTargetData = await processPayload (
307318 {
308319 sourceLocale,
309320 sourceData,
@@ -390,11 +401,20 @@ export default new Command()
390401 if ( flags . verbose ) {
391402 ora . info ( "Cache file deleted." ) ;
392403 }
404+ trackEvent ( auth . id , "cmd.i18n.success" , {
405+ i18nConfig,
406+ flags,
407+ } ) ;
393408 } else {
394409 ora . warn ( "Localization completed with errors." ) ;
395410 }
396411 } catch ( error : any ) {
397412 ora . fail ( error . message ) ;
413+
414+ trackEvent ( authId || "unknown" , "cmd.i18n.error" , {
415+ flags,
416+ error,
417+ } ) ;
398418 process . exit ( 1 ) ;
399419 }
400420 } ) ;
@@ -435,47 +455,6 @@ async function retryWithExponentialBackoff<T>(
435455 throw new Error ( "Unreachable code" ) ;
436456}
437457
438- function createLocalizationEngineConnection ( params : { apiKey : string ; apiUrl : string ; maxRetries ?: number } ) {
439- const engine = new LingoDotDevEngine ( {
440- apiKey : params . apiKey ,
441- apiUrl : params . apiUrl ,
442- } ) ;
443-
444- return {
445- process : async (
446- args : {
447- sourceLocale : string ;
448- sourceData : Record < string , any > ;
449- processableData : Record < string , any > ;
450- targetLocale : string ;
451- targetData : Record < string , any > ;
452- } ,
453- onProgress : (
454- progress : number ,
455- sourceChunk : Record < string , string > ,
456- processedChunk : Record < string , string > ,
457- ) => void ,
458- ) => {
459- return retryWithExponentialBackoff (
460- ( ) =>
461- engine . localizeObject (
462- args . processableData ,
463- {
464- sourceLocale : args . sourceLocale ,
465- targetLocale : args . targetLocale ,
466- reference : {
467- [ args . sourceLocale ] : args . sourceData ,
468- [ args . targetLocale ] : args . targetData ,
469- } ,
470- } ,
471- onProgress ,
472- ) ,
473- params . maxRetries ?? 3 ,
474- ) ;
475- } ,
476- } ;
477- }
478-
479458function parseFlags ( options : any ) {
480459 return Z . object ( {
481460 apiKey : Z . string ( ) . optional ( ) ,
0 commit comments