changeset 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 855b4903b256
children ea058f9ea9d4
files IBBoard.csproj Lang/Translation.cs Lang/TranslationLoadException.cs
diffstat 3 files changed, 56 insertions(+), 11 deletions(-) [+]
line wrap: on
line diff
--- a/IBBoard.csproj	Sat Feb 21 14:28:07 2009 +0000
+++ b/IBBoard.csproj	Thu Mar 05 20:34:31 2009 +0000
@@ -126,6 +126,7 @@
     <Compile Include="Xml\XmlParseException.cs" />
     <Compile Include="Collections\DictionaryUtils.cs" />
     <Compile Include="CustomMath\NumberParser.cs" />
+    <Compile Include="Lang\TranslationLoadException.cs" />
   </ItemGroup>
   <ItemGroup>
     <Content Include="libs\log4net.dll" />
--- 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)
 			{
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Lang/TranslationLoadException.cs	Thu Mar 05 20:34:31 2009 +0000
@@ -0,0 +1,24 @@
+//  This file (TranslationLoadException.cs) is a part of the IBBoard project and is copyright 2009 IBBoard
+// 
+//  The file and the library/program it is in are licensed under the GNU LGPL license, either version 3 of the License or (at your option) any later version. Please see COPYING.LGPL for more information and the full license.
+// 
+
+using System;
+using System.IO;
+
+namespace IBBoard
+{
+	/// <summary>
+	/// A sub-class of Exceptions that is thrown when translations fail to load.
+	/// </summary>
+	public class TranslationLoadException : Exception
+	{		
+		public TranslationLoadException(string message) : base(message)
+		{
+		}
+		
+		public TranslationLoadException(string message, Exception ex) : base(message, ex)
+		{
+		}
+	}
+}