view Lang/AbstractTranslationSet.cs @ 72:cec6c4c0892d

Fixes #34: Get name for translation * Add code for translation name fetching (using native name)
author IBBoard <dev@ibboard.co.uk>
date Tue, 06 Apr 2010 18:20:14 +0000
parents b5d7e8b93205
children 091bfa54d6c7
line wrap: on
line source

// 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.

using System;
using System.Collections.Generic;
using System.Globalization;

namespace IBBoard.Lang
{
	/// <summary>
	/// A collection of translations for a given language. The abstract class must be extended by implementations that
	/// provide different ways of loading the data.
	/// </summary>
	public class AbstractTranslationSet
	{
		private string langCode;
		private string langName;
		protected Dictionary<string, string> translations;
		
		public AbstractTranslationSet(string languageCode)
		{
			langCode = languageCode;
			translations = new Dictionary<string, string>();
		}
		
		/// <summary>
		/// Gets the language code that this translation claims to be for
		/// </summary>
		public string LanguageCode
		{
			get { return langCode; }
		}
		
		/// <summary>
		/// Gets a translation from the translation set, or <code>null</code> if the translation doesn't exist.
		/// </summary>
		/// <param name="key">
		/// The key (ID) of the translation to retrieve
		/// </param>
		public string this[string key]
		{
			set
			{
				translations[key] = value;
			}
			get
			{
				return DictionaryUtils.GetValue(translations, key);
			}
		}
		
		public string LanguageName
		{
			get
			{
				if (langName == null)
				{
					LoadLangName();
				}
				
				return langName;				
			}
		}
		
		private void LoadLangName()
		{
			try
			{
				CultureInfo culture = CultureInfo.GetCultureInfo(langCode);
				langName = culture.NativeName;
			}
			catch(ArgumentException)
			{
				langName = "Unknown ("+langCode+")";
			}
		}
	}
}