@@ -11,12 +11,12 @@ import fs from "fs/promises";
1111import path from "path" ;
1212import type { LingoConfig , MetadataSchema } from "../types" ;
1313import { logger } from "../utils/logger" ;
14- import { getCachePath } from "../utils/path-helpers" ;
1514import {
1615 startTranslationServer ,
1716 type TranslationServer ,
1817} from "../translation-server" ;
1918import { loadMetadata } from "../metadata/manager" ;
19+ import { LocalTranslationCache , type TranslationCache } from "../translators" ;
2020
2121export interface BuildTranslationOptions {
2222 config : LingoConfig ;
@@ -82,14 +82,19 @@ export async function processBuildTranslations(
8282 const totalEntries = Object . keys ( metadata . entries ) . length ;
8383 logger . info ( `📊 Found ${ totalEntries } translatable entries` ) ;
8484
85+ const cache = new LocalTranslationCache (
86+ { cacheDir : config . lingoDir } ,
87+ logger ,
88+ ) ;
89+
8590 // Handle cache-only mode
8691 if ( buildMode === "cache-only" ) {
8792 logger . info ( "🔍 Validating translation cache..." ) ;
88- await validateCache ( config , metadata ) ;
93+ await validateCache ( config , metadata , cache ) ;
8994 logger . info ( "✅ Cache validation passed" ) ;
9095
9196 if ( publicOutputPath ) {
92- await copyStaticFiles ( config , publicOutputPath , metadata ) ;
97+ await copyStaticFiles ( config , publicOutputPath , metadata , cache ) ;
9398 }
9499
95100 return {
@@ -161,7 +166,7 @@ export async function processBuildTranslations(
161166
162167 // Copy cache to public directory if requested
163168 if ( publicOutputPath ) {
164- await copyStaticFiles ( config , publicOutputPath , metadata ) ;
169+ await copyStaticFiles ( config , publicOutputPath , metadata , cache ) ;
165170 }
166171
167172 logger . info ( "✅ Translation generation completed successfully" ) ;
@@ -188,6 +193,7 @@ export async function processBuildTranslations(
188193async function validateCache (
189194 config : LingoConfig ,
190195 metadata : MetadataSchema ,
196+ cache : TranslationCache ,
191197) : Promise < void > {
192198 const allHashes = Object . keys ( metadata . entries ) ;
193199 const missingLocales : string [ ] = [ ] ;
@@ -204,13 +210,16 @@ async function validateCache(
204210 : config . targetLocales ;
205211
206212 for ( const locale of allLocales ) {
207- const cacheFilePath = getCachePath ( config , locale ) ;
208-
209213 try {
210- const cacheContent = await fs . readFile ( cacheFilePath , "utf-8" ) ;
211- const cache = JSON . parse ( cacheContent ) . entries as Record < string , string > ;
214+ const entries = await cache . get ( locale ) ;
212215
213- const missingHashes = allHashes . filter ( ( hash ) => ! cache [ hash ] ) ;
216+ if ( Object . keys ( entries ) . length === 0 ) {
217+ missingLocales . push ( locale ) ;
218+ logger . debug ( `Cache file not found or empty for ${ locale } ` ) ;
219+ continue ;
220+ }
221+
222+ const missingHashes = allHashes . filter ( ( hash ) => ! entries [ hash ] ) ;
214223
215224 if ( missingHashes . length > 0 ) {
216225 incompleteLocales . push ( {
@@ -228,7 +237,7 @@ async function validateCache(
228237 }
229238 } catch ( error ) {
230239 missingLocales . push ( locale ) ;
231- logger . debug ( `Cache file not found for ${ locale } : ${ cacheFilePath } ` ) ;
240+ logger . debug ( `Failed to read cache for ${ locale } :` , error ) ;
232241 }
233242 }
234243
@@ -270,6 +279,7 @@ async function copyStaticFiles(
270279 config : LingoConfig ,
271280 publicOutputPath : string ,
272281 metadata : MetadataSchema ,
282+ cache : TranslationCache ,
273283) : Promise < void > {
274284 logger . info ( `📦 Generating static translation files in ${ publicOutputPath } ` ) ;
275285
@@ -285,20 +295,24 @@ async function copyStaticFiles(
285295 : config . targetLocales ;
286296
287297 for ( const locale of allLocales ) {
288- const cacheFilePath = getCachePath ( config , locale ) ;
289298 const publicFilePath = path . join ( publicOutputPath , `${ locale } .json` ) ;
290299
291300 try {
292- const cacheContent = await fs . readFile ( cacheFilePath , "utf-8" ) ;
293- const cache = JSON . parse ( cacheContent ) ;
301+ // Use getDictionary if available (for LocalTranslationCache), otherwise use get
302+ const dictionary = cache . getDictionary
303+ ? await cache . getDictionary ( locale )
304+ : null ;
305+
306+ if ( ! dictionary ) {
307+ logger . error ( `❌ Failed to read cache for ${ locale } ` ) ;
308+ process . exit ( 1 ) ;
309+ }
294310
295311 const filteredEntries : Record < string , string > = { } ;
296312 let includedCount = 0 ;
297313 let skippedCount = 0 ;
298314
299- for ( const [ hash , translation ] of Object . entries (
300- cache . entries as Record < string , string > ,
301- ) ) {
315+ for ( const [ hash , translation ] of Object . entries ( dictionary . entries ) ) {
302316 if ( usedHashes . has ( hash ) ) {
303317 filteredEntries [ hash ] = translation ;
304318 includedCount ++ ;
@@ -311,7 +325,7 @@ async function copyStaticFiles(
311325 const outputData = {
312326 locale,
313327 entries : filteredEntries ,
314- version : cache . version ,
328+ version : dictionary . version ,
315329 } ;
316330
317331 await fs . writeFile (
0 commit comments