Beispiel #1
0
 public ActionResult SetLanguage(string langtag, string returnUrl)
 {
     // If valid 'langtag' passed.
     i18n.LanguageTag lt = i18n.LanguageTag.GetCachedInstance(langtag);
     if (lt.IsValid())
     {
         // Set persistent cookie in the client to remember the language choice.
         Response.Cookies.Add(new System.Web.HttpCookie("i18n.langtag")
         {
             Value    = lt.ToString(),
             HttpOnly = true,
             Expires  = DateTime.UtcNow.AddYears(1)
         });
     }
     // Owise...delete any 'language' cookie in the client.
     else
     {
         var cookie = Response.Cookies["i18n.langtag"];
         if (cookie != null)
         {
             cookie.Value   = null;
             cookie.Expires = DateTime.UtcNow.AddMonths(-1);
         }
     }
     // Update PAL setting so that new language is reflected in any URL patched in the
     // response (Late URL Localization).
     HttpContext.SetPrincipalAppLanguageForRequest(lt);
     // Patch in the new langtag into any return URL.
     if (returnUrl.IsSet())
     {
         returnUrl = LocalizedApplication.Current.UrlLocalizerForApp.SetLangTagInUrlPath(HttpContext, returnUrl, UriKind.RelativeOrAbsolute, lt == null ? null : lt.ToString()).ToString();
     }
     //Redirect user agent as approp.
     return(this.Redirect(returnUrl));
 }
Beispiel #2
0
 protected void Application_BeginRequest(object source, EventArgs e)
 {
     i18n.LanguageTag lt = i18n.LanguageTag.GetCachedInstance("en");
     if (lt.IsValid())
     {
         Response.Cookies.Add(new HttpCookie("i18n.langtag")
         {
             Value    = lt.ToString(),
             HttpOnly = true,
             Expires  = DateTime.UtcNow.AddYears(1)
         });
     }
     else
     {
         var cookie = Response.Cookies["i18n.langtag"];
         if (cookie != null)
         {
             cookie.Value   = null;
             cookie.Expires = DateTime.UtcNow.AddMonths(-1);
         }
     }
     if (HttpContext.Current.Request.RawUrl.ToLower().Contains("content") || HttpContext.Current.Request.RawUrl.ToLower().Contains("style"))
     {
         HttpContext.Current.Response.AddHeader("Access-Control-Allow-Origin", "*");
     }
     HttpContext.Current.SetPrincipalAppLanguageForRequest(lt);
 }
Beispiel #3
0
        public ActionResult SetLanguage(string langtag, string returnUrl)
        {
            //if valid "langtag" passed
            i18n.LanguageTag lt = i18n.LanguageTag.GetCachedInstance(langtag);
            if (lt.IsValid())
            {
                //Set persistent coockie in the client to remember the language choice.
                Response.Cookies.Add(
                    new HttpCookie("i18n.langtag")
                {
                    Value    = lt.ToString(),
                    HttpOnly = true,
                    Expires  = DateTime.UtcNow.AddYears(1)
                });
            }
            //delete any language coockie in the client.
            else
            {
                var coockie = Response.Cookies["i18n.langtag"];
                if (coockie != null)
                {
                    coockie.Value   = null;
                    coockie.Expires = DateTime.UtcNow.AddMonths(-1);
                }
            }

            System.Web.HttpContext.Current.SetPrincipalAppLanguageForRequest(lt);
            //if(returnUrl.IsSet())
            //{
            //    returnUrl = LocalizedApplication.Current.UrlLocalizerForApp.SetLangTagInUrlPath(HttpContext, returnUrl, UriKind.RelativeOrAbsolute, lt == null ? null : lt.ToString()).ToString();
            //}

            return(Redirect(returnUrl));
        }
Beispiel #4
0
        private void Application_SetAppLangulage(object sender, EventArgs e)
        {
            var langCookie = HttpContext.Current.Request.Cookies["Mcd_AM.LangTag"];

            if (langCookie != null)
            {
                var langtag = langCookie.Value;

                i18n.LanguageTag lt = i18n.LanguageTag.GetCachedInstance(langtag);
                if (lt.IsValid())
                {
                    // Set persistent cookie in the client to remember the language choice.
                    HttpContext.Current.Response.Cookies.Add(new HttpCookie("Mcd_AM.LangTag")
                    {
                        Value    = lt.ToString(),
                        HttpOnly = true,
                        Expires  = DateTime.UtcNow.AddYears(1)
                    });
                }
                // Owise...delete any 'language' cookie in the client.
                else
                {
                    var cookie = HttpContext.Current.Response.Cookies["Mcd_AM.LangTag"];
                    if (cookie != null)
                    {
                        cookie.Value   = null;
                        cookie.Expires = DateTime.UtcNow.AddMonths(-1);
                    }
                }
                // Update PAL setting so that new language is reflected in any URL patched in the
                // response (Late URL Localization).
                HttpContext.Current.SetPrincipalAppLanguageForRequest(lt);
            }
        }
