@@ -15,6 +15,7 @@ import chalk from "chalk";
1515import { createTwoFilesPatch } from "diff" ;
1616import inquirer from "inquirer" ;
1717import externalEditor from "external-editor" ;
18+ import { cacheChunk , deleteCache , getNormalizedCache } from "../utils/cache" ;
1819
1920export default new Command ( )
2021 . command ( "i18n" )
@@ -90,6 +91,54 @@ export default new Command()
9091 ora . succeed ( "i18n.lock loaded" ) ;
9192 }
9293
94+ // recover cache if exists
95+ const cache = getNormalizedCache ( ) ;
96+ if ( cache ) {
97+ console . log ( ) ;
98+ ora . succeed ( `Cache loaded. Attempting recovery...` ) ;
99+ const cacheOra = Ora ( { indent : 2 } ) ;
100+
101+ for ( const bucket of buckets ) {
102+ cacheOra . info ( `Processing bucket: ${ bucket . type } ` ) ;
103+ for ( const bucketConfig of bucket . config ) {
104+ const bucketOra = ora . info ( `Processing path: ${ bucketConfig . pathPattern } ` ) ;
105+
106+ const sourceLocale = resolveOverridenLocale ( i18nConfig ! . locale . source , bucketConfig . delimiter ) ;
107+ const bucketLoader = createBucketLoader ( bucket . type , bucketConfig . pathPattern ) ;
108+ bucketLoader . setDefaultLocale ( sourceLocale ) ;
109+ await bucketLoader . init ( ) ;
110+ const sourceData = await bucketLoader . pull ( sourceLocale ) ;
111+ const cachedSourceData : Record < string , string > = { } ;
112+
113+ for ( const targetLocale in cache ) {
114+ const targetData = await bucketLoader . pull ( targetLocale ) ;
115+
116+ for ( const key in cache [ targetLocale ] ) {
117+ const { source, result } = cache [ targetLocale ] [ key ] ;
118+
119+ if ( sourceData [ key ] === source && targetData [ key ] !== result ) {
120+ targetData [ key ] = result ;
121+ cachedSourceData [ key ] = source ;
122+ }
123+ }
124+
125+ await bucketLoader . push ( targetLocale , targetData ) ;
126+ lockfileHelper . registerPartialSourceData ( bucketConfig . pathPattern , cachedSourceData ) ;
127+
128+ bucketOra . succeed (
129+ `[${ sourceLocale } -> ${ targetLocale } ] Recovered ${ Object . keys ( cachedSourceData ) . length } entries from cache` ,
130+ ) ;
131+ }
132+ }
133+ }
134+ deleteCache ( ) ;
135+ if ( flags . verbose ) {
136+ cacheOra . info ( "Cache file deleted." ) ;
137+ }
138+ } else if ( flags . verbose ) {
139+ ora . info ( "Cache file not found. Skipping recovery." ) ;
140+ }
141+
93142 if ( flags . frozen ) {
94143 ora . start ( "Checking for lockfile updates..." ) ;
95144 let requiresUpdate = false ;
@@ -174,8 +223,18 @@ export default new Command()
174223 targetLocale,
175224 targetData,
176225 } ,
177- ( progress ) => {
178- bucketOra . text = `[${ sourceLocale } -> ${ targetLocale } ] [${ Object . keys ( processableData ) . length } entries] (${ progress } %) AI localization in progress...` ;
226+ ( progress , sourceChunk , processedChunk ) => {
227+ cacheChunk ( targetLocale , sourceChunk , processedChunk ) ;
228+
229+ const progressLog = `[${ sourceLocale } -> ${ targetLocale } ] [${ Object . keys ( processableData ) . length } entries] (${ progress } %) AI localization in progress...` ;
230+ if ( flags . verbose ) {
231+ ora . info ( progressLog ) ;
232+ ora . info (
233+ `Caching chunk ${ JSON . stringify ( sourceChunk , null , 2 ) } -> ${ JSON . stringify ( processedChunk , null , 2 ) } ` ,
234+ ) ;
235+ } else {
236+ ora . text = progressLog ;
237+ }
179238 } ,
180239 ) ;
181240
@@ -236,6 +295,10 @@ export default new Command()
236295 console . log ( ) ;
237296 if ( ! hasErrors ) {
238297 ora . succeed ( "Localization completed." ) ;
298+ deleteCache ( ) ;
299+ if ( flags . verbose ) {
300+ ora . info ( "Cache file deleted." ) ;
301+ }
239302 } else {
240303 ora . warn ( "Localization completed with errors." ) ;
241304 }
@@ -296,7 +359,11 @@ function createLocalizationEngineConnection(params: { apiKey: string; apiUrl: st
296359 targetLocale : string ;
297360 targetData : Record < string , any > ;
298361 } ,
299- onProgress : ( progress : number ) => void ,
362+ onProgress : (
363+ progress : number ,
364+ sourceChunk : Record < string , string > ,
365+ processedChunk : Record < string , string > ,
366+ ) => void ,
300367 ) => {
301368 return retryWithExponentialBackoff (
302369 ( ) =>
0 commit comments