@@ -259,14 +259,15 @@ void TranslateResxFiles()
259
259
} ;
260
260
261
261
IsBusy ( true ) ;
262
- new Action < string , string , TranslationOptions , List < string > , string , ResxProgressCallback , bool > ( TranslateResxFilesAsync ) . BeginInvoke (
262
+ new Action < string , string , TranslationOptions , List < string > , string , ResxProgressCallback , bool , bool > ( TranslateResxFilesAsync ) . BeginInvoke (
263
263
txtSourceResx . Text ,
264
264
srcLng ,
265
265
translationOptions ,
266
266
destLanguages ,
267
267
txtOutputDir . Text ,
268
268
ResxWorkingProgress ,
269
269
translateFromKey ,
270
+ checkBoxTranslateOnlyNew . Checked ,
270
271
( x ) => IsBusy ( false ) ,
271
272
null ) ;
272
273
}
@@ -279,7 +280,8 @@ async void TranslateResxFilesAsync(
279
280
TranslationOptions translationOptions ,
280
281
List < string > desLanguages , string destDir ,
281
282
ResxProgressCallback progress ,
282
- bool translateFromKey )
283
+ bool translateFromKey ,
284
+ bool translateOnlyNewKeys )
283
285
{
284
286
int max = 0 ;
285
287
int pos = 0 ;
@@ -298,14 +300,18 @@ async void TranslateResxFilesAsync(
298
300
doc . Load ( sourceResx ) ;
299
301
var dataList = ResxTranslator . ReadResxData ( doc ) ;
300
302
max = dataList . Count ;
301
- List < XmlNode > targetDataList = new List < XmlNode > ( ) ;
302
- bool FileExist = File . Exists ( destFile ) ;
303
- if ( checkBoxTranslateOnlyNew . Checked && FileExist )
304
- {
305
- var Targetdoc = new XmlDocument ( ) ;
306
- Targetdoc . Load ( destFile ) ;
307
303
308
- targetDataList = ResxTranslator . ReadResxData ( Targetdoc ) ;
304
+
305
+ List < XmlNode > destinationDataList = null ;
306
+ var destTranslateOnlyNewKeys =
307
+ translateOnlyNewKeys &&
308
+ File . Exists ( destFile ) ;
309
+
310
+ if ( destTranslateOnlyNewKeys )
311
+ {
312
+ var destDoc = new XmlDocument ( ) ;
313
+ destDoc . Load ( destFile ) ;
314
+ destinationDataList = ResxTranslator . ReadResxData ( destDoc ) ;
309
315
}
310
316
311
317
pos = 0 ;
@@ -314,33 +320,33 @@ async void TranslateResxFilesAsync(
314
320
315
321
try
316
322
{
317
- int indexCorrection = 0 ;
323
+ int destIndexCorrection = 0 ;
318
324
foreach ( var ( node , index ) in dataList . Select ( ( n , i ) => ( n , i ) ) )
319
325
{
320
326
status = "Translating language: " + destLng ;
321
327
pos += 1 ;
322
328
progress . BeginInvoke ( max , pos , status , null , null ) ;
323
329
var valueNode = ResxTranslator . GetDataValueNode ( node ) ;
324
- var orgText = translateFromKey ? ResxTranslator . GetDataKeyName ( node ) : valueNode . InnerText ;
325
- if ( checkBoxTranslateOnlyNew . Checked && FileExist )
330
+ if ( valueNode == null )
331
+ continue ;
332
+
333
+ var keyNode = ResxTranslator . GetDataKeyName ( node ) ;
334
+ var orgText = translateFromKey ? keyNode : valueNode . InnerText ;
335
+
336
+ if ( destTranslateOnlyNewKeys )
326
337
{
327
- int Destindex = index - indexCorrection ;
328
- if ( ResxTranslator . GetDataKeyName ( targetDataList . ElementAt ( Destindex ) ) == ResxTranslator . GetDataKeyName ( node ) )
338
+ int destIndex = index - destIndexCorrection ;
339
+ if ( ResxTranslator . GetDataKeyName ( destinationDataList . ElementAt ( destIndex ) ) == keyNode )
329
340
{
330
- valueNode . InnerText = ResxTranslator . GetDataValueNode ( targetDataList . ElementAt ( Destindex ) ) . InnerText ;
331
- indexCorrection ++ ;
341
+ valueNode . InnerText = ResxTranslator . GetDataValueNode ( destinationDataList . ElementAt ( destIndex ) ) . InnerText ;
342
+ destIndexCorrection ++ ;
332
343
continue ;
333
344
}
334
-
335
345
}
336
346
337
- if ( valueNode == null ) continue ;
338
-
339
-
340
347
if ( string . IsNullOrWhiteSpace ( orgText ) )
341
348
continue ;
342
349
343
-
344
350
if ( translationOptions . ServiceType == ServiceTypeEnum . Google )
345
351
{
346
352
// There is no longer a key to validate
@@ -364,8 +370,7 @@ async void TranslateResxFilesAsync(
364
370
365
371
if ( ! success )
366
372
{
367
- var key = ResxTranslator . GetDataKeyName ( node ) ;
368
- status = "Translating language: " + destLng + " , key '" + key + "' failed to translate in try " + trycount ;
373
+ status = "Translating language: " + destLng + " , key '" + keyNode + "' failed to translate in try " + trycount ;
369
374
progress . BeginInvoke ( max , pos , status , null , null ) ;
370
375
}
371
376
@@ -378,10 +383,9 @@ async void TranslateResxFilesAsync(
378
383
else
379
384
{
380
385
hasErrors = true ;
381
- var key = ResxTranslator . GetDataKeyName ( node ) ;
382
386
try
383
387
{
384
- string message = "\r \n Key '" + key + "' translation to language '" + destLng + "' failed." ;
388
+ string message = "\r \n Key '" + keyNode + "' translation to language '" + destLng + "' failed." ;
385
389
File . AppendAllText ( errorLogFile , message ) ;
386
390
}
387
391
catch
@@ -594,18 +598,25 @@ private void btnSelectResxSource_Click(object sender, EventArgs e)
594
598
{
595
599
txtOutputDir . Text = Path . GetDirectoryName ( txtSourceResx . Text ) ;
596
600
}
597
- string [ ] filePaths = Directory . GetFiles ( Path . GetDirectoryName ( txtSourceResx . Text ) , "*.resx" ) ;
601
+
602
+ // reset selection
598
603
foreach ( int i in lstResxLanguages . CheckedIndices )
599
604
lstResxLanguages . Items [ i ] . Checked = false ;
600
- foreach ( var file in filePaths )
605
+
606
+ // select based on what is in destination
607
+ string [ ] languageFilesInDir = Directory . GetFiles ( Path . GetDirectoryName ( txtSourceResx . Text ) , "*.resx" ) ;
608
+
609
+ foreach ( var lngFile in languageFilesInDir )
601
610
{
602
- var haslng = ReadLanguageName ( file ) ;
603
- if ( haslng != "" )
611
+ var languageTag = ReadLanguageName ( lngFile ) ;
612
+ if ( languageTag != "" )
604
613
{
605
- var haskey = _languages . FirstOrDefault ( x => string . Compare ( x . Key , haslng , StringComparison . InvariantCultureIgnoreCase ) == 0 ) ;
614
+ var haskey = _languages . FirstOrDefault ( x => x . Key . Equals ( languageTag , StringComparison . InvariantCultureIgnoreCase ) ) ;
615
+
606
616
lstResxLanguages . Items [ lstResxLanguages . Items . IndexOfKey ( haskey . Key ) ] . Checked = true ;
607
617
}
608
618
}
619
+
609
620
var lng = ReadLanguageName ( txtSourceResx . Text ) ;
610
621
var key = _languages . FirstOrDefault ( x => string . Compare ( x . Key , lng , StringComparison . InvariantCultureIgnoreCase ) == 0 ) ;
611
622
if ( key . Key != null )
0 commit comments