public static string GetCurrentLanguage() { var languageDetector = new NccLanguageDetector(new HttpContextAccessor()); var currentLanguage = languageDetector.GetCurrentLanguage(); return(currentLanguage); }
public static string CurrentLanguage(this IHtmlHelper helper) { var langDetector = new NccLanguageDetector(new HttpContextAccessor() { HttpContext = helper.ViewContext.HttpContext }); return(langDetector.GetCurrentLanguage()); }
public async Task <string> RenderToStringAsync <T>(string viewName, object model) { try { var httpContext = _httpContextAccessor.HttpContext; //new DefaultHttpContext { RequestServices = _serviceProvider }; var routeData = httpContext.GetRouteData(); var cad = new ControllerActionDescriptor(); var ac = new ActionContext(httpContext, httpContext.GetRouteData(), cad); var actionContext = new ControllerContext(ac); _nccLanguageDetector = new NccLanguageDetector(_httpContextAccessor); var language = _nccLanguageDetector.GetCurrentLanguage(); _nccTranslator = new NccTranslator(language); if (httpContext.Items.ContainsKey("NCC_RAZOR_PAGE_PROPERTY_CURRENT_LANGUAGE")) { httpContext.Items["NCC_RAZOR_PAGE_PROPERTY_CURRENT_LANGUAGE"] = language; } else { httpContext.Items.Add("NCC_RAZOR_PAGE_PROPERTY_CURRENT_LANGUAGE", language); } if (httpContext.Items.ContainsKey("NCC_RAZOR_PAGE_PROPERTY_TRANSLATOR")) { httpContext.Items["NCC_RAZOR_PAGE_PROPERTY_TRANSLATOR"] = _nccTranslator; } else { httpContext.Items.Add("NCC_RAZOR_PAGE_PROPERTY_TRANSLATOR", _nccTranslator); } if (httpContext.Items.ContainsKey("NCC_RAZOR_PAGE_PROPERTY_LOGGER")) { httpContext.Items["NCC_RAZOR_PAGE_PROPERTY_LOGGER"] = _logger; } else { httpContext.Items.Add("NCC_RAZOR_PAGE_PROPERTY_LOGGER", _logger); } var typeInfo = typeof(T).GetTypeInfo(); //actionContext.ActionDescriptor.ActionName = "Index"; //actionContext.ActionDescriptor.ControllerName = "Home"; actionContext.ActionDescriptor.DisplayName = typeInfo.Module.Name; actionContext.ActionDescriptor.ControllerTypeInfo = typeInfo; using (var sw = new StringWriter()) { var viewResult = _razorViewEngine.FindView(actionContext, viewName, false); if (viewResult.View == null) { throw new ArgumentNullException($"{viewName} does not match any available view"); } var viewDictionary = new ViewDataDictionary(new EmptyModelMetadataProvider(), new ModelStateDictionary()) { Model = model }; var viewContext = new ViewContext( actionContext, viewResult.View, viewDictionary, new TempDataDictionary(actionContext.HttpContext, _tempDataProvider), sw, new HtmlHelperOptions() ); await viewResult.View.RenderAsync(viewContext); return(sw.ToString()); } } catch (Exception ex) { _logger.LogError(ex.Message, ex); } return(string.Empty); }
public Language(IConfiguration config, NccLanguageDetector languageDetector) { _languageDetector = languageDetector; _config = config; }
public LanguageView(NccLanguageDetector nccLanguageDetector) { _nccLanguageDetector = nccLanguageDetector; }
public LanguageEnabledAnchorTagHelper(IHttpContextAccessor httpContextAccessor, NccLanguageDetector nccLanguageDetector) { _httpContextAccessor = httpContextAccessor; _nccLanguageDetector = nccLanguageDetector; }
public string RenderToString(Type controllerType, string viewName, object model) { var errorMessage = ""; try { var httpContext = _httpContextAccessor.HttpContext; //new DefaultHttpContext { RequestServices = _serviceProvider }; var routeData = httpContext.GetRouteData(); var cad = new ControllerActionDescriptor(); var ac = new ActionContext(httpContext, httpContext.GetRouteData(), cad); var actionContext = new ControllerContext(ac); _nccLanguageDetector = new NccLanguageDetector(_httpContextAccessor); var language = _nccLanguageDetector.GetCurrentLanguage(); _nccTranslator = new NccTranslator(language); if (httpContext.Items.ContainsKey("NCC_RAZOR_PAGE_PROPERTY_CURRENT_LANGUAGE")) { httpContext.Items["NCC_RAZOR_PAGE_PROPERTY_CURRENT_LANGUAGE"] = language; } else { httpContext.Items.Add("NCC_RAZOR_PAGE_PROPERTY_CURRENT_LANGUAGE", language); } if (httpContext.Items.ContainsKey("NCC_RAZOR_PAGE_PROPERTY_TRANSLATOR")) { httpContext.Items["NCC_RAZOR_PAGE_PROPERTY_TRANSLATOR"] = _nccTranslator; } else { httpContext.Items.Add("NCC_RAZOR_PAGE_PROPERTY_TRANSLATOR", _nccTranslator); } if (httpContext.Items.ContainsKey("NCC_RAZOR_PAGE_PROPERTY_LOGGER")) { httpContext.Items["NCC_RAZOR_PAGE_PROPERTY_LOGGER"] = _logger; } else { httpContext.Items.Add("NCC_RAZOR_PAGE_PROPERTY_LOGGER", _logger); } var typeInfo = controllerType.GetTypeInfo(); var actionName = typeInfo.DeclaredMethods?.FirstOrDefault()?.Name ?? "Index"; actionContext.ActionDescriptor.ActionName = actionName; actionContext.ActionDescriptor.ControllerName = typeInfo.Name; actionContext.ActionDescriptor.DisplayName = actionName; actionContext.ActionDescriptor.ControllerTypeInfo = typeInfo; using (var sw = new StringWriter()) { var viewResult = _razorViewEngine.FindView(actionContext, viewName, false); if (viewResult.View == null) { throw new ArgumentNullException($"{viewName} does not match any available view"); } var viewDictionary = new ViewDataDictionary(new EmptyModelMetadataProvider(), new ModelStateDictionary()) { Model = model }; var viewContext = new ViewContext( actionContext, viewResult.View, viewDictionary, new TempDataDictionary(actionContext.HttpContext, _tempDataProvider), sw, new HtmlHelperOptions() ); viewResult.View.RenderAsync(viewContext).Wait(); var viewContent = sw.ToString(); //For showing which view file finally used for rendering. viewContent += $"<!-- View: {viewResult.View.Path}-->"; return(viewContent); } } catch (Exception ex) { _logger.LogError(ex.Message, ex); if (GlobalContext.HostingEnvironment.EnvironmentName.Contains("Development")) { errorMessage = $"<p style='color:red;'> {ex.Message}</p>";; } else { errorMessage = $"<p style='color:red;'> Error in widget {viewName}</p>";; } } return(errorMessage); }