Esempio n. 1
0
 static HttpContextExtensions()
 {
     GetRequestUserLanguagesImplementation = (context) =>
     {
         return(LanguageItem.ParseHttpLanguageHeader(context.Request.Headers["Accept-Language"]));
         // NB: originally we passed LocalizedApplication.Current.DefaultLanguageTag
         // here as the second parameter i.e. to specify the PAL. However, this was
         // found to be incorrect when operating i18n with EarlyUrlLocalization disabled,
         // as SetPrincipalAppLanguageForRequest was not being called, that normally
         // overwriting the PAL set erroneously here.
     };
 }
Esempio n. 2
0
        public static void SetPrincipalAppLanguageForRequest(this HttpContextBase context, ILanguageTag pal, bool updateThreadCulture = true)
        {
            // The PAL is stored as the first item in the UserLanguages array (with Quality set to 2).
            //
            LanguageItem[] UserLanguages = GetRequestUserLanguages(context);
            UserLanguages[0] = new LanguageItem(pal, LanguageItem.PalQualitySetting, 0);

            // Run through any handlers installed for this event.
            if (LocalizedApplication.Current.SetPrincipalAppLanguageForRequestHandlers != null)
            {
                foreach (LocalizedApplication.SetLanguageHandler handler in LocalizedApplication.Current.SetPrincipalAppLanguageForRequestHandlers.GetInvocationList())
                {
                    handler(context, pal);
                }
            }
        }
Esempio n. 3
0
 public static LanguageItem[] GetRequestUserLanguages(this HttpContextBase context)
 {
     // Determine UserLanguages.
     // This value is created afresh first time this method is called per request,
     // and cached for the request's remaining calls to this method.
     LanguageItem[] UserLanguages = context.Items["i18n.UserLanguages"] as LanguageItem[];
     if (UserLanguages == null)
     {
         // Construct UserLanguages list and cache it for the rest of the request.
         context.Items["i18n.UserLanguages"]
               = UserLanguages
               = LanguageItem.ParseHttpLanguageHeader(
                     context.Request.Headers["Accept-Language"]);
         // NB: originally we passed LocalizedApplication.Current.DefaultLanguageTag
         // here as the second parameter i.e. to specify the PAL. However, this was
         // found to be incorrect when operating i18n with EarlyUrlLocalization disabled,
         // as SetPrincipalAppLanguageForRequest was not being called, that normally
         // overwriting the PAL set erroneously here.
     }
     return(UserLanguages);
 }
Esempio n. 4
0
 /// <summary>
 /// Attempts to match the passed language with an AppLanguage.
 /// </summary>
 /// <param name="langtag">The subject language to match, typically a UserLanguage.</param>
 /// <param name="maxPasses">
 /// 0 - allow exact match only
 /// 1 - allow exact match or default-region match only
 /// 2 - allow exact match or default-region match or script match only
 /// 3 - allow exact match or default-region match or script match or language match only
 /// 4 - allow exact match or default-region match or script or language match only, or failing return the default language.
 /// -1 to set to most tolerant (i.e. 4).
 /// </param>
 /// <returns>
 /// A language tag identifying an AppLanguage that will be the same as, or related langtag.
 /// </returns>
 public static LanguageTag GetMatchingAppLanguage(string langtag, int maxPasses = -1)
 {
     return(GetMatchingAppLanguage(LanguageItem.ParseHttpLanguageHeader(langtag), maxPasses));
 }
Esempio n. 5
0
 /// <summary>
 /// Helper for translating the passed string entity which may contain zero or more fully-formed nugget,
 /// which may be called when an HttpContext is not available e.g. in background jobs.
 /// </summary>
 /// <remarks>
 /// When translating entities when handling HTTP requests, the language to use is selected from the current UserLanguages value
 /// which is derived from the HttpContext (that is, the request headers). In a background job, however,
 /// we don't normally have an HttpContext. With this method, however, you can save the UserLanguages
 /// obtained from <see cref="i18n.HttpContextExtensions.ParseAndTranslate(System.Web.HttpContextBase, string)"/> when it is available,
 /// i.e. prior to scheduling the background job, and pass it through to the job and then on to this method.
 /// Note that the UserLanguages array value can be persisted as a string using the <see cref="LanguageItem.DehydrateLanguageItemsToString"/>
 /// helper and converted back to an array using the <see cref="LanguageItem.HydrateLanguageItemsFromString"/> helper.
 /// Note that this method does not support the localization of URLs embedded in the entity.
 /// </remarks>
 /// <param name="userLanguages">
 /// A list of language preferences, sorted in order or preference (most preferred first)
 /// in compact string form.
 /// May be null/empty string in which case a a single-item language item array
 /// representing a null PAL is returned.
 /// Example values:
 ///     "fr-CA;q=1,fr;q=0.5"
 ///     "en-CA;q=2,de;q=0.5,en;q=1,fr-FR;q=0,ga;q=0.5"
 ///     "en-CA;q=1,de;q=0.5,en;q=1,fr-FR;q=0,ga;q=0.5"
 ///     "en-CA;q=1"
 ///     "?;q=2"
 ///     "?;q=2,de;q=0.5,en;q=1,fr-FR;q=0,ga;q=0.5"
 ///     ""
 /// See <see cref="HttpContextExtensions.GetRequestUserLanguagesAsString"/>.
 /// See <see cref="LanguageItem.DehydrateLanguageItemsToString"/>.
 /// </param>
 /// <param name="entity">
 /// Entity to be processed. E.g HTTP response entity or Javascript file.
 /// </param>
 /// <returns>
 /// Processed (and possibly modified) entity.
 /// </returns>
 /// <seealso cref="i18n.HttpContextExtensions.ParseAndTranslate(System.Web.HttpContextBase, string)"/>
 public static string ParseAndTranslate(string entity, string userLanguages = null)
 {
     return(ParseAndTranslate(entity, LanguageItem.HydrateLanguageItemsFromString(userLanguages)));
 }
Esempio n. 6
0
 /// <summary>
 /// Returns a string representing a collection of languages supported by the user-agent, in descending order
 /// of preference. The first item in the collection refers to any Principle Application Language (PAL)
 /// for the request determined by EarlyUrlLocalization (which calls SetPrincipalAppLanguageForRequest),
 /// or is null if EarlyUrlLocalization is disabled.
 /// </summary>
 /// <param name="context">Context of the current request.</param>
 /// <returns>
 /// Compact string representation of the language item array.
 /// Example values:
 ///     "fr-CA;q=1,fr;q=0.5"
 ///     "en-CA;q=2,de;q=0.5,en;q=1,fr-FR;q=0,ga;q=0.5"
 ///     "en-CA;q=1,de;q=0.5,en;q=1,fr-FR;q=0,ga;q=0.5"
 ///     "en-CA;q=1"
 ///     "?;q=2"
 ///     "?;q=2,de;q=0.5,en;q=1,fr-FR;q=0,ga;q=0.5"
 /// </returns>
 /// <remarks>
 /// This method is optimised such that the collection is built only once per request.
 /// </remarks>
 /// See <see cref="LanguageItem.ParseHttpLanguageHeader"/>.
 /// See <see cref="LanguageItem.ParseHttpLanguageHeader"/>.
 /// See <see cref="HttpContextExtensions.GetRequestUserLanguagesAsString"/>.
 /// See <see cref="LanguageItem.DehydrateLanguageItemsToString"/>.
 public static string GetRequestUserLanguagesAsString(this System.Web.HttpContextBase context)
 {
     return(LanguageItem.DehydrateLanguageItemsToString(GetRequestUserLanguages(context)));
 }