view Lang/TranslationXmlExtractor.cs @ 79:a70d89de1435

Re #32: Add staged loading of translations * Add "extends" attribute to schema so that translation files can define what they extend * Add "get parent language" method to extractor * Move loader to using XML translation sets
author IBBoard <dev@ibboard.co.uk>
date Fri, 09 Apr 2010 19:48:51 +0000
parents da339d10c5fe
children a23c07137fa4
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") : "";
		}
		
		public string GetParentLanguageOfDocument(XmlDocument doc)
		{
			return doc != null ? doc.DocumentElement.GetAttribute("extends") : "";
		}
	}
}