[gbrainy] Fixes issue #613202



commit 42b41f9cc07e2b57b83ce5f40702f64e0798bde9
Author: Jordi Mas <jmas softcatala org>
Date:   Wed Apr 28 08:03:49 2010 +0200

    Fixes issue #613202

 src/Core/Libraries/GetText.cs     |   49 ++++++++++++++++++++++++++++++++
 src/Core/Main/Verbal/Analogies.cs |   56 ++++++++++++++++++++++++++++--------
 src/Core/Main/Verbal/Analogy.cs   |   21 ++++++++++++++
 src/Core/Makefile.am              |    1 +
 4 files changed, 114 insertions(+), 13 deletions(-)
---
diff --git a/src/Core/Libraries/GetText.cs b/src/Core/Libraries/GetText.cs
new file mode 100644
index 0000000..0aeb659
--- /dev/null
+++ b/src/Core/Libraries/GetText.cs
@@ -0,0 +1,49 @@
+/*
+ * Copyright (C) 2010 Jordi Mas i Hernàndez <jmas softcatala org>
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License as
+ * published by the Free Software Foundation; either version 2 of the
+ * License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public
+ * License along with this program; if not, write to the
+ * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
+ * Boston, MA 02111-1307, USA.
+ */
+
+using System;
+using Mono.Unix;
+using System.Runtime.InteropServices;
+
+namespace gbrainy.Core.Libraries
+{
+	static public class GetText
+	{
+		[DllImport("intl")]
+		static extern IntPtr gettext (IntPtr instring);
+		
+		// Verifies if a string is present (true) in the Catalog file 
+		static public bool StringExists (string s)
+		{
+			IntPtr ints = UnixMarshal.StringToHeap (s);
+			try {
+				// gettext returns the input pointer if no translation is found
+				IntPtr r = gettext (ints);
+				return r != ints;
+			}
+			catch (Exception e) {
+				return true;
+			}
+	
+			finally {
+				UnixMarshal.FreeHeap (ints);
+			}
+		}
+	}
+}
diff --git a/src/Core/Main/Verbal/Analogies.cs b/src/Core/Main/Verbal/Analogies.cs
index 7790144..f773923 100644
--- a/src/Core/Main/Verbal/Analogies.cs
+++ b/src/Core/Main/Verbal/Analogies.cs
@@ -19,6 +19,7 @@
 
 using System;
 using System.Collections.Generic;
+using gbrainy.Core.Libraries;
 
 using Mono.Unix;
 
@@ -112,7 +113,11 @@ namespace gbrainy.Core.Main.Verbal
 		public Analogy GetNext ()
 		{
 			int idx;
-			Analogy analogy;
+			Analogy analogy; // Holds a deep copy
+			Analogy analogy_ref; // Holds reference to the object
+			ArrayListIndicesRandom indices = null;
+			int new_right = 0;
+			bool localized = true;
 
 			if (List.Count == 0)
 				return null;
@@ -128,20 +133,19 @@ namespace gbrainy.Core.Main.Verbal
 		
 			try
 			{
-				List.TryGetValue (idx, out analogy);
+				List.TryGetValue (idx, out analogy_ref);
 			}
 
 			catch (KeyNotFoundException)
 			{
-				analogy = null;
+				return null;
 			}
 
-			if (analogy != null && analogy.answers != null) { // Randomize answers order
+			analogy = analogy_ref.Copy ();
 
-				ArrayListIndicesRandom indices;
+			if (analogy.answers != null) { // Randomize answers order
 				string [] answers;
-				int new_right = 0;
-
+			
 				indices = new ArrayListIndicesRandom (analogy.answers.Length);
 				answers = new string [analogy.answers.Length];
 
@@ -149,6 +153,9 @@ namespace gbrainy.Core.Main.Verbal
 
 				for (int i = 0; i < indices.Count; i++)
 				{
+					if (GetText.StringExists (analogy.answers [indices[i]]) == false)
+						localized = false;
+
 					answers [i] = Catalog.GetString (analogy.answers [indices[i]]);
 					if (indices[i] == analogy.right)
 						new_right = i;
@@ -157,13 +164,37 @@ namespace gbrainy.Core.Main.Verbal
 				analogy.answers = answers;
 			}
 
-			analogy.question = Catalog.GetString (analogy.question);
+			if ((GetText.StringExists (analogy.question) == false) ||
+				(String.IsNullOrEmpty (analogy.tip) == false && GetText.StringExists (analogy.tip) == false) ||
+				(String.IsNullOrEmpty (analogy.rationale) == false && GetText.StringExists (analogy.rationale) == false)) 
+				localized = false;
+
+			if (localized == true) {
+				analogy.question = Catalog.GetString (analogy.question);
+
+				if (String.IsNullOrEmpty (analogy.tip) == false)
+					analogy.tip = Catalog.GetString (analogy.tip);
+
+				if (String.IsNullOrEmpty (analogy.rationale) == false)
+					analogy.rationale = Catalog.GetString (analogy.rationale);
+			} else {
 
-			if (String.IsNullOrEmpty (analogy.tip) == false)
-				analogy.tip = Catalog.GetString (analogy.tip);
+				// Get analogy again
+				List.TryGetValue (idx, out analogy_ref);
+				analogy = analogy_ref.Copy ();
 
-			if (String.IsNullOrEmpty (analogy.rationale) == false)
-				analogy.rationale = Catalog.GetString (analogy.rationale);
+				if (analogy.answers != null) { // Randomize answers order
+					string [] answers;
+
+					answers = new string [analogy.answers.Length];
+
+					for (int i = 0; i < indices.Count; i++)
+						answers [i] = analogy.answers [indices[i]];
+
+					analogy.right = new_right;
+					analogy.answers = answers;
+				}
+			}
 
 			return analogy;
 		}
@@ -182,6 +213,5 @@ namespace gbrainy.Core.Main.Verbal
 
 			return base.CheckAnswer (answer);
 		}
-
 	}
 }
diff --git a/src/Core/Main/Verbal/Analogy.cs b/src/Core/Main/Verbal/Analogy.cs
index dabf7b1..8532511 100644
--- a/src/Core/Main/Verbal/Analogy.cs
+++ b/src/Core/Main/Verbal/Analogy.cs
@@ -68,5 +68,26 @@ namespace gbrainy.Core.Main.Verbal
 		{
 
 		}
+
+		// Uses deep copy
+		public Analogy Copy ()
+		{
+			Analogy analogy;
+
+			analogy = new Analogy ();
+			analogy.question = question;
+			analogy.type = type;
+			analogy.tip = tip;
+			analogy.rationale = rationale;
+			analogy.right = right;
+
+			if (answers != null) {
+				analogy.answers = new string [answers.Length];
+				for (int i = 0; i < answers.Length; i++)
+					analogy.answers [i] = answers[i];
+			}
+
+			return analogy;
+		}
 	}
 }
diff --git a/src/Core/Makefile.am b/src/Core/Makefile.am
index afebef2..e8be91c 100644
--- a/src/Core/Makefile.am
+++ b/src/Core/Makefile.am
@@ -40,6 +40,7 @@ CSDISTFILES =  \
 		$(srcdir)/Views/WelcomeView.cs		\
 		$(srcdir)/Platform/Unix.cs		\
 		$(srcdir)/Libraries/CairoContextEx.cs	\
+		$(srcdir)/Libraries/GetText.cs		\
 		$(srcdir)/Libraries/SVGImage.cs
 
 CSFILES = $(CSDISTFILES)	\



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]