public override RouteData GetRouteData(HttpContextBase httpContext) { RouteData routeData = base.GetRouteData(httpContext); if (routeData == null) { return(null); } foreach (KeyValuePair <string, object> pair in this.RouteValueTranslationProviders) { IRouteValueTranslationProvider translationProvider = pair.Value as IRouteValueTranslationProvider; if (translationProvider != null && routeData.Values.ContainsKey(pair.Key)) { RouteValueTranslation translation = translationProvider.TranslateToRouteValue( routeData.Values[pair.Key].ToString(), CultureInfo.CurrentCulture); routeData.Values[pair.Key] = translation.RouteValue; if (routeData.DataTokens[DetectedCultureKey] == null) { routeData.DataTokens.Add(DetectedCultureKey, translation.Culture); } if (this.SetDetectedCulture) { System.Threading.Thread.CurrentThread.CurrentCulture = translation.Culture; System.Threading.Thread.CurrentThread.CurrentUICulture = translation.Culture; } } } return(routeData); }
public override RouteData GetRouteData(HttpContextBase httpContext) { RouteData routeData = base.GetRouteData(httpContext); if (routeData == null) { return(null); } // > Determinar Cultura de la petición CultureInfo culture; try { culture = this.GetCultureFromString( (string)routeData.Values["lang"] ) ?? this.GetCultureFromString( (httpContext.Request.Cookies.Get("KMS.Culture") ?? new HttpCookie("KMS.Culture", null)).Value ) ?? this.GetCultureFromString( httpContext.Request.UserLanguages[0] ) ?? new CultureInfo("en-GB"); } catch { culture = new CultureInfo("en-GB"); } // > Verificar que la cultura esté catalogada como soportada if (!SupportedCultures.Cultures.Any(a => a.ToLower() == culture.Name.ToLower())) { culture = new CultureInfo(SupportedCultures.Cultures.First()); } // > Si {routeData} no contiene {lang}, redirigir a la URL que sí lo contiene if (routeData.Values["lang"] == null) { httpContext.Response.Redirect( new Uri( httpContext.Request.Url, "/" + culture.Name + httpContext.Request.Url.PathAndQuery ).AbsoluteUri, true ); httpContext.Response.End(); return(null); } // > Si {routeData} no coincide con {lang}, redirigir a la URL con {lang} if (((string)routeData.Values["lang"]).ToLower() != culture.Name.ToLower()) { string tmp = new Regex(@"/([a-zA-Z]{2})-([a-zA-Z]{2})").Replace( httpContext.Request.Url.AbsoluteUri, "/" + culture.Name ); httpContext.Response.Redirect( tmp, true ); httpContext.Response.End(); return(null); } // > Establecer cultura actual System.Threading.Thread.CurrentThread.CurrentCulture = culture; System.Threading.Thread.CurrentThread.CurrentUICulture = culture; // > Limpiar caché para que RegionInfo refleje cambios System.Threading.Thread.CurrentThread.CurrentCulture.ClearCachedData(); System.Threading.Thread.CurrentThread.CurrentUICulture.ClearCachedData(); // > Establecer cookie con Cultura actual httpContext.Response.Cookies.Set( new HttpCookie("KMS.Culture", CultureInfo.CurrentCulture.Name.ToLower()) ); // > Traducir otros elementos en la ruta foreach (KeyValuePair <string, object> pair in this.RouteValueTranslationProviders) { IRouteValueTranslationProvider translationProvider = pair.Value as IRouteValueTranslationProvider; if ( translationProvider != null && routeData.Values.ContainsKey(pair.Key) ) { RouteValueTranslation translation = translationProvider.TranslateToRouteValue( routeData.Values[pair.Key].ToString(), CultureInfo.CurrentCulture ); routeData.Values[pair.Key] = translation.RouteValue; } } // > Devolver info de la ruta return(routeData); }