Mercurial > repos > IBBoard
diff Lang/XmlTranslationSet.cs @ 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 | |
children | a23c07137fa4 |
line wrap: on
line diff
--- /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; + } + } +}