diff --git a/I18NPortable.Tests/I18NPortableTests.cs b/I18NPortable.Tests/I18NPortableTests.cs index a0402df..385268c 100644 --- a/I18NPortable.Tests/I18NPortableTests.cs +++ b/I18NPortable.Tests/I18NPortableTests.cs @@ -1,4 +1,5 @@ -using System.Collections.Generic; +using System; +using System.Collections.Generic; using System.Linq; using System.Reflection; using Microsoft.VisualStudio.TestTools.UnitTesting; @@ -154,6 +155,19 @@ public void Enums_ShouldBe_Translated() Assert.AreEqual("Gato", animals[Animals.Cat]); Assert.AreEqual("Rata", animals[Animals.Rat]); } + + [TestMethod] + public void Logger_CanBeSet_AsAction() + { + var logs = new List(); + Action logger = text => logs.Add(text); + + I18N.Current.SetLogger(logger); + I18N.Current.Locale = "en"; + I18N.Current.Locale = "es"; + + Assert.IsTrue(logs.Count > 0); + } } public enum Animals diff --git a/I18NPortable/I18N.cs b/I18NPortable/I18N.cs index df0c14c..50e9d8c 100644 --- a/I18NPortable/I18N.cs +++ b/I18NPortable/I18N.cs @@ -90,6 +90,7 @@ public List Languages private bool _throwWhenKeyNotFound; private string _notFoundSymbol = "?"; private string _fallbackLocale; + private Action _logger; private I18N() {} @@ -108,11 +109,12 @@ public I18N SetNotFoundSymbol(string symbol) } /// - /// Enable/disable log traces + /// Enable I18N logs /// - public I18N SetLogEnabled(bool enabled) + /// Action to be invoked as the output of the logger + public I18N SetLogger(Action output) { - _logEnabled = enabled; + _logger = output; return this; } @@ -314,7 +316,7 @@ private void LogTranslations() private void Log(string trace) { if (_logEnabled) - Debug.WriteLine($"[{nameof(I18N)}] {trace}"); + _logger?.Invoke($"[{nameof(I18N)}] {trace}"); } #endregion @@ -323,6 +325,8 @@ public void Unload() { _translations = new Dictionary(); _locales = new Dictionary(); + + _logger?.Invoke($"[{nameof(I18N)}] Unloaded"); } } } \ No newline at end of file diff --git a/I18NPortable/I18NPortable.nuspec b/I18NPortable/I18NPortable.nuspec index b8eadc3..a1db7ac 100644 --- a/I18NPortable/I18NPortable.nuspec +++ b/I18NPortable/I18NPortable.nuspec @@ -2,7 +2,7 @@ I18NPortable - 0.1.6 + 0.1.7 I18NPortable Diego Ponce de Leon Diego Ponce de Leon @@ -10,7 +10,7 @@ https://github.com/xleon/I18N-Portable false Simple cross platform internationalization/translations for Xamarin and Windows apps - Unload translations and languages before Initializing again + Set logger as an Action Copyright 2016 I18N, Internationalization, Translations, Xamarin, Xamarin.Forms, Mvvm, MvvmCross, Xamarin.iOS, Xamarin.Android, iOS, Android, UWP, Windows Universal diff --git a/README.md b/README.md index 8d0bb22..3aab1b9 100644 --- a/README.md +++ b/README.md @@ -146,9 +146,10 @@ The easiest way to bind your views to `I18N` translations is to use the built-in ### Misc -**Disable log** +**Enable logger** - I18N.Current.SetLogEnabled(false); + Action logger = text => Debug.WriteLine(text); + I18N.Current.SetLogger(logger); **Throw an exception whenever a key is not found**