changeset 73:091bfa54d6c7

Re #33: Add method to get list of available translations * Add "translation language" object to pair up code and name * Load all translations at initialisation time Also: * Remove rogue "set" property on abstract translation set
author IBBoard <dev@ibboard.co.uk>
date Tue, 06 Apr 2010 18:58:30 +0000
parents cec6c4c0892d
children 726731c78414
files IBBoard.csproj Lang/AbstractTranslationSet.cs Lang/Translation.cs Lang/TranslationLanguage.cs
diffstat 4 files changed, 144 insertions(+), 60 deletions(-) [+]
line wrap: on
line diff
--- a/IBBoard.csproj	Tue Apr 06 18:20:14 2010 +0000
+++ b/IBBoard.csproj	Tue Apr 06 18:58:30 2010 +0000
@@ -138,6 +138,7 @@
     <Compile Include="Lang\ModifiableTranslationSet.cs" />
     <Compile Include="Lang\TranslationXmlLoader.cs" />
     <Compile Include="Lang\AbstractTranslationSet.cs" />
+    <Compile Include="Lang\TranslationLanguage.cs" />
   </ItemGroup>
   <ItemGroup>
     <Content Include="libs\log4net.dll" />
--- a/Lang/AbstractTranslationSet.cs	Tue Apr 06 18:20:14 2010 +0000
+++ b/Lang/AbstractTranslationSet.cs	Tue Apr 06 18:58:30 2010 +0000
@@ -4,7 +4,6 @@
 
 using System;
 using System.Collections.Generic;
