forked from gitextensions/gitextensions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTranslationHelpers.cs
127 lines (111 loc) · 5.35 KB
/
TranslationHelpers.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
using System;
using System.Collections.Generic;
using System.Linq;
using ResourceManager;
using ResourceManager.Xliff;
using TranslationUtl = ResourceManager.Xliff.TranslationUtl;
using Xliff = ResourceManager.Xliff;
namespace TranslationApp
{
static class TranslationHelpers
{
public static IList<TranslationItemWithCategory> LoadNeutralItems()
{
Translation neutralTranslation = new Translation();
try
{
//Set language to neutral to get neutral translations
GitCommands.AppSettings.CurrentTranslation = "";
List<Type> translatableTypes = TranslationUtl.GetTranslatableTypes();
foreach (Type type in translatableTypes)
{
ITranslate obj = TranslationUtl.CreateInstanceOfClass(type) as ITranslate;
if (obj != null)
obj.AddTranslationItems(neutralTranslation);
IDisposable disposable = obj as IDisposable;
if (disposable != null)
disposable.Dispose();
}
}
finally
{
neutralTranslation.Sort();
//Restore translation
GitCommands.AppSettings.CurrentTranslation = null;
}
IList<TranslationItemWithCategory> neutralItems =
(from translationCategory in neutralTranslation.TranslationCategories
from translationItem in translationCategory.Body.TranslationItems
select new TranslationItemWithCategory(translationCategory.Name, translationItem)).ToList();
return neutralItems;
}
public static List<TranslationItemWithCategory> LoadTranslation(Translation translation, IEnumerable<TranslationItemWithCategory> neutralItems)
{
List<TranslationItemWithCategory> translateItems = new List<TranslationItemWithCategory>();
var oldTranslationItems =
(from translationCategory in translation.TranslationCategories
from translationItem in translationCategory.Body.TranslationItems
select new TranslationItemWithCategory(translationCategory.Name, translationItem)).ToList();
var dict = new Dictionary<string, string>();
foreach (var item in neutralItems)
{
var curItems = oldTranslationItems.Where(
trItem => trItem.Category.TrimStart('_') == item.Category.TrimStart('_') &&
trItem.Name.TrimStart('_') == item.Name.TrimStart('_') &&
trItem.Property == item.Property);
var curItem = curItems.FirstOrDefault();
if (curItem == null)
{
curItem = item.Clone();
translateItems.Add(curItem);
continue;
}
oldTranslationItems.Remove(curItem);
curItem.Category = item.Category;
curItem.Name = item.Name;
string source = curItem.NeutralValue ?? item.NeutralValue;
if (!String.IsNullOrEmpty(curItem.TranslatedValue) && !dict.ContainsKey(source))
dict.Add(source, curItem.TranslatedValue);
// Source text changed
if (!curItem.IsSourceEqual(item.NeutralValue) &&
(!String.IsNullOrEmpty(curItem.TranslatedValue) && !curItem.IsSourceEqual(item.NeutralValue)))
{
curItem.TranslatedValue = "";
}
curItem.NeutralValue = item.NeutralValue;
translateItems.Add(curItem);
}
foreach (var item in oldTranslationItems)
{
// Obsolete should be added only to dictionary
if (!String.IsNullOrEmpty(item.TranslatedValue) &&
item.NeutralValue != null && !dict.ContainsKey(item.NeutralValue))
{
dict.Add(item.NeutralValue, item.TranslatedValue);
}
}
// update untranslated items
var untranlatedItems =
from trItem in translateItems
where (String.IsNullOrEmpty(trItem.TranslatedValue)) && dict.ContainsKey(trItem.NeutralValue)
select trItem;
foreach (var untranlatedItem in untranlatedItems)
{
untranlatedItem.TranslatedValue = dict[untranlatedItem.NeutralValue];
}
return translateItems;
}
public static void SaveTranslation(string languageCode, IEnumerable<TranslationItemWithCategory> items, string filename)
{
var foreignTranslation = new Translation(GitCommands.AppSettings.ProductVersion, languageCode);
foreach (TranslationItemWithCategory translateItem in items)
{
var item = translateItem.GetTranslationItem();
var ti = new TranslationItem(item.Name, item.Property, item.Source, item.Value);
ti.Value = ti.Value ?? String.Empty;
foreignTranslation.FindOrAddTranslationCategory(translateItem.Category).Body.AddTranslationItem(ti);
}
TranslationSerializer.Serialize(foreignTranslation, filename);
}
}
}