view Lang/TranslationXmlExtractor.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 a70d89de1435
line wrap: on
line source

//  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") : "";
		}
	}
}