Exemplo n.º 1
0
        public static bool SetContentLanguageHeader(this System.Web.HttpContextBase context)
        {
            // Enumerate the possible user languages for the request. For any that have provided
            // a resource, add them to the header value.
            StringBuilder sb = new StringBuilder();

            LanguageItem[] langitems = context.GetRequestUserLanguages();
            foreach (LanguageItem langUser in langitems)
            {
                if (langUser.LanguageTag == null)
                {
                    continue;
                }
                if (langUser.UseCount > 0)
                {
                    if (sb.Length > 0)
                    {
                        sb.Append(",");
                    }
                    sb.Append(langUser.LanguageTag.ToString());
                }
            }
            if (sb.Length == 0)
            {
                return(false);
            }
            context.Response.AppendHeader("Content-Language", sb.ToString());
            return(true);
        }
Exemplo n.º 2
0
        public static string ParseAndTranslate(this System.Web.HttpContextBase context, string entity)
        {
            // For impl. notes see ResponseFilter.Flush().
            //
            //
            var nuggetLocalizer = LocalizedApplication.Current.NuggetLocalizerForApp;

            if (nuggetLocalizer != null)
            {
                entity = LocalizedApplication.Current.NuggetLocalizerForApp.ProcessNuggets(
                    entity,
                    context.GetRequestUserLanguages());
            }
            //
            if (UrlLocalizer.UrlLocalizationScheme != UrlLocalizationScheme.Void)
            {
                var earlyUrlLocalizer = LocalizedApplication.Current.EarlyUrlLocalizerForApp;
                if (earlyUrlLocalizer != null)
                {
                    entity = earlyUrlLocalizer.ProcessOutgoing(
                        entity,
                        context.GetPrincipalAppLanguageForRequest().ToString(),
                        context);
                }
            }
            //
            return(entity);
        }
Exemplo n.º 3
0
        public static LanguageTag ChooseAppLanguage(this System.Web.HttpContextBase context, IEnumerable <LanguageTag> AppLanguages)
        {
            string text;

            return(LanguageMatching.MatchLists(
                       context.GetRequestUserLanguages(),
                       AppLanguages,
                       null,
                       null,
                       out text));
        }
Exemplo n.º 4
0
        public static string GetText(
            this System.Web.HttpContextBase context,
            string msgid,
            string msgcomment,
            bool allowLookupWithHtmlDecodedMsgId = true)
        {
            // Relay on to the current text localizer for the appdomain.
            LanguageTag lt;

            return(LocalizedApplication.Current.TextLocalizerForApp.GetText(
                       allowLookupWithHtmlDecodedMsgId,
                       msgid,
                       msgcomment,
                       context.GetRequestUserLanguages(),
                       out lt));
        }
Exemplo n.º 5
0
        public static LanguageTag GetInferredLanguage(this System.Web.HttpContextBase context)
        {
            // langtag = best match between
            // 1. Inferred user languages (cookie and Accept-Language header)
            // 2. App Languages.
            LanguageTag lt = null;

            System.Web.HttpCookie cookie_langtag = context.Request.Cookies.Get(LocalizedApplication.Current.CookieName);
            if (cookie_langtag != null)
            {
                lt = LanguageHelpers.GetMatchingAppLanguage(cookie_langtag.Value);
            }
            if (lt == null)
            {
                lt = LanguageHelpers.GetMatchingAppLanguage(context.GetRequestUserLanguages());
            }
            if (lt == null)
            {
                throw new InvalidOperationException("Expected GetRequestUserLanguages to fall back to default language.");
            }
            return(lt);
        }
