public CodeMatchingCache( int cultureCacheCapacity, int perCultureCapacity, ICodeMatchingOption matchingOption, ICultureOption cultureOption, ICultureFileCache cultureFileCache) { this.cultureCache = new Cache <string, CulturePage>(cultureCacheCapacity); this.perCultureCapacity = perCultureCapacity; this.matchingOption = matchingOption; this.cultureOption = cultureOption; this.cultureFileCache = cultureFileCache; }
public static IServiceCollection AddCodeMatching( this IServiceCollection services, string resourceDirectory = "/Strings", bool isCaseSensitive = false, int culturePageCacheSize = 100, int cultureCacheSize = 20) { services.AddSingleton <ICodeMatchingOption>(new CodeMatchingOption(resourceDirectory, isCaseSensitive)); services.AddSingleton <ICodeMatchingCache>(provider => { ICodeMatchingOption option = provider.GetRequiredService <ICodeMatchingOption>(); ICultureOption cultureOption = provider.GetRequiredService <ICultureOption>(); ICultureFileCache cultureFileCache = provider.GetRequiredService <ICultureFileCache>(); return(new CodeMatchingCache(cultureCacheSize, culturePageCacheSize, option, cultureOption, cultureFileCache)); }); services.AddScoped <ICodeMatchingService, CodeMatchingService>(); return(services); }
public override ViewEngineResult FindView(ActionContext actionContext, ViewResult viewResult) { string viewName = viewResult.ViewName; if (viewName == null) { viewName = GetActionName(actionContext); } if (viewName == null) { return(base.FindView(actionContext, viewResult)); } string controllerName; if (!actionContext.ActionDescriptor.RouteValues.TryGetValue(ControllerNameKey, out controllerName) || string.IsNullOrEmpty(controllerName)) { controllerName = ""; } string directory = Path.Combine("Views", controllerName); HttpContext httpContext = actionContext.HttpContext; IServiceProvider provider = httpContext.RequestServices; ICultureOption cultureOption = provider.GetRequiredService <ICultureOption>(); ICultureContext cultureContext = provider.GetRequiredService <ICultureContext>(); CultureMatchingViewResult viewFindResult = (CultureMatchingViewResult)viewResult; ICultureFileCache fileCache = provider.GetRequiredService <ICultureFileCache>(); IFileCultureInfo fileCultureInfo = fileCache.Get(viewFindResult.RequestedCulture, directory, viewName, "cshtml"); if (fileCultureInfo != null) { httpContext.RequestServices.GetRequiredService <ILocalizedViewRenderContextAccessor>().Context = new LocalizedViewRenderContext(viewFindResult.RequestedCulture, fileCultureInfo.Culture, cultureContext.UrlCultureSpecifier); string relativePath = fileCultureInfo.RelativePath.Substring(0, fileCultureInfo.RelativePath.Length - 7); // 7 == ".cshtml".Length relativePath = relativePath.Substring(directory.Length + 1); viewResult.ViewName = relativePath; return(base.FindView(actionContext, viewResult)); } else { httpContext.RequestServices.GetRequiredService <ILocalizedViewRenderContextAccessor>().Context = new LocalizedViewRenderContext(viewFindResult.RequestedCulture, null, cultureContext.UrlCultureSpecifier); return(base.FindView(actionContext, viewResult)); } }