diff Lang/Translation.cs @ 21:c8d74202182a

Closes #14 - Throw specific exceptions from translations * Add TranslationLoadException * Make Translation class catch various exceptions and throw meaninful TranslationLoadExceptions * Note potential exceptions in documentation of Translation
author IBBoard <dev@ibboard.co.uk>
date Thu, 05 Mar 2009 20:34:31 +0000
parents 0352fa33ee8f
children ea058f9ea9d4
line wrap: on
line diff
--- a/Lang/Translation.cs	Sat Feb 21 14:28:07 2009 +0000
+++ b/Lang/Translation.cs	Thu Mar 05 20:34:31 2009 +0000
@@ -30,7 +30,9 @@
 		private static Dictionary<string, string> translationsDefault;
 
 		/// <summary>
-		/// Initialises the translations for the language specified and the default translations so that the Translation class can be used
+		/// Initialises the translations for the language specified and the default translations so that the Translation class can be used.
+		/// Throws a TranslationLoadException if a problem occurred while loading translations. If this occurs then the translation methods can
+		/// still be called but no translation will be performed.
 		/// </summary>
 		/// <param name="appPath">
 		/// The full path that the application is running from. Must contain the "translations" folder.
@@ -59,7 +61,7 @@
 			}
 			else
 			{
-				throw new ArgumentException("Translation path must exist ("+translationPath+")");
+				throw new TranslationLoadException("Translation path not found ("+translationPath+")");
 			}
 		}
 		
@@ -71,9 +73,25 @@
 			settings.ValidationType = ValidationType.DTD;
 			settings.ProhibitDtd = false;
 			settings.ValidationEventHandler+= new ValidationEventHandler(ValidationEventMethod);
-			XmlReader valReader = XmlReader.Create(file.FullName, settings);
-			doc.Load(valReader);
-			valReader.Close();
+			XmlReader valReader = XmlReader.Create(file.FullName, settings);
+			
+			try
+			{
+				doc.Load(valReader);
+			}
+			catch (XmlSchemaException ex)
+			{
+				throw new TranslationLoadException("Problem validating schema for translation: " + ex.Message, ex);
+			}
+			catch (XmlException ex)
+			{
+				throw new TranslationLoadException("Problem reading data for translation: " + ex.Message, ex);
+			}
+			finally
+			{
+				valReader.Close();
+			}
+			
 			return doc;
 		}
 		
@@ -83,7 +101,7 @@
 
 			if (!file.Exists)
 			{
-				throw new FileNotFoundException(language + "translation could not be found in "+translationDir.FullName);
+				throw new TranslationLoadException(language + ".translation could not be found in "+translationDir.FullName);
 			}
 			
 			return file;
@@ -118,7 +136,7 @@
 			}
 			catch(Exception ex)
 			{
-				throw new XmlParseException("Error while parsing " + GetLanguageOfDocument(doc)+" translation: "+ex.Message);
+				throw new TranslationLoadException("Error while parsing " + GetLanguageOfDocument(doc)+" translation: "+ex.Message, ex);
 			}	
 		}
 		
@@ -134,7 +152,9 @@
 		}
 
 		/// <summary>
-		/// Loads translations for a given language and sets them as the local language
+		/// Loads translations for a given language and sets them as the local language.
+		/// hrows a TranslationLoadException if a problem occurred while loading translations. If this occurs then the translation methods can
+		/// still be called but all translations will fall back to the default translation.
 		/// </summary>
 		/// <param name="translationLang">
 		/// The new local language to load
@@ -151,7 +171,7 @@
 		
 		private static void LoadTranslationForLanguage(string translationLanguage)
 		{
-			checkInitialisation();
+			CheckInitialisation();
 			
 			if (translationLanguage != DEFAULT_LANGUAGE && translationLanguage != "" && translationLanguage != null)
 			{
@@ -221,7 +241,7 @@
 		/// </returns>
 		public static string GetTranslation(string translationID, string defaultTranslation, params object[] replacements)
 		{
-			checkInitialisation();
+			CheckInitialisation();
 			string trans = GetTranslationFromTables(translationID);
 			
 			if (trans == null)
@@ -271,7 +291,7 @@
 			return translation;
 		}
 		
-		private static void checkInitialisation()
+		private static void CheckInitialisation()
 		{
 			if (translationDir==null)
 			{