Skip to content

Commit

Permalink
added delegate to make the GetRequestUserLanguage more extensible
Browse files Browse the repository at this point in the history
  • Loading branch information
Jandev committed Aug 2, 2016
1 parent 086ad9c commit 4d4e639
Showing 1 changed file with 21 additions and 19 deletions.
40 changes: 21 additions & 19 deletions src/i18n/Helpers/HttpContextExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -194,29 +194,31 @@ public static LanguageItem[] GetRequestUserLanguages(this System.Web.HttpContext
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.
context.Items["i18n.UserLanguages"]
= UserLanguages = GetRequestUserLanguagesImplementation(context);
// 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;
}

/// <summary>
/// Add a Content-Language HTTP header to the response, based on any languages
/// that have provided resources during the request.
/// </summary>
/// <param name="context">Context of the current request.</param>
/// <returns>
/// true if header added; false if no languages provided content during the request and
/// so no header was added.
/// </returns>
public static bool SetContentLanguageHeader(this System.Web.HttpContext context)
public delegate LanguageItem[] GetRequestUserLanguagesProc(System.Web.HttpContextBase context);

public static GetRequestUserLanguagesProc GetRequestUserLanguagesImplementation { get; set; } = (context) => LanguageItem.ParseHttpLanguageHeader(context.Request.Headers["Accept-Language"]);

/// <summary>
/// Add a Content-Language HTTP header to the response, based on any languages
/// that have provided resources during the request.
/// </summary>
/// <param name="context">Context of the current request.</param>
/// <returns>
/// true if header added; false if no languages provided content during the request and
/// so no header was added.
/// </returns>
public static bool SetContentLanguageHeader(this System.Web.HttpContext context)
{
return context.GetHttpContextBase().SetContentLanguageHeader();
}
Expand Down

0 comments on commit 4d4e639

Please sign in to comment.