[gbrainy] Allow to run gbrainy in English language + warn when translation is not completed



commit 9ba3fb8c0ed7758691ee4b4b181bae9281b2c07a
Author: Jordi Mas <jmas softcatala org>
Date:   Thu Mar 31 00:05:22 2011 +0200

    Allow to run gbrainy in English language + warn when translation is not completed

 src/Clients/Classical/Dialogs/PreferencesDialog.cs |    5 +-
 .../Classical/Dialogs/ui/PreferencesDialog.ui      |   17 ++++-
 src/Clients/Classical/gbrainy.cs                   |   48 ++++++++++-
 src/Clients/WebForms/TranslationsWeb.cs            |    4 +
 src/Core/Libraries/TranslationsCatalog.cs          |   21 +++++
 src/Core/Main/Preferences.cs                       |    4 +
 src/Core/Services/ITranslations.cs                 |    2 +
 tests/Clients/Classical/gbrainyTest.cs             |   96 ++++++++++++++++++++
 tests/Makefile.am                                  |    3 +-
 tests/UnitTests.csproj                             |    1 +
 10 files changed, 197 insertions(+), 4 deletions(-)
---
diff --git a/src/Clients/Classical/Dialogs/PreferencesDialog.cs b/src/Clients/Classical/Dialogs/PreferencesDialog.cs
index 2f1cb53..03f7a5a 100644
--- a/src/Clients/Classical/Dialogs/PreferencesDialog.cs
+++ b/src/Clients/Classical/Dialogs/PreferencesDialog.cs
@@ -36,6 +36,7 @@ namespace gbrainy.Clients.Classical.Dialogs
 		[GtkBeans.Builder.Object] Gtk.RadioButton rb_medium;
 		[GtkBeans.Builder.Object] Gtk.RadioButton rb_master;
 		[GtkBeans.Builder.Object] Gtk.ComboBox themes_combobox;
+		[GtkBeans.Builder.Object] Gtk.CheckButton english_checkbutton;
 
 		const int COLUMN_VALUE = 1;
 		PlayerHistory history;
@@ -48,6 +49,7 @@ namespace gbrainy.Clients.Classical.Dialogs
 			maxstoredspinbutton.Value = Preferences.GetIntValue (Preferences.MaxStoredGamesKey);
 			minplayedspinbutton.Value = Preferences.GetIntValue (Preferences.MinPlayedGamesKey);
 			colorblindcheckbutton.Active = Preferences.GetBoolValue (Preferences.ColorBlindKey);
+			english_checkbutton.Active = Preferences.GetBoolValue (Preferences.EnglishKey);
 
 			switch ((GameDifficulty) Preferences.GetIntValue (Preferences.DifficultyKey)) {
 			case GameDifficulty.Easy:
@@ -61,7 +63,7 @@ namespace gbrainy.Clients.Classical.Dialogs
 				break;
 			}
 
-			ListStore store = new ListStore (typeof (string), typeof (Theme)); // DisplayName, theme referenece
+			ListStore store = new ListStore (typeof (string), typeof (Theme)); // DisplayName, theme reference
 			CellRenderer layout_cell = new CellRendererText ();
 			themes_combobox.Model = store;
 			themes_combobox.PackStart (layout_cell, true);
@@ -120,6 +122,7 @@ namespace gbrainy.Clients.Classical.Dialogs
 			Preferences.SetIntValue (Preferences.MaxStoredGamesKey, (int) maxstoredspinbutton.Value);
 			Preferences.SetIntValue (Preferences.MinPlayedGamesKey, (int) minplayedspinbutton.Value);
 			Preferences.SetBoolValue (Preferences.ColorBlindKey, colorblindcheckbutton.Active);
+			Preferences.SetBoolValue (Preferences.EnglishKey, english_checkbutton.Active);
 
 			TreeIter iter;
 			themes_combobox.GetActiveIter (out iter);
