/// <summary> /// Gets the current culture from the http request. /// </summary> /// <param name="request">The request.</param> /// <returns></returns> public static CultureInfo GetCurrentCulture(this HttpRequestBase request) { CultureInfo currentCulture = Thread.CurrentThread.CurrentCulture; return(LocalizationContext.TryGetCultureFromTwoLetterIsoCode( twoLetterISOLanguageName: currentCulture.TwoLetterISOLanguageName)); }
/// <summary> /// Gets the current UI culture from the http request. /// </summary> /// <param name="request">The request.</param> /// <param name="getFromBrowserIfNotFound">if set to <c>true</c> [get from browser if not found].</param> /// <returns></returns> public static CultureInfo GetCurrentUICulture(this HttpRequestBase request, bool getFromBrowserIfNotFound = true) { CultureInfo currentUICulture = Thread.CurrentThread.CurrentUICulture; // If not found try to get it from the browser if (getFromBrowserIfNotFound && currentUICulture == null) { currentUICulture = request.TryGetCultureFromBrowser(); } return(LocalizationContext.TryGetCultureFromTwoLetterIsoCode( twoLetterISOLanguageName: currentUICulture.TwoLetterISOLanguageName)); }
/// <summary> /// Gets the localized URL. /// </summary> /// <param name="content">The content.</param> /// <param name="language">The language.</param> /// <returns></returns> public static string GetLocalizedUrl(this IPublishedContent content, string language) { //TODO: Check if language is valid? System.Web.HttpContext httpContext = System.Web.HttpContext.Current; //TODO: Better way? Uri current = httpContext.Request.Url; //TODO: Better way? if (Routing.AliasUrlProvider.FindByUrlAliasEnabled == false) { //TODO: Should we add also here the "just" replacing way if alias url provider disabled? /de/page => /en/page? //TODO: Helper for adding url something like => current.AddUrl("/de/"); with handles the ensure thingy // Sometimes the URL contains the host (when hostnames are added) e.g. http://www.opten.ch/de // So the content.Url is already an absolute URL => no need the get the base url... Uri uri; bool withDomain = true; if (Uri.TryCreate(content.Url, UriKind.Absolute, out uri) == false) { // but we have to handle it, if there are no hostnames uri = new Uri(current.GetBaseUrl() + content.Url.EnsureStartsWith('/')); withDomain = false; // If we don't have any hostnames we don't want to return the domain } return(uri.GetUrlWithLanguage(language: language, withDomain: withDomain)); } Opten.Umbraco.Localization.Web.Routing.AliasUrlProvider urlProvider = new Routing.AliasUrlProvider(); CultureInfo culture = LocalizationContext.TryGetCultureFromTwoLetterIsoCode( twoLetterISOLanguageName: language); return(urlProvider.GetUrl( umbracoContext: UmbracoContext.Current, id: content.Id, current: current, mode: global::Umbraco.Web.Routing.UrlProviderMode.AutoLegacy, culture: culture)); }
/// <summary> /// Tries to get the culture from browser if not found default language will returned. /// </summary> /// <param name="request">The request.</param> /// <returns>Browser culture or default culture if not found.</returns> public static CultureInfo TryGetCultureFromBrowser(this HttpRequestBase request) { //TODO: Move to Opten.Common? CultureInfo defaultCulture = LocalizationContext.DefaultCulture; try { string browserCulture = (request.UserLanguages ?? Enumerable.Empty <string>()).FirstOrDefault(); if (string.IsNullOrWhiteSpace(browserCulture)) { browserCulture = Thread.CurrentThread.CurrentUICulture.Name; } return(LocalizationContext.TryGetCultureFromTwoLetterIsoCode( twoLetterISOLanguageName: browserCulture.GetTwoLetterISOCodeByName())); } catch (Exception ex) { LogHelper.Error <HttpRequestBase>("Couldn't get browser language -> return default language: " + defaultCulture, ex); return(defaultCulture); } }