diff --git a/Dapplo.Config.Test/LanguageLoaderTest.cs b/Dapplo.Config.Test/LanguageLoaderTest.cs index 388d016..263839a 100644 --- a/Dapplo.Config.Test/LanguageLoaderTest.cs +++ b/Dapplo.Config.Test/LanguageLoaderTest.cs @@ -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(); } [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); @@ -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(); + 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(); Assert.IsTrue(languageLoader.AvailableLanguages.ContainsKey("nl-NL")); Assert.IsTrue(languageLoader.AvailableLanguages.ContainsKey("en-US")); diff --git a/Dapplo.Config/Language/ILanguage.cs b/Dapplo.Config/Language/ILanguage.cs index 7b7819d..a27c8b9 100644 --- a/Dapplo.Config/Language/ILanguage.cs +++ b/Dapplo.Config/Language/ILanguage.cs @@ -34,5 +34,11 @@ namespace Dapplo.Config.Language /// public interface ILanguage : IDefaultValue { + /// + /// Get the translation for a key + /// + /// Key for the translation + /// IniValue + string this[string languageKey] { get; } } } \ No newline at end of file diff --git a/Dapplo.Config/Language/Implementation/LanguageExtension.cs b/Dapplo.Config/Language/Implementation/LanguageExtension.cs index 66ef19c..01ec298 100644 --- a/Dapplo.Config/Language/Implementation/LanguageExtension.cs +++ b/Dapplo.Config/Language/Implementation/LanguageExtension.cs @@ -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 { @@ -34,6 +36,7 @@ internal class LanguageExtension : AbstractPropertyProxyExtension public LanguageExtension(IPropertyProxy proxy) : base(proxy) { CheckType(typeof (ILanguage)); + Proxy.RegisterMethod(ExpressionExtensions.GetMemberName(x => x[null]), GetTranslation); } public override void InitProperty(PropertyInfo propertyInfo) { @@ -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)); } } + + /// + /// Get a single translation + /// + private void GetTranslation(MethodCallInfo methodCallInfo) + { + methodCallInfo.ReturnValue = Proxy.Properties[methodCallInfo.PropertyNameOf(0)] as string; + } } } \ No newline at end of file