private CultureExpression ExtractLanguageFromHeader(HttpContext context, CultureContext cultureContext) { CultureOptions cultureOptions = cultureContext.Options; StringValues languageValues; if (!context.Request.Headers.TryGetValue("Accept-Language", out languageValues)) { return(null); } List <string> languageCodes = new List <string>(); foreach (string lan in languageValues) { languageCodes.AddRange(lan.Split(';').Select(x => x.ToLower())); } CultureExpression exp; foreach (string lan in languageCodes) { if (CultureExpression.TryParse(lan, out exp) && cultureOptions.IsLanguageSupported(exp)) { return(exp); } } return(null); }
private CultureExpression ExtractLanguageFromUrl(HttpContext context, CultureContext cultureContext, out string urlSpecifier) { CultureOptions cultureOptions = cultureContext.Options; string path = context.Request.Path.Value; int slashIndex = 1; int pathLength = path.Length; for (; slashIndex < pathLength; slashIndex++) { if (path[slashIndex] == '/') { break; } } string lang = path.Substring(1, slashIndex - 1).ToLower(); if (!CultureExpression.TryParse(lang, out CultureExpression cultureExpression)) { urlSpecifier = ""; return(null); } urlSpecifier = path.Substring(0, slashIndex); if (slashIndex < pathLength) { context.Request.Path = new PathString(path.Substring(slashIndex)); } else { context.Request.Path = new PathString("/"); } if (!cultureContext.Options.IsLanguageSupported(cultureExpression)) { return(null); } return(cultureExpression); }
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 basePath = Path.Combine("Views", controllerName, viewName); IServiceProvider services = actionContext.HttpContext.RequestServices; CultureContext cultureContext = services.GetRequiredService <CultureContext>(); CultureExpression cultureExpression = cultureContext.Culture; string requestedLanguage = cultureExpression.Language; string lang = requestedLanguage; string nameResult; if (!cultureExpression.IsAllRegion && IsViewFileExisits(lang, out nameResult)) { // exact match viewResult.ViewName = nameResult; viewResult.ViewData["Language"] = new LanguageRequestResult(lang, lang, true); return(base.FindView(actionContext, viewResult)); } lang = cultureExpression.LanguageName; if (IsViewFileExisits(lang, out nameResult)) { // language name match viewResult.ViewName = nameResult; viewResult.ViewData["Language"] = new LanguageRequestResult(requestedLanguage, lang, true); return(base.FindView(actionContext, viewResult)); } else { // find the first availble region of one language IDirectoryContents directoryContents = env.ContentRootFileProvider.GetDirectoryContents(Path.Combine("Views", controllerName)); string startsWithFilter = $"{viewName}.{lang}"; IFileInfo file = directoryContents.FirstOrDefault(x => x.Name.StartsWith(startsWithFilter) && x.Name.EndsWith(".cshtml")); if (file != null) { string cultureName = file.Name.Substring(viewName.Length + 1); cultureName.Substring(0, cultureName.Length - 7); nameResult = file.Name.Substring(0, file.Name.Length - 7); if (CultureExpression.TryParse(cultureName, out CultureExpression exp)) { viewResult.ViewData["Language"] = new LanguageRequestResult(requestedLanguage, exp.Language, true); } else { viewResult.ViewData["Language"] = new LanguageRequestResult(requestedLanguage, null, false); } viewResult.ViewName = nameResult; return(base.FindView(actionContext, viewResult)); } } CultureExpression defaultLanguage = cultureContext.Options.DefaultLanguage; lang = defaultLanguage.Language; if (IsViewFileExisits(lang, out nameResult)) { // default language match viewResult.ViewName = nameResult; viewResult.ViewData["Language"] = new LanguageRequestResult(requestedLanguage, lang, false); return(base.FindView(actionContext, viewResult)); } lang = defaultLanguage.LanguageName; if (IsViewFileExisits(lang, out nameResult)) { // default language name match viewResult.ViewName = nameResult; viewResult.ViewData["Language"] = new LanguageRequestResult(requestedLanguage, lang, false); return(base.FindView(actionContext, viewResult)); } // default file match viewResult.ViewName = viewName; viewResult.ViewData["Language"] = new LanguageRequestResult(requestedLanguage, null, false); return(base.FindView(actionContext, viewResult)); bool IsViewFileExisits(string language, out string viewNameaResult) { string pathWithLang = $"{basePath}.{lang}.cshtml"; if (env.ContentRootFileProvider.GetFileInfo(pathWithLang).Exists) { viewNameaResult = viewName + "." + lang; return(true); } viewNameaResult = null; return(false); } }