public static IServiceCollection AddJsonLocalization(this IServiceCollection services, IDictionaryBuilder dictionaryBuilder, Action <JsonLocalizationOptions> setupAction) { if (services == null) { throw new ArgumentNullException(nameof(services)); } if (dictionaryBuilder == null) { throw new ArgumentNullException(nameof(dictionaryBuilder)); } if (setupAction == null) { throw new ArgumentNullException(nameof(setupAction)); } void SetupWrapperAction(JsonLocalizationOptions options) { setupAction(options); I18N.BuildDictionary(dictionaryBuilder, options); } services.Configure <JsonLocalizationOptions>(SetupWrapperAction); services.AddScoped <II18N, I18N>(); services.AddScoped <IKeyProvider, DefaultKeyProvider>(); return(services); }
public void Translate(string expected, string json, string key, string fallback, string[] args) { var options = new JsonLocalizationOptions { ResourceFolders = new[] { "test" }, DefaultLocale = "en-CA" }; DefaultDictionaryBuilder.ReadAllText = path => json; DefaultDictionaryBuilder.DirectoryGetFiles = (path, searchPattern, searchOption) => new[] { "en-ca.json" }; I18N.BuildDictionary(new DefaultDictionaryBuilder(), options); var sut = new I18N(new OptionsWrapper <JsonLocalizationOptions>(options), new DefaultKeyProvider()); Assert.Equal(expected, sut.Translate(key, fallback, args)); }