changeset 78:da339d10c5fe

Re #32: Add staged loading of translations * Add "XML Translation Set" that lazy loads translations from XML * Extract some methods out to an XML extractor * Fix header in abstract translation set * Extract method in Translation class to add a translation set (allows combined file loading and in-code creation)
author IBBoard <dev@ibboard.co.uk>
date Fri, 09 Apr 2010 19:35:18 +0000
parents eb47e17ec824
children a70d89de1435
files IBBoard.csproj Lang/AbstractTranslationSet.cs Lang/Translation.cs Lang/TranslationXmlExtractor.cs Lang/TranslationXmlLoader.cs Lang/XmlTranslationSet.cs
diffstat 6 files changed, 126 insertions(+), 21 deletions(-) [+]
line wrap: on
line diff
--- a/IBBoard.csproj	Fri Apr 09 15:12:01 2010 +0000
+++ b/IBBoard.csproj	Fri Apr 09 19:35:18 2010 +0000
@@ -139,6 +139,8 @@
     <Compile Include="Lang\TranslationXmlLoader.cs" />
     <Compile Include="Lang\AbstractTranslationSet.cs" />
     <Compile Include="Lang\TranslationLanguage.cs" />
+    <Compile Include="Lang\XmlTranslationSet.cs" />
+    <Compile Include="Lang\TranslationXmlExtractor.cs" />
   </ItemGroup>
   <ItemGroup>
     <Content Include="libs\log4net.dll" />
@@ -166,6 +168,7 @@
       <Properties>
         <Policies>
           <DotNetNamingPolicy DirectoryNamespaceAssociation="PrefixedHierarchical" ResourceNamePolicy="FileFormatDefault" />
+          <StandardHeader Text=" This file (${FileName}) is a part of the ${ProjectName} project and is copyright ${Year} ${CopyrightHolder}&#xA;&#xA; The file and the library/program it is in are licensed and distributed, without warranty, under the GNU LGPL, either version 3 of the License or (at your option) any later version. Please see COPYING for more information and the full license." inheritsSet="MITX11License" />
         </Policies>
       </Properties>
     </MonoDevelop>
--- a/Lang/AbstractTranslationSet.cs	Fri Apr 09 15:12:01 2010 +0000
+++ b/Lang/AbstractTranslationSet.cs	Fri Apr 09 19:35:18 2010 +0000
@@ -1,6 +1,6 @@
 // This file (AbstractTranslationSet.cs) is a part of the IBBoard project and is copyright 2010 IBBoard
 // 
-// The file and the library/program it is in are licensed and distributed, without warranty, under the GNU Affero GPL license, either version 3 of the License or (at your option) any later version. Please see COPYING for more information and the full license.
+// The file and the library/program it is in are licensed and distributed, without warranty, under the GNU LGPL, either version 3 of the License or (at your option) any later version. Please see COPYING for more information and the full license.
 
 using System;
 using System.Collections.Generic;
@@ -37,7 +37,7 @@
 		/// <param name="key">
 		/// The key (ID) of the translation to retrieve
 		/// </param>
-		public string this[string key]
+		public virtual string this[string key]
 		{
 			get
 			{
--- a/Lang/Translation.cs	Fri Apr 09 15:12:01 2010 +0000
+++ b/Lang/Translation.cs	Fri Apr 09 19:35:18 2010 +0000
@@ -83,11 +83,22 @@
 			foreach (FileInfo file in dir.GetFiles("*.translation"))
 			{
 				AbstractTranslationSet translations = loader.LoadTranslations(file.FullName);
-				langToTranslationMap[translations.LanguageCode] = translations;
+				AddTranslationSet(translations);
 			}
 		}
 		
 		/// <summary>
+		/// Adds the supplied translation set to the list of available translations
+		/// </summary>
+		/// <param name="translations">
+		/// The translation set to add
+		/// </param>
+		public static void AddTranslationSet(AbstractTranslationSet translations)
+		{
+			langToTranslationMap[translations.LanguageCode] = translations;
+		}
+		
+		/// <summary>
 		/// Resets the loaded translations and reverts to no translations.
 		/// </summary>
 		public static void Reset()
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Lang/TranslationXmlExtractor.cs	Fri Apr 09 19:35:18 2010 +0000
@@ -0,0 +1,40 @@
+//  This file (TranslationXmlExtractor.cs) is a part of the IBBoard project and is copyright 2010 IBBoard
+// 
+//  The file and the library/program it is in are licensed and distributed, without warranty, under the GNU LGPL, either version 3 of the License or (at your option) any later version. Please see COPYING for more information and the full license.
+
+using System;
+using System.Collections.Generic;
+using System.Xml;
+
+namespace IBBoard.Lang
+{
+	public class TranslationXmlExtractor
+	{		
+		public Dictionary<string, string> ExtractTranslationsFromDocument(XmlDocument doc)
+		{
+			try
+			{
+				XmlNodeList translationNodes = doc.GetElementsByTagName("translation");
+				Dictionary<string, string> translationStrings = new Dictionary<string, string>();
+
+				foreach (XmlNode node in translationNodes)
+				{
+					string id = node.Attributes["id"].Value;
+					string text = node.InnerText;
+					translationStrings.Add(id, text);
+				}
+							
+				return translationStrings;
+			}
+			catch(Exception ex)
+			{
+				throw new TranslationLoadException("Error while parsing " + GetLanguageOfDocument(doc)+" translation: "+ex.Message, ex);
+			}
+		}
+		
+		public string GetLanguageOfDocument(XmlDocument doc)
+		{
+			return doc != null ? doc.DocumentElement.GetAttribute("lang") : "";
+		}
+	}
+}
--- a/Lang/TranslationXmlLoader.cs	Fri Apr 09 15:12:01 2010 +0000
+++ b/Lang/TranslationXmlLoader.cs	Fri Apr 09 19:35:18 2010 +0000
@@ -19,10 +19,12 @@
 	{
 		private XmlReaderSettings settings;
 		private string schemaLocation;
+		private TranslationXmlExtractor extractor;
 		
 		public TranslationXmlLoader(string schemaLocation)
 		{
 			this.schemaLocation = schemaLocation;
+			extractor = new TranslationXmlExtractor();
 		}
 		
 		
@@ -36,7 +38,7 @@
 			}				
 				
 			XmlDocument doc = LoadTranslationDocument(file);
-			ModifiableTranslationSet translations = new ModifiableTranslationSet(GetLanguageOfDocument(doc));
+			ModifiableTranslationSet translations = new ModifiableTranslationSet(extractor.GetLanguageOfDocument(doc));
 			LoadTranslationsFromDocument(doc, translations);
 			return translations;
 		}
