Mercurial > repos > IBBoard
changeset 70:753be4b6c3b0
Re #31: Break out Translations for language to own class
* Create loader for translations (pull code from Translation class)
author | IBBoard <dev@ibboard.co.uk> |
---|---|
date | Tue, 06 Apr 2010 16:01:01 +0000 |
parents | b5d7e8b93205 |
children | 40c09e57d213 |
files | Lang/ModifiableTranslationSet.cs Lang/TranslationXmlLoader.cs |
diffstat | 2 files changed, 125 insertions(+), 13 deletions(-) [+] |
line wrap: on
line diff
--- a/Lang/ModifiableTranslationSet.cs Tue Apr 06 15:41:20 2010 +0000 +++ b/Lang/ModifiableTranslationSet.cs Tue Apr 06 16:01:01 2010 +0000 @@ -17,16 +17,9 @@ //Do nothing extra } - public string this[string key] + public void SetTranslation(string key, string translation) { - set - { - translations[key] = value; - } - get - { - return base[key]; - } - } + translations[key] = translation; + } } }
--- a/Lang/TranslationXmlLoader.cs Tue Apr 06 15:41:20 2010 +0000 +++ b/Lang/TranslationXmlLoader.cs Tue Apr 06 16:01:01 2010 +0000 @@ -3,16 +3,135 @@ // 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. using System; +using System.IO; +using System.Xml; +using System.Xml.Schema; +using System.Collections.Generic; +using IBBoard.IO; +using IBBoard.Xml; namespace IBBoard.Lang { - - + /// <summary> + /// A simple loader of translations from XML files + /// </summary> public class TranslationXmlLoader { + private XmlReaderSettings settings; + private string schemaLocation; + + public TranslationXmlLoader(string schemaLocation) + { + this.schemaLocation = schemaLocation; + } + + + public AbstractTranslationSet LoadTranslations(string path) + { + FileInfo file = new FileInfo(path); + + if (!file.Exists) + { + throw new TranslationLoadException("Translation file "+file.FullName+" did not exist"); + } + + XmlDocument doc = LoadTranslationDocument(file); + ModifiableTranslationSet translations = new ModifiableTranslationSet(GetLanguageOfDocument(doc)); + LoadTranslationsFromDocument(doc, translations); + return translations; + } + + private XmlDocument LoadTranslationDocument(FileInfo file) + { + XmlDocument doc = new XmlDocument(); + XmlReader valReader = XmlReader.Create(file.FullName, GetReaderSettings()); + + try + { + doc.Load(valReader); + } + catch (DirectoryNotFoundException ex) + { + throw new TranslationLoadException("Problem validating schema for translation: " + ex.Message, ex); + } + 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; + } + + /// <summary> + /// Lazy-getter for XML reader settings. May throw a <see cref="TranslationLoadException"/> if there is a problem with the translation schema. + /// </summary> + /// <returns> + /// A <see cref="XmlReaderSettings"/> with the default values for validating the translation document against the translation schema + /// </returns> + private XmlReaderSettings GetReaderSettings() + { + if (settings == null) + { + try + { + settings = new XmlReaderSettings(); + settings.ValidationType = ValidationType.Schema; + settings.ValidationFlags = XmlSchemaValidationFlags.ReportValidationWarnings; + settings.ValidationEventHandler+= new ValidationEventHandler(ValidationEventMethod); + XmlSchemaSet cache = new XmlSchemaSet(); + cache.Add("http://ibboard.co.uk/translation", schemaLocation); + settings.Schemas.Add(cache); + } + catch (DirectoryNotFoundException ex) + { + throw new TranslationLoadException("Problem validating schema for translation: " + ex.Message, ex); + } + catch (XmlSchemaException ex) + { + throw new TranslationLoadException("Problem validating schema for translation: " + ex.Message, ex); + } + catch (XmlException ex) + { + throw new TranslationLoadException("Problem reading data for schema: " + ex.Message, ex); + } + } + + return settings; + } + + private void ValidationEventMethod(object sender, ValidationEventArgs e) + { + throw new TranslationLoadException("Problem validating schema for translation: " + e.Exception.Message, e.Exception); + } + + private void LoadTranslationsFromDocument(XmlDocument doc, ModifiableTranslationSet translations) + { + try + { + XmlNodeList translationNodes = doc.GetElementsByTagName("translation"); - public TranslationXmlLoader () + foreach (XmlNode node in translationNodes) + { + translations.SetTranslation(node.Attributes["id"].Value, node.InnerText); + } + } + 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") : ""; } } }