Skip to content
This repository has been archived by the owner on Nov 12, 2023. It is now read-only.

Commit

Permalink
Merge branch 'develop'
Browse files Browse the repository at this point in the history
  • Loading branch information
Diego Ponce de León committed Jul 6, 2016
2 parents 6a1c745 + 93bb160 commit 7b86a19
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 9 deletions.
16 changes: 15 additions & 1 deletion I18NPortable.Tests/I18NPortableTests.cs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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<string>();
Action<string> 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
Expand Down
12 changes: 8 additions & 4 deletions I18NPortable/I18N.cs
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ public List<PortableLanguage> Languages
private bool _throwWhenKeyNotFound;
private string _notFoundSymbol = "?";
private string _fallbackLocale;
private Action<string> _logger;

private I18N() {}

Expand All @@ -108,11 +109,12 @@ public I18N SetNotFoundSymbol(string symbol)
}

/// <summary>
/// Enable/disable log traces
/// Enable I18N logs
/// </summary>
public I18N SetLogEnabled(bool enabled)
/// <param name="output">Action to be invoked as the output of the logger</param>
public I18N SetLogger(Action<string> output)
{
_logEnabled = enabled;
_logger = output;
return this;
}

Expand Down Expand Up @@ -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
Expand All @@ -323,6 +325,8 @@ public void Unload()
{
_translations = new Dictionary<string, string>();
_locales = new Dictionary<string, string>();

_logger?.Invoke($"[{nameof(I18N)}] Unloaded");
}
}
}
4 changes: 2 additions & 2 deletions I18NPortable/I18NPortable.nuspec
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@
<package >
<metadata>
<id>I18NPortable</id>
<version>0.1.6</version>
<version>0.1.7</version>
<title>I18NPortable</title>
<authors>Diego Ponce de Leon</authors>
<owners>Diego Ponce de Leon</owners>
<licenseUrl>https://github.com/xleon/I18N-Portable/blob/master/LICENSE</licenseUrl>
<projectUrl>https://github.com/xleon/I18N-Portable</projectUrl>
<requireLicenseAcceptance>false</requireLicenseAcceptance>
<description>Simple cross platform internationalization/translations for Xamarin and Windows apps</description>
<releaseNotes>Unload translations and languages before Initializing again</releaseNotes>
<releaseNotes>Set logger as an Action</releaseNotes>
<copyright>Copyright 2016</copyright>
<tags>I18N, Internationalization, Translations, Xamarin, Xamarin.Forms, Mvvm, MvvmCross, Xamarin.iOS, Xamarin.Android, iOS, Android, UWP, Windows Universal</tags>
</metadata>
Expand Down
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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<string> logger = text => Debug.WriteLine(text);
I18N.Current.SetLogger(logger);

**Throw an exception whenever a key is not found**

Expand Down

0 comments on commit 7b86a19

Please sign in to comment.