private void LoadLocales()
{
lstTextVariables = new List<TextVariables>();
if (App.Current.Resources["TextVariables"] == null)
{
MemberInfo[] theMembers = new TextVariables().GetType().GetProperties();
//how many locales do we have?
string[] arFiles = Directory.GetFiles("lang", "*.xml");
//load each one
foreach (string strLocaleFile in arFiles)
{
string strErrorMessage = "";
try
{
XElement theLanguage = XElement.Load(strLocaleFile);
TextVariables theVariables = new TextVariables();
foreach (MemberInfo theMemberInfo in theMembers)
{
try
{
//collect it from the data first
//most of them will be here
theVariables.GetType().GetProperty(theMemberInfo.Name).SetValue(theVariables, theLanguage.Element(theMemberInfo.Name).Value, null);
}
catch (Exception ex)
{
//some are also attributes though
try
{
theVariables.GetType().GetProperty(theMemberInfo.Name).SetValue(theVariables, theLanguage.Attribute(theMemberInfo.Name).Value, null);
}
catch (Exception exGeneral)
{
strErrorMessage += theMemberInfo.Name + "not found" + Environment.NewLine;
}
}
}
if (string.IsNullOrEmpty(strErrorMessage))
{
lstTextVariables.Add(theVariables);
}
else
{
MessageBox.Show("Error loading " + Path.GetFileName(strLocaleFile) + Environment.NewLine + strErrorMessage);
}
}
catch (Exception ex)
{
MessageBox.Show("Error loading " + strLocaleFile + "\r\n" + ex.Message);
}
}
if (App.Current.Resources["TextVariables"] == null)
{
App.Current.Resources.Add("TextVariables", lstTextVariables);
}
else
{
App.Current.Resources["TextVariables"] = lstTextVariables;
}
}
else
{
lstTextVariables = App.Current.Resources["TextVariables"] as List<TextVariables>;
}
//and load the required one
var currentLocales = lstTextVariables.Where(loc => loc.Locale.Equals(_CurrentLocale, StringComparison.InvariantCultureIgnoreCase));
switch (currentLocales.Count())
{
case 0:
CurrentLocale = new TextVariables();
break;
case 1:
CurrentLocale = currentLocales.First();
break;
default:
MessageBox.Show("Multiple files found for " + _CurrentLocale);
CurrentLocale = currentLocales.First();
break;
}
//and record the amount that were loaded
NumberLoaded = lstTextVariables.Count;
}