-using System.Globalization;
 
 namespace IBBoard.Lang
 {
@@ -14,13 +13,12 @@
 	/// </summary>
 	public class AbstractTranslationSet
 	{
-		private string langCode;
-		private string langName;
+		private TranslationLanguage language;
 		protected Dictionary<string, string> translations;
 		
 		public AbstractTranslationSet(string languageCode)
 		{
-			langCode = languageCode;
+			language = new TranslationLanguage(languageCode);
 			translations = new Dictionary<string, string>();
 		}
 		
@@ -29,7 +27,7 @@
 		/// </summary>
 		public string LanguageCode
 		{
-			get { return langCode; }
+			get { return language.Code; }
 		}
 		
 		/// <summary>
@@ -40,10 +38,6 @@
 		/// </param>
 		public string this[string key]
 		{
-			set
-			{
-				translations[key] = value;
-			}
 			get
 			{
 				return DictionaryUtils.GetValue(translations, key);
@@ -54,26 +48,34 @@
 		{
 			get
 			{
-				if (langName == null)
-				{
-					LoadLangName();
-				}
-				
-				return langName;				
+				return language.Name;
 			}
 		}
+
+		public TranslationLanguage Language
+		{
+			get { return language; }
+		}
 		
-		private void LoadLangName()
+		public override bool Equals(object obj)
 		{
-			try
+			bool equal = true;
+			
+			if (obj == null || !obj.GetType().Equals(GetType()))
+			{
+				equal = false;
+			}
+			else
 			{
-				CultureInfo culture = CultureInfo.GetCultureInfo(langCode);
-				langName = culture.NativeName;
+				equal = LanguageCode.Equals(((AbstractTranslationSet)obj).LanguageCode);
 			}
-			catch(ArgumentException)
-			{
-				langName = "Unknown ("+langCode+")";
-			}
+			
+			return equal;
+		}
+		
+		public override int GetHashCode ()
+		{
+			return GetType().GetHashCode() + LanguageCode.GetHashCode();
 		}
 	}
 }
--- a/Lang/Translation.cs	Tue Apr 06 18:20:14 2010 +0000
+++ b/Lang/Translation.cs	Tue Apr 06 18:58:30 2010 +0000
@@ -26,9 +26,13 @@
 	public class Translation
 	{
 		private static readonly string DIVIDER_STRING = "-";
-		private static DirectoryInfo translationDir;
-		private static AbstractTranslationSet translations;
-		private static TranslationXmlLoader loader;
+		private static AbstractTranslationSet currentTranslations;
+		private static Dictionary<string, AbstractTranslationSet> langToTranslationMap;
+		
+		static Translation()
+		{
+			langToTranslationMap = new Dictionary<string, AbstractTranslationSet>();
+		}
 
 		/// <summary>
 		/// Initialises the translations and loads the specified language so that the Translation class can be used.
@@ -60,17 +64,25 @@
 		
 		private static void InitialiseDefaults(string appPath)
 		{
-			string translationPath = Path.Combine(appPath, "translations");
-
-			if (Directory.Exists(translationPath))
+			Reset();
+			LoadTranslations(appPath);
+		}
+		
+		private static void LoadTranslations(string appPath)
+		{
+			DirectoryInfo dir = new DirectoryInfo(Path.Combine(appPath, "translations"));
+			
+			if (!dir.Exists)
 			{
-				translations = null;
-				translationDir = new DirectoryInfo(translationPath);
-				loader = new TranslationXmlLoader(Path.Combine(appPath, "schemas/translation.xsd"));
+				throw new TranslationLoadException("Translation path not found (" + dir.FullName + ")");
 			}
-			else
+						
+			TranslationXmlLoader loader = new TranslationXmlLoader(Path.Combine(appPath, "schemas/translation.xsd"));
+			
+			foreach (FileInfo file in dir.GetFiles("*.translation"))
 			{
-				throw new TranslationLoadException("Translation path not found ("+new FileInfo(translationPath).FullName+")");
+				AbstractTranslationSet translations = loader.LoadTranslations(file.FullName);
+				langToTranslationMap[translations.LanguageCode] = translations;
 			}
 		}
 		
@@ -79,7 +91,8 @@
 		/// </summary>
 		public static void Reset()
 		{
-			translations = null;
+			currentTranslations = null;
+			langToTranslationMap.Clear();
 		}
 
 		/// <summary>
@@ -91,7 +104,7 @@
 		/// The new local language to load
 		/// </param>
 		public static void LoadTranslation(string translationLanguage)
-		{			
+		{
 			if (translationLanguage == "" || translationLanguage == null)
 			{
 				throw new ArgumentException("Translation language cannot be null or empty");
@@ -102,27 +115,7 @@
 		
 		private static void LoadTranslationForLanguage(string translationLanguage)
 		{			
-			if (translationLanguage != "" && translationLanguage != null)
-			{
-				translations = loader.LoadTranslations(GetTranslationFile(translationLanguage));
-			}
-			else
-			{
-				translations = null;
-			}
-		}
-		
-		private static string GetTranslationFile(string language)
-		{
-			string translationFileName = language + ".translation";
-			string path = Path.Combine(translationDir.FullName, translationFileName);
-
-			if (!File.Exists(path))
-			{
-				throw new TranslationLoadException(translationFileName +" could not be found in "+translationDir.FullName);
-			}
-			
-			return path;
+			currentTranslations = DictionaryUtils.GetValue(langToTranslationMap, translationLanguage);
 		}
 
 		/// <summary>
@@ -195,9 +188,9 @@
 		{
 			string translation = null;
 			
-			if (translations!=null)
+			if (currentTranslations!=null)
 			{
-				translation = translations[translationID];
+				translation = currentTranslations[translationID];
 			}
 			
 			return translation;
@@ -271,7 +264,19 @@
 		/// </returns>
 		public static string GetTranslationLanguage()
 		{
-			return (translations!=null ? translations.LanguageCode : "");
+			return (currentTranslations != null ? currentTranslations.LanguageCode : "");
+		}
+		
+		public static ICollection<TranslationLanguage> GetLanguages()
+		{
+			ICollection<TranslationLanguage> langs = new List<TranslationLanguage>();
+			
+			foreach (AbstractTranslationSet translations in langToTranslationMap.Values)
+			{
+				langs.Add(translations.Language);
+			}
+			
+			return langs;
 		}
 	}
 }
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Lang/TranslationLanguage.cs	Tue Apr 06 18:58:30 2010 +0000
@@ -0,0 +1,76 @@
+// This file (TranslationLanguage.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.Globalization;
+
+namespace IBBoard.Lang
+{
+	/// <summary>
+	/// A simple object that holds the name and code pairing for a language
+	/// </summary>
+	public class TranslationLanguage
+	{
+		private string langCode;
+		private string langName;
+		
+		public TranslationLanguage(string languageCode)
+		{
+			langCode = languageCode;
+		}
+		
+		public string Name
+		{
+			get
+			{
+				if (langName == null)
+				{
+					LoadLangName();
+				}
+				
+				return langName; 
+			}
+		}
+		
+		public string Code
+		{
+			get { return langCode; }
+		}
+		
+		private void LoadLangName()
+		{
+			try
+			{
+				CultureInfo culture = CultureInfo.GetCultureInfo(langCode);
+				langName = culture.NativeName;
+			}
+			catch (ArgumentException)
+			{
+				langName = "Unknown (" + langCode + ")";
+			}
+		}
+		
+		public override bool Equals(object obj)
+		{
+			bool equal = true;
+			
+			if (obj == null || !obj.GetType().Equals(GetType()))
+			{
+				equal = false;
+			}
+
+			else
+			{
+				equal = Code.Equals(((TranslationLanguage)obj).Code);
+			}
+			
+			return equal;
+		}
+		
+		public override int GetHashCode()
+		{
+			return GetType().GetHashCode() + langCode.GetHashCode();
+		}
+	}
+}