@@ -314,20 +314,29 @@ class Mandarin {
314
314
// get the translation results from Google
315
315
if ( ! _ . isString ( translation ) ) {
316
316
debug ( 'getting translation' , key ) ;
317
- [ translation ] = await this . client . translate ( phrase , locale ) ;
318
- debug ( 'got translation' , translation ) ;
319
- if ( this . redisClient ) await this . redisClient . set ( key , translation ) ;
317
+ try {
318
+ [ translation ] = await this . client . translate ( phrase , locale ) ;
319
+ } catch ( err ) {
320
+ debug ( 'error' , err , 'key' , key , 'phrase' , phrase , 'locale' , locale ) ;
321
+ }
322
+
323
+ if ( _ . isString ( translation ) ) {
324
+ debug ( 'got translation' , translation ) ;
325
+ if ( this . redisClient ) await this . redisClient . set ( key , translation ) ;
326
+ }
320
327
}
321
328
322
329
// replace `|` pipe character because translation will
323
330
// interpret as ranged interval
324
331
// <https://github.com/mashpie/i18n-node/issues/274>
325
332
// TODO: maybe use `he` package to re-encode entities?
326
- file [ phrase ] = translation . replace ( / \| / g, '|' ) ;
333
+ if ( _ . isString ( translation ) ) {
334
+ file [ phrase ] = translation . replace ( / \| / g, '|' ) ;
327
335
328
- // write the file again
329
- debug ( 'writing filePath' , filePath , 'with translation' , translation ) ;
330
- await writeFile ( filePath , JSON . stringify ( file , null , 2 ) ) ;
336
+ // write the file again
337
+ debug ( 'writing filePath' , filePath , 'with translation' , translation ) ;
338
+ await writeFile ( filePath , JSON . stringify ( file , null , 2 ) ) ;
339
+ }
331
340
} ) ;
332
341
333
342
return file ;
0 commit comments