@@ -44,7 +46,7 @@
 		private XmlDocument LoadTranslationDocument(FileInfo file)
 		{
 			XmlDocument doc = new XmlDocument();			
-			XmlReader valReader = XmlReader.Create(file.FullName, GetReaderSettings());
+			XmlReader valReader = XmlReader.Create(file.OpenRead(), GetReaderSettings());
 			
 			try
 			{
@@ -114,24 +116,12 @@
 		
 		private void LoadTranslationsFromDocument(XmlDocument doc, ModifiableTranslationSet translations)
 		{
-			try
+			Dictionary<string, string> translationStrings = extractor.ExtractTranslationsFromDocument(doc);
+			
+			foreach (KeyValuePair<string, string> translation in translationStrings)
 			{
-				XmlNodeList translationNodes = doc.GetElementsByTagName("translation");
-
-				foreach (XmlNode node in translationNodes)
-				{
-					translations.SetTranslation(node.Attributes["id"].Value, node.InnerText);
-				}
+				translations.SetTranslation(translation.Key, translation.Value);
 			}
-			catch(Exception ex)
-			{
-				throw new TranslationLoadException("Error while parsing " + GetLanguageOfDocument(doc)+" translation: "+ex.Message, ex);
-			}	
-		}
-		
-		private string GetLanguageOfDocument(XmlDocument doc)
-		{
-			return doc != null ? doc.DocumentElement.GetAttribute("lang") : "";
 		}
 	}
 }
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Lang/XmlTranslationSet.cs	Fri Apr 09 19:35:18 2010 +0000
@@ -0,0 +1,61 @@
+//  This file (XmlTranslationSet.cs) is a part of the IBBoard project and is copyright 2010 IBBoard
+// 
+//  // The file and the library/program it is in are licensed and distributed, without warranty, under the GNU LGPL, either version 3 of the License or (at your option) any later version. Please see COPYING for more information and the full license.
+
+using System;
+using System.Collections.Generic;
+using System.Xml;
+
+namespace IBBoard.Lang
+{
+	public class XmlTranslationSet : AbstractTranslationSet
+	{
+		private string parentLanguage;
+		private XmlDocument doc;
+		
+		public XmlTranslationSet(String languageCode) : base(languageCode)
+		{
+			//Do nothing extra
+		}
+		
+		public override string this[string key]
+		{
+			get
+			{
+				if (translations.Count == 0 && doc != null)
+				{
+					PopulateTranslations();
+				}
+				
+				return base[key];
+			}
+		}
+
+		private void PopulateTranslations()
+		{
+			Dictionary<string, string> docTranslations = new TranslationXmlExtractor().ExtractTranslationsFromDocument(doc);
+			
+			foreach (KeyValuePair<string, string> pair in docTranslations)
+			{
+    			translations.Add(pair.Key, pair.Value);
+			}
+		}
+
+
+		
+		public void SetParentLanguage(string parentLanguageCode)
+		{
+			parentLanguage = parentLanguageCode;
+		}
+		
+		protected override AbstractTranslationSet GetParentTranslations()
+		{
+			return Translation.GetTranslationSet(parentLanguage);
+		}
+		
+		public void SetSourceXml(XmlDocument sourceDocument)
+		{
+			doc = sourceDocument;
+		}
+	}
+}