/// <summary> /// Adds a StatusCodePages middleware to the pipeline. Specifies that the response body should be generated by /// re-executing the request pipeline using an alternate localized path. This path may contain a '{0}' placeholder /// of the culture and the '{1}' placeholder the status code. /// </summary> /// <param name="app">App builder.</param> /// <param name="localizedPathFormat">Format pattern with two placeholders: the culture and the status code.</param> /// <param name="queryFormat">Query format</param> /// <returns></returns> public static IApplicationBuilder UseLocalizedStatusCodePagesWithReExecute( this IApplicationBuilder app, string localizedPathFormat, string?queryFormat = default) { if (app == null) { throw new ArgumentNullException(nameof(app)); } return(app.UseStatusCodePages(async context => { var originalPath = context.HttpContext.Request.Path; var originalQueryString = context.HttpContext.Request.QueryString; // Store the original paths so the app can check it. context.HttpContext.Features.Set <IStatusCodeReExecuteFeature>(new StatusCodeReExecuteFeature() { OriginalPathBase = context.HttpContext.Request.PathBase.Value, OriginalPath = originalPath.Value, OriginalQueryString = originalQueryString.HasValue ? originalQueryString.Value : null, }); var firstSegment = Regex.Match(originalPath, @"/([^/]+)/"); var segmentValue = firstSegment.Groups[1]?.Value; string?culture = default; if (firstSegment.Success && !string.IsNullOrWhiteSpace(segmentValue)) { var currentSiteName = SiteContext.CurrentSiteName; var siteInfoIdentifier = new SiteInfoIdentifier(currentSiteName); var defaultCulture = SettingsKeyInfoProvider.GetSettingsKeyInfo("CMSDefaultCultureCode", siteInfoIdentifier)?.KeyValue; culture = CultureSiteInfoProvider.IsCultureOnSite(segmentValue, currentSiteName) ? segmentValue : defaultCulture?.ToLowerInvariant(); } var cultureRouteValue = culture ?? "en-us"; var newPath = new PathString( string.Format(CultureInfo.InvariantCulture, localizedPathFormat, cultureRouteValue, context.HttpContext.Response.StatusCode)); var formatedQueryString = queryFormat == null ? null : string.Format(CultureInfo.InvariantCulture, queryFormat, context.HttpContext.Response.StatusCode); var newQueryString = queryFormat == null ? QueryString.Empty : new QueryString(formatedQueryString); await ReExecuteRequest(context, originalPath, originalQueryString, newPath, newQueryString); })); }
public static SiteCulture?ToSiteCulture(this CultureInfo cultureInfo) { var siteName = SiteContext.CurrentSiteName; if (cultureInfo != null && CultureSiteInfoProvider.IsCultureOnSite(cultureInfo.Name, siteName)) { var culture = CultureSiteInfoProvider .GetSiteCultures(siteName) .FirstOrDefault(culture => culture.CultureCode.Equals(cultureInfo.Name, StringComparison.InvariantCulture)); if (culture != null) { return(SiteCultureRepository.MapDtoProperties(culture, siteName)); } } return(null); }
/// <summary> /// Page load event handling. /// </summary> /// <param name="sender">Sender object</param> /// <param name="e">Event argument</param> protected void Page_Load(object sender, EventArgs e) { if (siteInfo == null) { return; } HandleReAssigningCulture(); Control.Visible = true; bool multilingual = LicenseHelper.CheckFeature(URLHelper.GetDomainName(siteInfo.DomainName), FeatureEnum.Multilingual); bool cultureOnSite = CultureSiteInfoProvider.IsCultureOnSite(CultureHelper.GetDefaultCultureCode(siteInfo.SiteName), siteInfo.SiteName); if (!multilingual && !cultureOnSite) { Control.Visible = false; // Add link that assign the default content culture to the site LocalizedHyperlink linkButton = new LocalizedHyperlink { ResourceString = "sitecultures.assigntodefault", NavigateUrl = "javascript:" + ControlsHelper.GetPostBackEventReference(Control.Page, ASSIGN_ARGUMENT_NAME) + ";" }; Control.Parent.Controls.Add(linkButton); } else { // Redirect only if cultures not exceeded => to be able to unassign if (!CultureSiteInfoProvider.LicenseVersionCheck(siteInfo.DomainName, FeatureEnum.Multilingual, ObjectActionEnum.Edit)) { LicenseHelper.CheckFeatureAndRedirect(URLHelper.GetDomainName(siteInfo.DomainName), FeatureEnum.Multilingual); } } // Get the active cultures from DB var cultureIds = GetCurrentCultureIds(); if (cultureIds.Count > 0) { currentValues = TextHelper.Join(";", cultureIds); } }
/// <summary> /// Determines whether the URL parameter contains an allowed culture name for this constraint. /// </summary> /// <param name="httpContext">Object that encapsulates information about the HTTP request.</param> /// <param name="route">Object that this constraint belongs to.</param> /// <param name="parameterName">Name of the parameter that is being checked.</param> /// <param name="values">Object that contains the parameters for the URL.</param> /// <param name="routeDirection">Object that indicates whether the constraint check is being performed when an incoming request is being handled or when a URL is being generated.</param> /// <returns>True if the URL parameter contains an allowed culture name; otherwise, false.</returns> public bool Match(HttpContextBase httpContext, Route route, string parameterName, RouteValueDictionary values, RouteDirection routeDirection) { string cultureCodeName = values[parameterName]?.ToString(); return(CultureSiteInfoProvider.IsCultureOnSite(cultureCodeName, SiteContext.CurrentSiteName)); }