Skip to content

Commit

Permalink
Added indexer to the LanguageLoader, this could be used for a generic…
Browse files Browse the repository at this point in the history
… access.
  • Loading branch information
Lakritzator committed Sep 20, 2015
1 parent c6a9a31 commit a3eefb0
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 5 deletions.
21 changes: 16 additions & 5 deletions Dapplo.Config.Test/LanguageLoaderTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,20 +35,24 @@ namespace Dapplo.Config.Test
public class LanguageLoaderTest
{
public const string Ok = "Ok";
private LanguageLoader languageLoader;

[TestInitialize]
public void Initialize()
{
languageLoader = new LanguageLoader("Dapplo");
}

[TestMethod]
[ExpectedException(typeof(NotSupportedException))]
public async Task TestIllegalInterface()
{
var languageLoader = new LanguageLoader("Dapplo");
await languageLoader.RegisterAndGetAsync<ILanguageLoaderFailTest>();
}

[TestMethod]
public async Task TestModules()
{
var languageLoader = new LanguageLoader("Dapplo");

// Make sure that the module (for testing) is available, we count all file-path which end with the filename
var count = languageLoader.Files.Count(file => file.EndsWith("language_mymodule-en-US.ini"));
Assert.IsTrue(count > 0);
Expand All @@ -57,10 +61,17 @@ public async Task TestModules()
Assert.AreEqual("Some value", languageMyModule.ModuleSettings);
}

[TestMethod]
[TestMethod]
public async Task TestIndexer()
{
var language = await languageLoader.RegisterAndGetAsync<ILanguageLoaderTest>();
await languageLoader.ChangeLanguage("nl-NL");
Assert.AreEqual("Afbreken", language["TestValue"]);
}

[TestMethod]
public async Task TestTranslations()
{
var languageLoader = new LanguageLoader("Dapplo");
var language = await languageLoader.RegisterAndGetAsync<ILanguageLoaderTest>();
Assert.IsTrue(languageLoader.AvailableLanguages.ContainsKey("nl-NL"));
Assert.IsTrue(languageLoader.AvailableLanguages.ContainsKey("en-US"));
Expand Down
6 changes: 6 additions & 0 deletions Dapplo.Config/Language/ILanguage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,5 +34,11 @@ namespace Dapplo.Config.Language
/// </summary>
public interface ILanguage : IDefaultValue
{
/// <summary>
/// Get the translation for a key
/// </summary>
/// <param name="languageKey">Key for the translation</param>
/// <returns>IniValue</returns>
string this[string languageKey] { get; }
}
}
11 changes: 11 additions & 0 deletions Dapplo.Config/Language/Implementation/LanguageExtension.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
using Dapplo.Config.Extension.Implementation;
using System;
using System.Reflection;
using System.Linq;
using Dapplo.Config.Support;

namespace Dapplo.Config.Language.Implementation
{
Expand All @@ -34,6 +36,7 @@ internal class LanguageExtension<T> : AbstractPropertyProxyExtension<T>
public LanguageExtension(IPropertyProxy<T> proxy) : base(proxy)
{
CheckType(typeof (ILanguage));
Proxy.RegisterMethod(ExpressionExtensions.GetMemberName<ILanguage, object>(x => x[null]), GetTranslation);
}

public override void InitProperty(PropertyInfo propertyInfo) {
Expand All @@ -42,5 +45,13 @@ public override void InitProperty(PropertyInfo propertyInfo) {
throw new NotSupportedException(string.Format("Property {0}.{1} has defined a set, this is not allowed for {2} derrived interfaces. Fix by removing the set for the property, leave the get.", propertyInfo.DeclaringType, propertyInfo.Name, typeof(ILanguage).Name));
}
}

/// <summary>
/// Get a single translation
/// </summary>
private void GetTranslation(MethodCallInfo methodCallInfo)
{
methodCallInfo.ReturnValue = Proxy.Properties[methodCallInfo.PropertyNameOf(0)] as string;
}
}
}

0 comments on commit a3eefb0

Please sign in to comment.