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 diff
     1.1 --- a/IBBoard.csproj	Fri Apr 09 15:12:01 2010 +0000
     1.2 +++ b/IBBoard.csproj	Fri Apr 09 19:35:18 2010 +0000
     1.3 @@ -139,6 +139,8 @@
     1.4      <Compile Include="Lang\TranslationXmlLoader.cs" />
     1.5      <Compile Include="Lang\AbstractTranslationSet.cs" />
     1.6      <Compile Include="Lang\TranslationLanguage.cs" />
     1.7 +    <Compile Include="Lang\XmlTranslationSet.cs" />
     1.8 +    <Compile Include="Lang\TranslationXmlExtractor.cs" />
     1.9    </ItemGroup>
    1.10    <ItemGroup>
    1.11      <Content Include="libs\log4net.dll" />
    1.12 @@ -166,6 +168,7 @@
    1.13        <Properties>
    1.14          <Policies>
    1.15            <DotNetNamingPolicy DirectoryNamespaceAssociation="PrefixedHierarchical" ResourceNamePolicy="FileFormatDefault" />
    1.16 +          <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" />
    1.17          </Policies>
    1.18        </Properties>
    1.19      </MonoDevelop>
     2.1 --- a/Lang/AbstractTranslationSet.cs	Fri Apr 09 15:12:01 2010 +0000
     2.2 +++ b/Lang/AbstractTranslationSet.cs	Fri Apr 09 19:35:18 2010 +0000
     2.3 @@ -1,6 +1,6 @@
     2.4  // This file (AbstractTranslationSet.cs) is a part of the IBBoard project and is copyright 2010 IBBoard
     2.5  // 
     2.6 -// 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.
     2.7 +// 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.
     2.8  
     2.9  using System;
    2.10  using System.Collections.Generic;
    2.11 @@ -37,7 +37,7 @@
    2.12  		/// <param name="key">
    2.13  		/// The key (ID) of the translation to retrieve
    2.14  		/// </param>
    2.15 -		public string this[string key]
    2.16 +		public virtual string this[string key]
    2.17  		{
    2.18  			get
    2.19  			{
     3.1 --- a/Lang/Translation.cs	Fri Apr 09 15:12:01 2010 +0000
     3.2 +++ b/Lang/Translation.cs	Fri Apr 09 19:35:18 2010 +0000
     3.3 @@ -83,11 +83,22 @@
     3.4  			foreach (FileInfo file in dir.GetFiles("*.translation"))
     3.5  			{
     3.6  				AbstractTranslationSet translations = loader.LoadTranslations(file.FullName);
     3.7 -				langToTranslationMap[translations.LanguageCode] = translations;
     3.8 +				AddTranslationSet(translations);
     3.9  			}
    3.10  		}
    3.11  		
    3.12  		/// <summary>
    3.13 +		/// Adds the supplied translation set to the list of available translations
    3.14 +		/// </summary>
    3.15 +		/// <param name="translations">
    3.16 +		/// The translation set to add
    3.17 +		/// </param>
    3.18 +		public static void AddTranslationSet(AbstractTranslationSet translations)
    3.19 +		{
    3.20 +			langToTranslationMap[translations.LanguageCode] = translations;
    3.21 +		}
    3.22 +		
    3.23 +		/// <summary>
    3.24  		/// Resets the loaded translations and reverts to no translations.
    3.25  		/// </summary>
    3.26  		public static void Reset()
     4.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     4.2 +++ b/Lang/TranslationXmlExtractor.cs	Fri Apr 09 19:35:18 2010 +0000
     4.3 @@ -0,0 +1,40 @@
     4.4 +//  This file (TranslationXmlExtractor.cs) is a part of the IBBoard project and is copyright 2010 IBBoard
     4.5 +// 
     4.6 +//  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.
     4.7 +
     4.8 +using System;
     4.9 +using System.Collections.Generic;
    4.10 +using System.Xml;
    4.11 +
    4.12 +namespace IBBoard.Lang
    4.13 +{
    4.14 +	public class TranslationXmlExtractor
    4.15 +	{		
    4.16 +		public Dictionary<string, string> ExtractTranslationsFromDocument(XmlDocument doc)
    4.17 +		{
    4.18 +			try
    4.19 +			{
    4.20 +				XmlNodeList translationNodes = doc.GetElementsByTagName("translation");
    4.21 +				Dictionary<string, string> translationStrings = new Dictionary<string, string>();
    4.22 +
    4.23 +				foreach (XmlNode node in translationNodes)
    4.24 +				{
    4.25 +					string id = node.Attributes["id"].Value;
    4.26 +					string text = node.InnerText;
    4.27 +					translationStrings.Add(id, text);
    4.28 +				}
    4.29 +							
    4.30 +				return translationStrings;
    4.31 +			}
    4.32 +			catch(Exception ex)
    4.33 +			{
    4.34 +				throw new TranslationLoadException("Error while parsing " + GetLanguageOfDocument(doc)+" translation: "+ex.Message, ex);
    4.35 +			}
    4.36 +		}
    4.37 +		
    4.38 +		public string GetLanguageOfDocument(XmlDocument doc)
    4.39 +		{
    4.40 +			return doc != null ? doc.DocumentElement.GetAttribute("lang") : "";
    4.41 +		}
    4.42 +	}
    4.43 +}
     5.1 --- a/Lang/TranslationXmlLoader.cs	Fri Apr 09 15:12:01 2010 +0000
     5.2 +++ b/Lang/TranslationXmlLoader.cs	Fri Apr 09 19:35:18 2010 +0000
     5.3 @@ -19,10 +19,12 @@
     5.4  	{
     5.5  		private XmlReaderSettings settings;
     5.6  		private string schemaLocation;
     5.7 +		private TranslationXmlExtractor extractor;
     5.8  		
     5.9  		public TranslationXmlLoader(string schemaLocation)
    5.10  		{
    5.11  			this.schemaLocation = schemaLocation;
    5.12 +			extractor = new TranslationXmlExtractor();
    5.13  		}
    5.14  		
    5.15  		
    5.16 @@ -36,7 +38,7 @@
    5.17  			}				
    5.18  				
    5.19  			XmlDocument doc = LoadTranslationDocument(file);
    5.20 -			ModifiableTranslationSet translations = new ModifiableTranslationSet(GetLanguageOfDocument(doc));
    5.21 +			ModifiableTranslationSet translations = new ModifiableTranslationSet(extractor.GetLanguageOfDocument(doc));
    5.22  			LoadTranslationsFromDocument(doc, translations);
    5.23  			return translations;
    5.24  		}
    5.25 @@ -44,7 +46,7 @@
    5.26  		private XmlDocument LoadTranslationDocument(FileInfo file)
    5.27  		{
    5.28  			XmlDocument doc = new XmlDocument();			
    5.29 -			XmlReader valReader = XmlReader.Create(file.FullName, GetReaderSettings());
    5.30 +			XmlReader valReader = XmlReader.Create(file.OpenRead(), GetReaderSettings());
    5.31  			
    5.32  			try
    5.33  			{
    5.34 @@ -114,24 +116,12 @@
    5.35  		
    5.36  		private void LoadTranslationsFromDocument(XmlDocument doc, ModifiableTranslationSet translations)
    5.37  		{
    5.38 -			try
    5.39 +			Dictionary<string, string> translationStrings = extractor.ExtractTranslationsFromDocument(doc);
    5.40 +			
    5.41 +			foreach (KeyValuePair<string, string> translation in translationStrings)
    5.42  			{
    5.43 -				XmlNodeList translationNodes = doc.GetElementsByTagName("translation");
    5.44 -
    5.45 -				foreach (XmlNode node in translationNodes)
    5.46 -				{
    5.47 -					translations.SetTranslation(node.Attributes["id"].Value, node.InnerText);
    5.48 -				}
    5.49 +				translations.SetTranslation(translation.Key, translation.Value);
    5.50  			}
    5.51 -			catch(Exception ex)
    5.52 -			{
    5.53 -				throw new TranslationLoadException("Error while parsing " + GetLanguageOfDocument(doc)+" translation: "+ex.Message, ex);
    5.54 -			}	
    5.55 -		}
    5.56 -		
    5.57 -		private string GetLanguageOfDocument(XmlDocument doc)
    5.58 -		{
    5.59 -			return doc != null ? doc.DocumentElement.GetAttribute("lang") : "";
    5.60  		}
    5.61  	}
    5.62  }
     6.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     6.2 +++ b/Lang/XmlTranslationSet.cs	Fri Apr 09 19:35:18 2010 +0000
     6.3 @@ -0,0 +1,61 @@
     6.4 +//  This file (XmlTranslationSet.cs) is a part of the IBBoard project and is copyright 2010 IBBoard
     6.5 +// 
     6.6 +//  // 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.
     6.7 +
     6.8 +using System;
     6.9 +using System.Collections.Generic;
    6.10 +using System.Xml;
    6.11 +
    6.12 +namespace IBBoard.Lang
    6.13 +{
    6.14 +	public class XmlTranslationSet : AbstractTranslationSet
    6.15 +	{
    6.16 +		private string parentLanguage;
    6.17 +		private XmlDocument doc;
    6.18 +		
    6.19 +		public XmlTranslationSet(String languageCode) : base(languageCode)
    6.20 +		{
    6.21 +			//Do nothing extra
    6.22 +		}
    6.23 +		
    6.24 +		public override string this[string key]
    6.25 +		{
    6.26 +			get
    6.27 +			{
    6.28 +				if (translations.Count == 0 && doc != null)
    6.29 +				{
    6.30 +					PopulateTranslations();
    6.31 +				}
    6.32 +				
    6.33 +				return base[key];
    6.34 +			}
    6.35 +		}
    6.36 +
    6.37 +		private void PopulateTranslations()
    6.38 +		{
    6.39 +			Dictionary<string, string> docTranslations = new TranslationXmlExtractor().ExtractTranslationsFromDocument(doc);
    6.40 +			
    6.41 +			foreach (KeyValuePair<string, string> pair in docTranslations)
    6.42 +			{
    6.43 +    			translations.Add(pair.Key, pair.Value);
    6.44 +			}
    6.45 +		}
    6.46 +
    6.47 +
    6.48 +		
    6.49 +		public void SetParentLanguage(string parentLanguageCode)
    6.50 +		{
    6.51 +			parentLanguage = parentLanguageCode;
    6.52 +		}
    6.53 +		
    6.54 +		protected override AbstractTranslationSet GetParentTranslations()
    6.55 +		{
    6.56 +			return Translation.GetTranslationSet(parentLanguage);
    6.57 +		}
    6.58 +		
    6.59 +		public void SetSourceXml(XmlDocument sourceDocument)
    6.60 +		{
    6.61 +			doc = sourceDocument;
    6.62 +		}
    6.63 +	}
    6.64 +}