Created
December 15, 2016 01:01
-
-
Save scionwest/d850ca2bb71b6b95faa7c6472db07ba6 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| using System; | |
| using System.Collections.Generic; | |
| using System.Globalization; | |
| using System.Linq; | |
| using Microsoft.Extensions.Localization; | |
| namespace BlogWeb.Tests.Fakes | |
| { | |
| public class FakeLocalizer<T> : IStringLocalizer<T>, IFakeLocalizerBuilder<T> | |
| { | |
| private Func<string, string, string> indexWithValue; | |
| private IEnumerable<LocalizedString> allLocalizedStrings; | |
| public static IFakeLocalizerBuilder<T> MockNewLocalizer() => new FakeLocalizer<T>(); | |
| public static IFakeLocalizerBuilder<T> MockNewLocalizer(IStringLocalizer<T> existing) | |
| => MockNewLocalizer() | |
| .UseIndex((name, value) => existing[name]) | |
| .UseGetAllStrings(existing.GetAllStrings().ToArray()); | |
| public static IStringLocalizer<T> DefaultLocalizer => | |
| FakeLocalizer<T>.MockNewLocalizer().UseIndex((name, value) => value).Build(); | |
| public LocalizedString this[string name] | |
| { | |
| get | |
| { | |
| if (indexWithValue != null) | |
| { | |
| string localizedString = indexWithValue(name, name); | |
| return new LocalizedString(localizedString, localizedString); | |
| } | |
| return new LocalizedString(name, name); | |
| } | |
| } | |
| public LocalizedString this[string name, params object[] arguments] | |
| { | |
| get | |
| { | |
| return this[name]; | |
| } | |
| } | |
| public IEnumerable<LocalizedString> GetAllStrings(bool includeParentCultures) => this.allLocalizedStrings ?? Enumerable.Empty<LocalizedString>(); | |
| public IStringLocalizer WithCulture(CultureInfo culture) => this; | |
| IFakeLocalizerBuilder<T> IFakeLocalizerBuilder<T>.UseGetAllStrings(params LocalizedString[] strings) | |
| { | |
| this.allLocalizedStrings = strings; | |
| return this; | |
| } | |
| IStringLocalizer<T> IFakeLocalizerBuilder<T>.Build() => this; | |
| IFakeLocalizerBuilder<T> IFakeLocalizerBuilder<T>.UseIndex(Func<string, string> indexerWithNamedValue) | |
| { | |
| this.indexWithValue = new Func<string, string, string>((name, value) => indexerWithNamedValue(name)); | |
| return this; | |
| } | |
| IFakeLocalizerBuilder<T> IFakeLocalizerBuilder<T>.UseIndex(Func<string, string, string> IndexWithNameAndValue) | |
| { | |
| this.indexWithValue = IndexWithNameAndValue; | |
| return this; | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment