# HG changeset patch # User IBBoard # Date 1270841718 0 # Node ID da339d10c5fe0e0650bf9f63cd48bbfd0ee5243b # Parent eb47e17ec8249a4dbbaff0782714ede2eb20ba27 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) diff -r eb47e17ec824 -r da339d10c5fe IBBoard.csproj --- 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 @@ + + @@ -166,6 +168,7 @@ + diff -r eb47e17ec824 -r da339d10c5fe Lang/AbstractTranslationSet.cs --- 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 @@ /// /// The key (ID) of the translation to retrieve /// - public string this[string key] + public virtual string this[string key] { get { diff -r eb47e17ec824 -r da339d10c5fe Lang/Translation.cs --- 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); } } /// + /// Adds the supplied translation set to the list of available translations + /// + /// + /// The translation set to add + /// + public static void AddTranslationSet(AbstractTranslationSet translations) + { + langToTranslationMap[translations.LanguageCode] = translations; + } + + /// /// Resets the loaded translations and reverts to no translations. /// public static void Reset() diff -r eb47e17ec824 -r da339d10c5fe Lang/TranslationXmlExtractor.cs --- /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 ExtractTranslationsFromDocument(XmlDocument doc) + { + try + { + XmlNodeList translationNodes = doc.GetElementsByTagName("translation"); + Dictionary translationStrings = new Dictionary(); + + 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") : ""; + } + } +} diff -r eb47e17ec824 -r da339d10c5fe Lang/TranslationXmlLoader.cs --- 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 translationStrings = extractor.ExtractTranslationsFromDocument(doc); + + foreach (KeyValuePair 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") : ""; } } } diff -r eb47e17ec824 -r da339d10c5fe Lang/XmlTranslationSet.cs --- /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 docTranslations = new TranslationXmlExtractor().ExtractTranslationsFromDocument(doc); + + foreach (KeyValuePair 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; + } + } +}