Exemplo n.º 6
0
        public override void Flush()
        {
            if (m_stagingBuffer == null)
            {
                return;
            }

            DebugHelpers.WriteLine("ResponseFilter::Flush");

            Byte[] buf = m_stagingBuffer.GetBuffer();

            //If the buffer holds compressed content then we allow the original output stream to be used because we don't try to modify compressed streams
            if (m_streamIsCompressed)
            {
                DebugHelpers.WriteLine("ResponseFilter::Flush -- skipping compressed content");
                m_outputStream.Flush();
                return;
            }

            // Convert byte array into string.
            Encoding enc    = m_httpContext.Response.ContentEncoding;
            string   entity = enc.GetString(buf, 0, (int)m_stagingBuffer.Length);

            // Prep for special BOM handling.
            // NB: at present we only support UTF-8 for this logic.
            //bool utf8WithoutBom = enc is UTF8Encoding && !buf.IsTextWithBom_Utf8();
            // #86 -- disabled this BOM handling for now as it doesn't seem to help.
            // Furthermore, it appears that the Encoding instance returned by
            // Response.ContentEncoding above is correctly configured to omit
            // BOM or not from its GetByte method, depending on whether or not
            // the response buffer has a BOM in it or not (for instance, see the
            // ctor of UTF8Encoding that takes a bool for this).

            // Buffer no longer required so release memory.
            m_stagingBuffer.Dispose();
            m_stagingBuffer = null;
            buf             = null;

            // Translate any embedded messages aka 'nuggets'.
            if (m_nuggetLocalizer != null)
            {
                var  page            = m_httpContext.Handler as System.Web.UI.Page;
                bool isScriptManager = false;
                if (page != null)
                {
                    var sm = System.Web.UI.ScriptManager.GetCurrent(page);
                    if (sm != null && sm.IsInAsyncPostBack)
                    {
                        isScriptManager = true;
                    }
                }
                //if async postback
                if (page != null && page.IsPostBack && isScriptManager && !String.IsNullOrEmpty(entity) && !String.IsNullOrEmpty(entity.Replace("\r", "").Split('\n')[0]))  //#178
                {
                    var asyncPostbackParser = new AsyncPostbackParser(entity);
                    var types = LocalizedApplication.Current.AsyncPostbackTypesToTranslate.Split(new char[] { ',' });
                    foreach (var type in types)
                    {
                        asyncPostbackParser.GetSections(type).ForEach(section => {
                            section.Content = m_nuggetLocalizer.ProcessNuggets(
                                section.Content,
                                m_httpContext.GetRequestUserLanguages());
                        });
                    }
                    entity = asyncPostbackParser.ToString();
                }
                else
                {
                    entity = m_nuggetLocalizer.ProcessNuggets(
                        entity,
                        m_httpContext.GetRequestUserLanguages());
                }
            }

            // If Early Localization is enabled, we balance that here with Late URL Localization.
            // The goal is to localize same-host URLs in the entity body and so save a redirect
            // on subsequent requests to those URLs by the user-agent (Early URL Localization).
            // We patch all URLs in the entity which are:
            //  1. same-host
            //  2. are not already localized
            //  3. pass any custom filtering
            // Examples of attributes containing urls include:
            //   <script src="..."> tags
            //   <img src="..."> tags
            //   <a href="..."> tags
            //   <link href="..."> tags
            if (m_earlyUrlLocalizer != null)
            {
                entity = m_earlyUrlLocalizer.ProcessOutgoing(
                    entity,
                    m_httpContext.GetPrincipalAppLanguageForRequest().ToString(),
                    m_httpContext);
            }

            //DebugHelpers.WriteLine("ResponseFilter::Write -- entity:\n{0}", entity);

            // Render the string back to an array of bytes.
            buf = enc.GetBytes(entity);
            enc = null; // release memory asap.
            int count = buf.Length;

            // Prep to skip any BOM if it wasn't originally there.
            // NB: at present we only support UTF-8 for this logic.
            int skip = 0;

            //if (utf8WithoutBom && buf.IsTextWithBom_Utf8()) {
            //    skip = 3; }
            // #86 -- see matching comment above.

            // Forward data on to the original response stream.
            m_outputStream.Write(buf, skip, count - skip);

            // Complete the write.
            m_outputStream.Flush();
        }