19 using System.Collections.Generic;
20 using Lucene.Net.Support;
22 namespace Lucene.Net.Messages
51 public AnonymousClassPrivilegedAction(System.Reflection.FieldInfo field)
55 private void InitBlock(System.Reflection.FieldInfo field)
59 private System.Reflection.FieldInfo field;
60 public virtual System.Object Run()
67 private static IDictionary<string, Type> bundles =
new HashMap<string, Type>(0);
69 protected internal NLS()
74 public static System.String GetLocalizedMessage(System.String key)
76 return GetLocalizedMessage(key, System.Threading.Thread.CurrentThread.CurrentCulture);
79 public static System.String GetLocalizedMessage(System.String key, System.Globalization.CultureInfo locale)
81 System.Object message = GetResourceBundleObject(key, locale);
84 return "Message with key:" + key +
" and locale: " + locale +
" not found.";
86 return message.ToString();
89 public static System.String GetLocalizedMessage(System.String key, System.Globalization.CultureInfo locale, params System.Object[] args)
91 System.String str = GetLocalizedMessage(key, locale);
95 str = System.String.Format(str, args);
101 public static System.String GetLocalizedMessage(System.String key, params System.Object[] args)
103 return GetLocalizedMessage(key, System.Threading.Thread.CurrentThread.CurrentCulture, args);
115 protected internal static void InitializeMessages<T>(System.String bundleName)
120 if (!bundles.ContainsKey(bundleName))
121 bundles[bundleName] = typeof(T);
123 catch (System.Exception)
130 private static System.Object GetResourceBundleObject(System.String messageKey, System.Globalization.CultureInfo locale)
135 for (var it = bundles.Keys.GetEnumerator(); it.MoveNext(); )
137 System.Type clazz = bundles[it.Current];
138 System.Threading.Thread.CurrentThread.CurrentUICulture = locale;
139 System.Resources.ResourceManager resourceBundle = System.Resources.ResourceManager.CreateFileBasedResourceManager(clazz.Name,
"Messages", null);
140 if (resourceBundle != null)
144 System.Object obj = resourceBundle.GetObject(messageKey);
148 catch (System.Resources.MissingManifestResourceException)
158 private static void Load<T>()
160 var clazz = typeof (T);
161 System.Reflection.FieldInfo[] fieldArray = clazz.GetFields(System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.DeclaredOnly | System.Reflection.BindingFlags.Static);
163 bool isFieldAccessible = clazz.IsPublic;
166 int len = fieldArray.Length;
167 var fields =
new HashMap<string, System.Reflection.FieldInfo>(len * 2);
168 for (
int i = 0; i < len; i++)
170 fields[fieldArray[i].Name] = fieldArray[i];
171 LoadfieldValue<T>(fieldArray[i], isFieldAccessible);
177 private static void LoadfieldValue<T>(System.Reflection.FieldInfo field,
bool isFieldAccessible)
179 var clazz = typeof (T);
186 if (!(field.IsPublic || field.IsStatic))
190 if (!isFieldAccessible)
191 MakeAccessible(field);
194 field.SetValue(null, field.Name);
195 ValidateMessage<T>(field.Name);
197 catch (System.ArgumentException)
201 catch (System.UnauthorizedAccessException)
209 private static void ValidateMessage<T>(System.String key)
212 var clazz = typeof (T);
215 System.Threading.Thread.CurrentThread.CurrentUICulture = System.Threading.Thread.CurrentThread.CurrentCulture;
216 System.Resources.ResourceManager resourceBundle = System.Resources.ResourceManager.CreateFileBasedResourceManager(clazz.FullName,
"", null);
217 if (resourceBundle != null)
219 System.Object obj = resourceBundle.GetObject(key);
222 System.Console.Error.WriteLine(
"WARN: Message with key:" + key +
" and locale: " + System.Threading.Thread.CurrentThread.CurrentCulture +
" not found.");
226 catch (System.Resources.MissingManifestResourceException)
228 System.Console.Error.WriteLine(
"WARN: Message with key:" + key +
" and locale: " + System.Threading.Thread.CurrentThread.CurrentCulture +
" not found.");
230 catch (System.Exception)
242 private static void MakeAccessible(System.Reflection.FieldInfo field)
244 if (System.Security.SecurityManager.SecurityEnabled)