forked from gitextensions/gitextensions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
FormTranslate.cs
527 lines (449 loc) · 20.1 KB
/
FormTranslate.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Windows.Forms;
using GitUI;
using ResourceManager;
using ResourceManager.Xliff;
using TranslationUtl = ResourceManager.Xliff.TranslationUtl;
namespace TranslationApp
{
public partial class FormTranslate : GitExtensionsForm
{
//TranslationStrings
readonly TranslationString translateProgressText = new TranslationString("Translated {0} out of {1}");
readonly TranslationString allText = new TranslationString("All");
readonly TranslationString saveCurrentChangesText = new TranslationString("Do you want to save the current changes?");
readonly TranslationString saveCurrentChangesCaption = new TranslationString("Save changes");
readonly TranslationString saveAsText = new TranslationString("Save as");
readonly TranslationString saveAsTextFilter = new TranslationString("Translation file (*.xlf)");
readonly TranslationString noLanguageCodeSelected = new TranslationString("There is no language code selected." +
Environment.NewLine + "Do you want to select a language code first?");
readonly TranslationString noLanguageCodeSelectedCaption = new TranslationString("Language code");
readonly TranslationString editingCellPrefixText = new TranslationString("[EDITING]");
private IDictionary<string, List<TranslationItemWithCategory>> translationItems;
readonly IDictionary<string, TranslationFile> _neutralTranslation = new Dictionary<string, TranslationFile>();
IDictionary<string, TranslationFile> _translation = new Dictionary<string, TranslationFile>();
private readonly TranslationCategory _allCategories = new TranslationCategory();
private bool _changesMade;
public FormTranslate()
: base(true)
{
InitializeComponent(); Translate();
}
private void FormTranslate_Load(object sender, EventArgs e)
{
translations.Items.Clear();
translations.Sorted = true;
translations.Items.AddRange(Translator.GetAllTranslations());
FillNeutralTranslation();
_allCategories.Name = allText.Text;
UpdateCategoriesList();
translations.SelectedItem = GitCommands.AppSettings.Translation; // should be called after FillNeutralTranslation()
if (_translation == null)
LoadTranslation();
translateCategories.SelectedItem = _allCategories;
FillTranslateGrid(_allCategories);
foreach (CultureInfo cultureInfo in CultureInfo.GetCultures(CultureTypes.AllCultures))
{
if (!_NO_TRANSLATE_languageCode.Items.Contains(cultureInfo.TwoLetterISOLanguageName))
{
_NO_TRANSLATE_languageCode.Items.Add(string.Concat(cultureInfo.TwoLetterISOLanguageName, " (", cultureInfo.DisplayName, ")"));
}
}
FormClosing += FormTranslate_FormClosing;
}
void FormTranslate_FormClosing(object sender, FormClosingEventArgs e)
{
AskForSave();
}
private void UpdateProgress()
{
int translatedCount = translationItems.Sum(p => p.Value.Count(translateItem => !string.IsNullOrEmpty(translateItem.TranslatedValue)));
int totalCount = translationItems.Count();
var progresMsg = string.Format(translateProgressText.Text, translatedCount, totalCount);
if (translateProgress.Text != progresMsg)
{
translateProgress.Text = progresMsg;
toolStrip1.Refresh();
}
}
private void LoadTranslation()
{
if (_translation.Count != 0)
{
var items = TranslationHelpers.GetItemsDictionary(_translation);
translationItems = TranslationHelpers.LoadTranslation(_translation, items);
}
else
{
var neutralItems = new Dictionary<string, List<TranslationItemWithCategory>>();
foreach (var pair in _neutralTranslation)
{
var list = from item in pair.Value.TranslationCategories
from translationItem in item.Body.TranslationItems
select new TranslationItemWithCategory(item.Name, translationItem.Clone());
neutralItems.Add(pair.Key, list.ToList());
}
translationItems = neutralItems;
}
UpdateProgress();
}
private void FillTranslateGrid(TranslationCategory filter)
{
if (translationItems == null)
return;
translateItemBindingSource.DataSource = null;
if (filter == _allCategories)
filter = null;
translateItemBindingSource.DataSource = GetCategoryItems(filter).ToList();
UpdateProgress();
}
private IEnumerable<TranslationItemWithCategory> GetCategoryItems(TranslationCategory filter)
{
var filteredByCategory = translationItems.SelectMany(p => p.Value).Where(
translateItem => filter == null || filter.Name.Equals(translateItem.Category));
var filteredItems = filteredByCategory.Where(
translateItem => !hideTranslatedItems.Checked);
return filteredItems;
}
private IEnumerable<TranslationCategory> GetCategories(IDictionary<string, TranslationFile> translation)
{
return translation.SelectMany(pair => pair.Value.TranslationCategories);
}
public void UpdateCategoriesList()
{
var tc = translateCategories.SelectedItem as TranslationCategory;
translateCategories.Items.Clear();
translateCategories.Items.Add(_allCategories);
if (!hideTranslatedItems.Checked)
translateCategories.Items.AddRange(GetCategories(_neutralTranslation).ToArray());
else
{
var categories = GetCategories(_neutralTranslation).Where(cat => GetCategoryItems(cat).Any());
translateCategories.Items.AddRange(categories.ToArray());
}
if (hideTranslatedItems.Checked && !GetCategoryItems(tc).Any())
tc = _allCategories;
translateCategories.SelectedItem = tc;
}
private void FillNeutralTranslation()
{
try
{
//Set language to neutral to get neutral translations
GitCommands.AppSettings.CurrentTranslation = "";
var translatableTypes = TranslationUtl.GetTranslatableTypes();
progressBar.Maximum = translatableTypes.Sum(types => types.Value.Count);
progressBar.Visible = true;
int index = 0;
foreach (var types in translatableTypes)
{
var translation = new TranslationFile();
try
{
foreach (Type type in types.Value)
{
ITranslate obj = TranslationUtl.CreateInstanceOfClass(type) as ITranslate;
if (obj != null)
obj.AddTranslationItems(translation);
progressBar.Value = index;
index++;
if (index % 10 == 0)
Update();
}
}
finally
{
translation.Sort();
_neutralTranslation[types.Key] = translation;
}
}
}
finally
{
//Restore translation
GitCommands.AppSettings.CurrentTranslation = null;
progressBar.Visible = false;
}
}
private void translateCategories_SelectedIndexChanged(object sender, EventArgs e)
{
categoryDataGridViewTextBoxColumn.Visible = (translateCategories.SelectedItem == _allCategories);
FillTranslateGrid(translateCategories.SelectedItem as TranslationCategory);
}
private void saveAs_Click(object sender, EventArgs e)
{
if (string.IsNullOrEmpty(_NO_TRANSLATE_languageCode.Text))
if (MessageBox.Show(this, noLanguageCodeSelected.Text, noLanguageCodeSelectedCaption.Text, MessageBoxButtons.YesNo) == DialogResult.Yes)
return;
toolStrip1.Select();
SaveAs();
}
#region Move translations
/*
//methods used to move translations from FormSettings to new settings pages
private void MoveTranslations()
{
string[] translationsNames = Translator.GetAllTranslations();
foreach (string translationName in translationsNames)
{
translation = Translator.GetTranslation(translationName);
if (translation == null)
continue;
LoadTranslation();
MoveTranslationItems("FormSettings", "AppearanceSettingsPage");
MoveTranslationItems("FormSettings", "ChecklistSettingsPage");
MoveTranslationItems("FormSettings", "ColorsSettingsPage");
MoveTranslationItems("FormSettings", "GitExtensionsSettingsPage");
MoveTranslationItems("FormSettings", "GitSettingsPage");
MoveTranslationItems("FormSettings", "GlobalSettingsSettingsPage");
MoveTranslationItems("FormSettings", "HotkeysSettingsPage");
MoveTranslationItems("FormSettings", "LocalSettingsSettingsPage");
MoveTranslationItems("FormSettings", "ScriptsSettingsPage");
MoveTranslationItems("FormSettings", "ShellExtensionSettingsPage");
MoveTranslationItems("FormSettings", "SshSettingsPage");
MoveTranslationItems("FormSettings", "StartPageSettingsPage");
MoveTranslationItems("FormSettings", "CommonLogic");
MoveTranslationItems("FormSettings", "CheckSettingsLogic");
SaveAs();
}
}
private void MoveTranslationItems(string fromCategoryName, string toCategoryName)
{
TranslationCategory fromCategory = translation.GetTranslationCategory(fromCategoryName);
if (fromCategory == null)
return;
Dictionary<string, TranslationItem> exactMatch = new Dictionary<string, TranslationItem>();
Dictionary<string, TranslationItem> byNeutralValueMatch = new Dictionary<string, TranslationItem>();
Dictionary<string, TranslationItem> ambiguous = new Dictionary<string, TranslationItem>();
foreach (var item in fromCategory.TranslationItems)
{
if (!item.Value.IsNullOrEmpty())
{
string neutralValue = item.Source;
if (neutralValue != null)
{
string neutralValueKey = neutralValue + ":" + item.Property;
TranslationItem foo;
if (byNeutralValueMatch.TryGetValue(neutralValueKey, out foo))
ambiguous[neutralValueKey] = item;
else
byNeutralValueMatch.Add(neutralValueKey, item);
var exactKey = item.Name + ":" + neutralValueKey;
exactMatch.Add(exactKey, item);
}
}
}
foreach (var key in ambiguous.Keys)
byNeutralValueMatch.Remove(key);
foreach (var item in translate.Where(itm => itm.Category.Equals(toCategoryName)))
{
if (item.Status != TranslationType.Translated)
{
string neutralValueKey = item.NeutralValue + ":" + item.Property;
string exactKey = item.Name + ":" + neutralValueKey;
TranslationItem fromItem;
if (exactMatch.TryGetValue(exactKey, out fromItem))
{
item.TranslatedValue = fromItem.Value;
item.Status = fromItem.Status;
}
else
{
if (byNeutralValueMatch.TryGetValue(neutralValueKey, out fromItem))
{
item.TranslatedValue = fromItem.Value;
item.Status = fromItem.Status;
}
}
}
}
}
*/
#endregion
private string GetSelectedLanguageCode()
{
if (string.IsNullOrEmpty(_NO_TRANSLATE_languageCode.Text) || _NO_TRANSLATE_languageCode.Text.Length < 2)
return null;
return _NO_TRANSLATE_languageCode.Text.Substring(0, 2);
}
private void SaveAs()
{
using (var fileDialog =
new SaveFileDialog
{
Title = saveAsText.Text,
FileName = translations.Text + ".xlf",
Filter = saveAsTextFilter.Text + "|*.xlf",
DefaultExt = ".xlf",
AddExtension = true
})
{
if (fileDialog.ShowDialog(this) == DialogResult.OK)
{
TranslationHelpers.SaveTranslation(GetSelectedLanguageCode(), translationItems, fileDialog.FileName);
_changesMade = false;
}
}
}
private void translations_SelectedIndexChanged(object sender, EventArgs e)
{
AskForSave();
_changesMade = false;
_translation = Translator.GetTranslation(translations.Text);
LoadTranslation();
UpdateCategoriesList();
FillTranslateGrid(translateCategories.SelectedItem as TranslationCategory);
if (_translation == null)
{
_NO_TRANSLATE_languageCode.Text = "";
return;
}
var languageCode = _translation.First().Value.TargetLanguage;
try
{
var culture = new CultureInfo(languageCode);
_NO_TRANSLATE_languageCode.Text = string.Concat(culture.TwoLetterISOLanguageName, " (", culture.DisplayName, ")");
}
catch
{
_NO_TRANSLATE_languageCode.Text = languageCode;
}
}
private void hideTranslatedItems_CheckedChanged(object sender, EventArgs e)
{
UpdateCategoriesList();
FillTranslateGrid(translateCategories.SelectedItem as TranslationCategory);
}
private void AskForSave()
{
if (_changesMade)
{
if (MessageBox.Show(this, saveCurrentChangesText.Text, saveCurrentChangesCaption.Text, MessageBoxButtons.YesNo) == DialogResult.Yes)
{
SaveAs();
}
}
}
private void translateGrid_CellEndEdit(object sender, DataGridViewCellEventArgs e)
{
_changesMade = true;
UpdateProgress();
}
private void toolStripButton1_CheckedChanged(object sender, EventArgs e)
{
splitContainer2.Panel2Collapsed = _toolStripButton1.Checked;
}
private void translatedText_TextChanged(object sender, EventArgs e)
{
if (translateGrid.SelectedRows.Count == 1)
{
_changesMade = true;
UpdateProgress();
}
}
TranslationItemWithCategory _translationItemWithCategoryInEditing;
private void translatedText_Enter(object sender, System.EventArgs e)
{
if (_translationItemWithCategoryInEditing != null)
{
_translationItemWithCategoryInEditing.TranslatedValue = translatedText.Text;
}
_translationItemWithCategoryInEditing = (TranslationItemWithCategory)translateItemBindingSource.Current;
if (_translationItemWithCategoryInEditing != null)
{
_translationItemWithCategoryInEditing.TranslatedValue = editingCellPrefixText.Text + " " + _translationItemWithCategoryInEditing.TranslatedValue;
}
}
private void translatedText_Leave(object sender, System.EventArgs e)
{
//Debug.Assert(_translationItemWithCategoryInEditing != null);
if (_translationItemWithCategoryInEditing != null)
{
_translationItemWithCategoryInEditing.TranslatedValue = translatedText.Text;
_translationItemWithCategoryInEditing = null;
}
}
private void translateGrid_Click(object sender, EventArgs e)
{
if (translateGrid.SelectedRows.Count == 1)
{
if (_toolStripButton1.Checked)
{
splitContainer2.Panel2Collapsed = false;
}
var translateItem = (TranslationItemWithCategory)translateGrid.SelectedRows[0].DataBoundItem;
if (translateItem == null) return;
neutralText.Text = translateItem.NeutralValue;
translatedText.Text = translateItem.TranslatedValue;
}
else
{
splitContainer2.Panel2Collapsed = true;
}
}
private void translateGrid_SelectionChanged(object sender, EventArgs e)
{
bool nowTranslatedTextInEditing = _translationItemWithCategoryInEditing != null;
translatedText_Leave(null, null);
translateGrid_Click(null, null);
if (nowTranslatedTextInEditing)
{
translatedText_Enter(null, null);
}
}
private void nextButton_Click(object sender, EventArgs e)
{
if (translateGrid.SelectedRows.Count == 1)
{
if (translateGrid.CurrentCell.RowIndex < translateGrid.Rows.Count - 1)
translateItemBindingSource.MoveNext();
}
else
if (translateGrid.Rows.Count > 0)
{
translateGrid.Rows[0].Selected = true;
}
}
private void previousButton_Click(object sender, EventArgs e)
{
if (translateGrid.SelectedRows.Count == 1)
{
if (translateGrid.CurrentCell.RowIndex > 0)
translateItemBindingSource.MovePrevious();
}
else
if (translateGrid.Rows.Count > 0)
{
translateGrid.Rows[0].Selected = true;
}
}
private void toolStripButtonNew_Click(object sender, EventArgs e)
{
translations.Text = "";
translations_SelectedIndexChanged(null, null);
}
private void translatedText_KeyDown(object sender, KeyEventArgs e)
{
if (e.Alt && e.KeyCode == Keys.Up)
{
e.Handled = true;
previousButton_Click(sender, e);
}
else if (e.Alt && e.KeyCode == Keys.Down ||
e.Control && e.KeyCode == Keys.Enter)
{
e.Handled = true;
nextButton_Click(sender, e);
}
else if (e.Control && e.KeyCode == Keys.Down)
{
e.Handled = true;
translatedText.SelectAll();
translatedText.SelectedText = neutralText.Text;
}
}
}
}