diff --git a/src/Clients/Classical/Dialogs/ui/PreferencesDialog.ui b/src/Clients/Classical/Dialogs/ui/PreferencesDialog.ui
index f364442..81e1e84 100644
--- a/src/Clients/Classical/Dialogs/ui/PreferencesDialog.ui
+++ b/src/Clients/Classical/Dialogs/ui/PreferencesDialog.ui
@@ -43,6 +43,18 @@
               </packing>
             </child>
             <child>
+              <object class="GtkCheckButton" id="english_checkbutton">
+                <property name="label" translatable="yes">Run gbrainy using English language (ignore translation)</property>
+                <property name="visible">True</property>
+                <property name="can_focus">True</property>
+                <property name="receives_default">False</property>
+                <property name="draw_indicator">True</property>
+              </object>
+              <packing>
+                <property name="position">2</property>
+              </packing>
+            </child>
+            <child>
               <object class="GtkHBox" id="hbox1">
                 <property name="visible">True</property>
                 <child>
@@ -64,9 +76,12 @@
                     <property name="position">1</property>
                   </packing>
                 </child>
+                <child>
+                  <placeholder/>
+                </child>
               </object>
               <packing>
-                <property name="position">2</property>
+                <property name="position">3</property>
               </packing>
             </child>
           </object>
diff --git a/src/Clients/Classical/gbrainy.cs b/src/Clients/Classical/gbrainy.cs
index e1c8225..6678dea 100644
--- a/src/Clients/Classical/gbrainy.cs
+++ b/src/Clients/Classical/gbrainy.cs
@@ -80,12 +80,18 @@ namespace gbrainy.Clients.Classical
 		bool full_screen;
 		GameSession.Types initial_session;
 
+		public readonly int MIN_TRANSLATION = 80;
+
 		public GtkClient ()
 #if GNOME
 		: base ("gbrainy", Defines.VERSION, Modules.UI, new string [0])
 #endif
 		{
-			Catalog.Init ("gbrainy", Defines.GNOME_LOCALE_DIR);
+			if (Preferences.GetBoolValue (Preferences.EnglishKey) == false)
+			{
+				Catalog.Init ("gbrainy", Defines.GNOME_LOCALE_DIR);
+			}
+
 			Unix.FixLocaleInfo ();
 		}
 
@@ -473,6 +479,10 @@ namespace gbrainy.Clients.Classical
 
 		void OnNewGame (GameSession.Types type)
 		{
+			// If the translation is lower than MIN_TRANSLATION explain that running the English version is an option
+			if (ShowTranslationWarning ())
+				Translations ();
+
 			session.Type = type;
 			session.New ();
 			GetNextGame ();
@@ -482,6 +492,42 @@ namespace gbrainy.Clients.Classical
 			UpdateStatusBar ();
 		}
 