Beispiel #5
0
        /// <summary>
        /// Implements the Early Url Localization logic.
        /// <see href="https://docs.google.com/drawings/d/1cH3_PRAFHDz7N41l8Uz7hOIRGpmgaIlJe0fYSIOSZ_Y/edit?usp=sharing"/>
        /// </summary>
        public void ProcessIncoming(
            System.Web.HttpContextBase context)
        {
            // Is URL explicitly excluded from localization?
            if (!m_urlLocalizer.FilterIncoming(context.Request.Url))
            {
                return;
            }             // YES. Continue handling request.

            bool allowRedirect =
                context.Request.HttpMethod.Equals("GET", StringComparison.OrdinalIgnoreCase) ||
                context.Request.HttpMethod.Equals("HEAD", StringComparison.OrdinalIgnoreCase);

            // NO. Is request URL localized?
            string urlNonlocalized;
            string langtag = m_urlLocalizer.ExtractLangTagFromUrl(context, context.Request.RawUrl, UriKind.Relative, true, out urlNonlocalized);

            if (langtag == null)
            {
                // NO.
                // langtag = best match between
                // 1. Inferred user languages (cookie and Accept-Language header)
                // 2. App Languages.
                LanguageTag lt = context.GetInferredLanguage();

                // If redirection allowed...redirect user agent (browser) to localized URL.
                // The principle purpose of this redirection is to ensure the browser is showing the correct URL
                // in its address field.
                if (allowRedirect)
                {
                    RedirectWithLanguage(context, context.Request.RawUrl, lt.ToString(), m_urlLocalizer);
                    return;
                }

                // Otherwise, handle the request under the language infered above but without doing the redirect.
                // NB: this will mean that the user agent (browser) won't have the correct URL displayed;
                // however, this typically won't be an issue because we are talking about POST, PUT and DELETE methods
                // here which are typically not shown to the user.
                else
                {
                    context.SetPrincipalAppLanguageForRequest(lt);
                    return; // Continue handling request.
                }
            }

            // YES. Does langtag EXACTLY match an App Language?
            LanguageTag appLangTag = LanguageHelpers.GetMatchingAppLanguage(langtag);

            if (appLangTag.IsValid() &&
                appLangTag.Equals(langtag))
            {
                // YES. Establish langtag as the PAL for the request.
                context.SetPrincipalAppLanguageForRequest(appLangTag);

                // Rewrite URL for this request.
                context.RewritePath(urlNonlocalized);

                // Continue handling request.
                return;
            }

            // NO. Does langtag LOOSELY match an App Language?
            else if (appLangTag.IsValid() &&
                     !appLangTag.Equals(langtag))
            {
                // YES. Localize URL with matching App Language.
                // Conditionally redirect user agent to localized URL.
                if (allowRedirect)
                {
                    RedirectWithLanguage(context, urlNonlocalized, appLangTag.ToString(), m_urlLocalizer);
                    return;
                }
            }
            // NO. Do nothing to URL; expect a 404 which corresponds to language not supported.
            // Continue handling request.
        }
        /// <summary>
        /// Sessions the start.
        /// </summary>
        /// <param name="sender">Sender.</param>
        /// <param name="e">E.</param>
        private void Session_Start(object sender, EventArgs e)
        {
            string languageBrowser = null;

            try
            {
                if (Request.IsAuthenticated)
                {
                    UserService _userService  = new UserService();
                    var         ConnectedUser = _userService.GetUserByUserName(User.Identity.Name);
                    if (ConnectedUser != null)
                    {
                        languageBrowser = ConnectedUser.Language?.Code;
                    }
                }
                else
                {
                    Session[CommonsConst.Const.UserSession] = null;
                }
                if (String.IsNullOrEmpty(languageBrowser))
                {
                    string[] languages = Request?.UserLanguages;
                    if (languages != null && languages.Length > 0)
                    {
                        string Favoritelanguage = languages[0];
                        languageBrowser = CommonsConst.Const.DefaultCulture;
                        CategoryService _categoryService = new CategoryService();
                        var             ListLanguages    = _categoryService.GetSelectionList(CommonsConst.CategoryTypes.Language);
                        if (ListLanguages != null && ListLanguages.Count > 0)
                        {
                            foreach (var Language in ListLanguages)
                            {
                                if (Language.Code == Favoritelanguage || Favoritelanguage.IndexOf(Language.Code + "-") > -1)
                                {
                                    languageBrowser = Language.Code;
                                    break;
                                }
                            }
                        }
                    }
                }



                if (!String.IsNullOrEmpty(languageBrowser))
                {
                    Session[CommonsConst.Const.WebsiteLanguageSession] = languageBrowser;
                    i18n.LanguageTag lt = i18n.LanguageTag.GetCachedInstance(languageBrowser);
                    Response.Cookies.Add(new HttpCookie(CommonsConst.Const.i18nlangtag)
                    {
                        Value    = lt.ToString(),
                        HttpOnly = true,
                        Expires  = DateTime.UtcNow.AddYears(1)
                    });


                    System.Web.HttpContext.Current.SetPrincipalAppLanguageForRequest(lt);
                }
            }
            catch (Exception ex)
            {
                Commons.Logger.GenerateError(ex, System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
            }
        }