forked from gitextensions/gitextensions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
TranslationUtl.cs
211 lines (188 loc) · 8.91 KB
/
TranslationUtl.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
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Reflection;
using System.Windows.Forms;
namespace ResourceManager.Translation
{
public static class TranslationUtl
{
private static bool AllowTranslateProperty(string text)
{
if (text == null)
return false;
foreach (char c in text)
if (Char.IsLetter(c))
return true;
return false;
}
public static IEnumerable<Tuple<string, object>> GetObjProperties(object obj, string objName)
{
if (objName != null)
yield return new Tuple<string, object>(objName, obj);
foreach (FieldInfo fieldInfo in obj.GetType().GetFields(BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Static | BindingFlags.SetField))
{
yield return new Tuple<string, object>(fieldInfo.Name, fieldInfo.GetValue(obj));
}
}
public static void AddTranslationItemsFromFields(string category, object obj, Translation translation)
{
if (obj == null)
return;
AddTranslationItemsFromList(category, translation, GetObjProperties(obj, "$this"));
}
public static void AddTranslationItemsFromList(string category, Translation translation, IEnumerable<Tuple<string, object>> items)
{
Action<string, object, PropertyInfo> action = delegate(string item, object itemObj, PropertyInfo propertyInfo)
{
var value = (string)propertyInfo.GetValue(itemObj, null);
if (AllowTranslateProperty(value))
translation.AddTranslationItem(category, item, propertyInfo.Name, value);
};
ForEachItem(items, action);
}
public static void ForEachItem(IEnumerable<Tuple<string, object>> items, Action<string, object, PropertyInfo> action)
{
foreach (var item in items)
{
string itemName = item.Item1;
object itemObj = item.Item2;
//Skip controls with a name started with "_NO_TRANSLATE_"
//this is a naming convention, these are not translated
if (itemName.StartsWith("_NO_TRANSLATE_"))
continue;
Func<PropertyInfo, bool> IsTranslatableItem = null;
if (itemObj is DataGridViewColumn)
{
DataGridViewColumn c = itemObj as DataGridViewColumn;
IsTranslatableItem = delegate(PropertyInfo propertyInfo)
{
return IsTranslatableItemInDataGridViewColumn(propertyInfo, c);
};
}
else
{
IsTranslatableItem = IsTranslatableItemInComponent;
}
if (IsTranslatableItem != null)
{
Action<PropertyInfo> paction = delegate(PropertyInfo propertyInfo)
{
action(itemName, itemObj, propertyInfo);
};
ForEachProperty(itemObj, paction, IsTranslatableItem);
}
}
}
public static void TranslateItemsFromList(string category, Translation translation, IEnumerable<Tuple<string, object>> items)
{
Action<string, object, PropertyInfo> action = delegate(string item, object itemObj, PropertyInfo propertyInfo)
{
string value = translation.TranslateItem(category, item, propertyInfo.Name, null);
if (!String.IsNullOrEmpty(value))
{
if (propertyInfo.CanWrite)
propertyInfo.SetValue(itemObj, value, null);
}
else if (propertyInfo.Name == "ToolTipText" && !String.IsNullOrEmpty((string)propertyInfo.GetValue(itemObj, null)))
{
value = translation.TranslateItem(category, item, "Text", null);
if (!String.IsNullOrEmpty(value))
{
if (propertyInfo.CanWrite)
propertyInfo.SetValue(itemObj, value, null);
}
}
};
ForEachItem(items, action);
}
public static void TranslateItemsFromFields(string category, object obj, Translation translation)
{
if (obj == null)
return;
TranslateItemsFromList(category, translation, GetObjProperties(obj, "$this"));
}
public static void ForEachProperty(object obj, Action<PropertyInfo> action, Func<PropertyInfo, bool> IsTranslatableItem)
{
if (obj == null)
return;
foreach (PropertyInfo propertyInfo in obj.GetType().GetProperties(BindingFlags.Public | BindingFlags.Instance | BindingFlags.Static | BindingFlags.NonPublic | BindingFlags.SetProperty))
if (IsTranslatableItem(propertyInfo))
action(propertyInfo);
}
public static bool IsTranslatableItemInComponent(PropertyInfo propertyInfo)
{
if (propertyInfo.PropertyType != typeof(string))
return false;
if (propertyInfo.Name.Equals("Caption", StringComparison.CurrentCulture))
return true;
if (propertyInfo.Name.Equals("Text", StringComparison.CurrentCulture))
return true;
if (propertyInfo.Name.Equals("ToolTipText", StringComparison.CurrentCulture))
return true;
if (propertyInfo.Name.Equals("Title", StringComparison.CurrentCulture))
return true;
return false;
}
public static bool IsTranslatableItemInDataGridViewColumn(PropertyInfo propertyInfo, DataGridViewColumn viewCol)
{
return propertyInfo.Name.Equals("HeaderText", StringComparison.CurrentCulture) && viewCol.Visible;
}
public static bool IsAssemblyTranslatable(Assembly assembly)
{
if ((assembly.FullName.StartsWith("mscorlib", StringComparison.OrdinalIgnoreCase)) ||
(assembly.FullName.StartsWith("Microsoft", StringComparison.OrdinalIgnoreCase)) ||
(assembly.FullName.StartsWith("Presentation", StringComparison.OrdinalIgnoreCase)) ||
(assembly.FullName.StartsWith("WindowsBase", StringComparison.OrdinalIgnoreCase)) ||
(assembly.FullName.StartsWith("ICSharpCode", StringComparison.OrdinalIgnoreCase)) ||
(assembly.FullName.StartsWith("access", StringComparison.OrdinalIgnoreCase)) ||
(assembly.FullName.StartsWith("SMDiag", StringComparison.OrdinalIgnoreCase)) ||
(assembly.FullName.StartsWith("System", StringComparison.OrdinalIgnoreCase)) ||
(assembly.FullName.StartsWith("vshost", StringComparison.OrdinalIgnoreCase)))
{
return false;
}
return true;
}
public static List<Type> GetTranslatableTypes()
{
List<Type> translatableTypes = new List<Type>();
foreach (Assembly assembly in AppDomain.CurrentDomain.GetAssemblies())
{
if (IsAssemblyTranslatable(assembly))
{
foreach (Type type in assembly.GetTypes())
{
//TODO: Check if class contain TranslationString but doesn't implement ITranslate
if (type.IsClass && typeof(ITranslate).IsAssignableFrom(type) && !type.IsAbstract)
{
translatableTypes.Add(type);
}
}
}
}
return translatableTypes;
}
public static object CreateInstanceOfClass(Type type)
{
BindingFlags flags = BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance;
object obj = null;
// try to find parameter less constructor first
foreach (ConstructorInfo constructor in type.GetConstructors(flags))
{
if (constructor.GetParameters().Length == 0)
obj = Activator.CreateInstance(type, true);
}
if (obj == null && type.GetConstructors().Length > 0)
{
ConstructorInfo parameterConstructor = type.GetConstructors(flags)[0];
var parameters = new List<object>(parameterConstructor.GetParameters().Length);
for (int i = 0; i < parameterConstructor.GetParameters().Length; i++)
parameters.Add(null);
obj = parameterConstructor.Invoke(parameters.ToArray());
}
Debug.Assert(obj != null);
return obj;
}
}
}