+		public bool ShowTranslationWarning ()
+		{
+			// Notify the user once per version only
+			if (String.Compare (Preferences.GetStringValue (Preferences.EnglishVersionKey), Defines.VERSION, 0) == 0)
+				return false;
+
+			bool show;
+	
+			show = (ServiceLocator.Instance.GetService <ITranslations> ().TranslationPercentage < MIN_TRANSLATION);
+
+			if (show == true)
+			{
+				Preferences.SetStringValue (Preferences.EnglishVersionKey, Defines.VERSION);
+				Preferences.Save ();
+			}
+			return show;
+		}
+
+		void Translations ()
+		{		
+			HigMessageDialog dlg;
+	
+			dlg = new HigMessageDialog (app_window,
+				Gtk.DialogFlags.DestroyWithParent,
+				Gtk.MessageType.Warning,
+				Gtk.ButtonsType.Ok,
+				Catalog.GetString ("The level of translation of gbrainy for your language is low."),
+				Catalog.GetString ("You may be exposed to partially translated games making it more difficult to play. If you prefer to play in English you can do so in gbrainy's Preferences."));
+		
+			try {
+	 			dlg.Run ();
+	 		} finally {
+	 			dlg.Destroy ();
+	 		}
+		}
+
 		void OnMathOnly (object sender, EventArgs args)
 		{
 			OnNewGame (GameSession.Types.Calculation);
diff --git a/src/Clients/WebForms/TranslationsWeb.cs b/src/Clients/WebForms/TranslationsWeb.cs
index bd036cc..0eb289d 100644
--- a/src/Clients/WebForms/TranslationsWeb.cs
+++ b/src/Clients/WebForms/TranslationsWeb.cs
@@ -29,6 +29,10 @@ namespace gbrainy.Clients.WebForms
 	{
 		public delegate string GetLanguageFromSessionHandler ();
 		static readonly object sync = new object ();
+		
+		public int TranslationPercentage {
+			get { return 100;}
+		}
 
 		public GetLanguageFromSessionHandler OnGetLanguageFromSession;
 
diff --git a/src/Core/Libraries/TranslationsCatalog.cs b/src/Core/Libraries/TranslationsCatalog.cs
index efc8cec..0f1c37c 100644
--- a/src/Core/Libraries/TranslationsCatalog.cs
+++ b/src/Core/Libraries/TranslationsCatalog.cs
@@ -17,6 +17,7 @@
  * Boston, MA 02111-1307, USA.
  */
 
+using System;
 using Mono.Unix;
 
 using gbrainy.Core.Services;
@@ -25,6 +26,18 @@ namespace gbrainy.Core.Libraries
 {
 	public class TranslationsCatalog : ITranslations
 	{
+		double strings, translated;
+		const int max_sample = 250;
+
+		public int TranslationPercentage { 
+			get {
+				if (strings > 0)
+					return (int) (translated / strings * 100d);
+
+				return 100; // Cannot tell
+			}
+		}
+
 		public void Init (string package, string localedir)
 		{
 			Catalog.Init (package, localedir);
@@ -32,6 +45,14 @@ namespace gbrainy.Core.Libraries
 		
 		public string GetString (string s)
 		{
+			if (strings < max_sample)
+			{
+				if (GetText.StringExists (s))
+					translated++;
+
+				strings++;
+			}
+
 			return Catalog.GetString (s);
 		}
 
diff --git a/src/Core/Main/Preferences.cs b/src/Core/Main/Preferences.cs
index 9cf307d..20bb4e3 100644
--- a/src/Core/Main/Preferences.cs
+++ b/src/Core/Main/Preferences.cs
@@ -40,6 +40,8 @@ namespace gbrainy.Core.Main
 		public const string ToolbarOrientationKey = "ToolbarOrientation";
 		public const string ColorBlindKey = "ColorBlind";
 		public const string ThemeKey = "Theme";
+		public const string EnglishKey = "English";
+		public const string EnglishVersionKey = "EnglishVersion";
 
 		const string element_item = "item";
 		const string element_key = "key";
@@ -179,6 +181,8 @@ namespace gbrainy.Core.Main
 			properties.Add (ToolbarOrientationKey, "0");
 			properties.Add (ColorBlindKey, false.ToString ());
 			properties.Add (ThemeKey, "classic");
+			properties.Add (EnglishVersionKey, string.Empty);
+			properties.Add (EnglishKey, false.ToString ());
 		}
 
 		static void Load ()
diff --git a/src/Core/Services/ITranslations.cs b/src/Core/Services/ITranslations.cs
index 96515ba..7ad859a 100644
--- a/src/Core/Services/ITranslations.cs
+++ b/src/Core/Services/ITranslations.cs
@@ -21,6 +21,8 @@ namespace gbrainy.Core.Services
 {
 	public interface ITranslations : IService
 	{
+		int TranslationPercentage { get;}
+
 		void Init (string package, string localedir);
 		string GetString (string s);
 		string GetPluralString (string s, string p, int n);
diff --git a/tests/Clients/Classical/gbrainyTest.cs b/tests/Clients/Classical/gbrainyTest.cs
new file mode 100644
index 0000000..c52fa0f
--- /dev/null
+++ b/tests/Clients/Classical/gbrainyTest.cs
@@ -0,0 +1,96 @@
+/*
+ * Copyright (C) 2011 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 System.Text;
+using NUnit.Framework;
+
+using gbrainy.Clients.Classical;
+using gbrainy.Core.Main;
+using gbrainy.Core.Services;
+
+namespace gbrainyTest
+{
+	[TestFixture]
+	public class gbrainyTest : UnitTestSupport
+	{
+		TranslationsTest translations;
+
+		public class TranslationsTest : ITranslations
+		{
+			public int Percentage { set; get; }
+			public int TranslationPercentage {
+				get { return Percentage; }
+			}
+
+			public void Init (string package, string localedir) {}
+			public string GetString (string s) { return string.Empty; }
+			public string GetPluralString (string s, string p, int n) { return string.Empty; }
+		}
+
+		[TestFixtureSetUp]
+		public void Construct ()
+		{
+			RegisterDefaultServices ();
+			translations =  new TranslationsTest ();
+			ServiceLocator.Instance.RegisterService (typeof (ITranslations), translations);
+
+
+		}
+
+		[Test]
+		public void ShowTranslationMessageHonorPercentage ()
+		{
+			GtkClient client = new GtkClient ();
+
+			// Ignore gbrainy instance preferences
+			Preferences.LoadDefaultValues ();
+
+			translations.Percentage = client.MIN_TRANSLATION;
+			Assert.AreEqual (false, client.ShowTranslationWarning ());
+		}
+
+		[Test]
+		public void ShowTranslationMessageOnlyOnce ()
+		{
+			GtkClient client = new GtkClient ();
+
+			// Ignore gbrainy instance preferences
+			Preferences.LoadDefaultValues ();
+
+			translations.Percentage = client.MIN_TRANSLATION - 1;
+			Assert.AreEqual (true, client.ShowTranslationWarning ());
+			Assert.AreEqual (false, client.ShowTranslationWarning ());
+		}
+
+		[Test]
+		public void ShowTranslationMessageWhenChangingVersion ()
+		{
+			GtkClient client = new GtkClient ();
+
+			// Ignore gbrainy instance preferences
+			Preferences.LoadDefaultValues ();
+
+			translations.Percentage = client.MIN_TRANSLATION - 1;
+			Assert.AreEqual (true, client.ShowTranslationWarning ());
+			Preferences.SetStringValue (Preferences.EnglishVersionKey, "n.n.n");
+			Assert.AreEqual (true, client.ShowTranslationWarning ());
+		}
+	}
+}
diff --git a/tests/Makefile.am b/tests/Makefile.am
index 32cf024..965f181 100644
--- a/tests/Makefile.am
+++ b/tests/Makefile.am
@@ -19,7 +19,8 @@ CSFILES_CORE =					\
 
 CSFILES_CLASSICAL =					\
 	$(srcdir)/TestSupport/UnitTestSupport.cs \
-	$(srcdir)/Clients/Classical/CommandLineTest.cs
+	$(srcdir)/Clients/Classical/CommandLineTest.cs	\
+	$(srcdir)/Clients/Classical/gbrainyTest.cs
 
 CSFILES_GAMES =						\
 	$(srcdir)/TestSupport/UnitTestSupport.cs \
diff --git a/tests/UnitTests.csproj b/tests/UnitTests.csproj
index c806109..abeeeda 100644
--- a/tests/UnitTests.csproj
+++ b/tests/UnitTests.csproj
@@ -51,6 +51,7 @@
     <Compile Include="Games\Logic\PuzzleNumericRelationTest.cs" />
     <Compile Include="TestSupport\UnitTestSupport.cs" />
     <Compile Include="Core\GameAnswerTest.cs" />
+    <Compile Include="Clients\Classical\gbrainyTest.cs" />
   </ItemGroup>
   <Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
   <ProjectExtensions>



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