Skip to content

GetAcceptLanguages ​

Reads every language from the request Accept-Language header, ordered by the quality value the client assigned, highest first, counting a missing weight as 1. An entry weighted 0 is dropped rather than ranked last, since that is the client saying it will not accept that language, and so are the * wildcard and any malformed tag. Use GetAcceptLanguage when only the top preference matters, and see language negotiation for how the full list is walked.

Usage ​

csharp
using Microsoft.AspNetCore.Mvc;
using AlmightyShogun.AspNet.Localization;

[ApiController]
[Route("preferences")]
public sealed class PreferencesController : ControllerBase
{
    [HttpGet("languages")]
    public IActionResult Get()
    {
        IReadOnlyList<string> languages = Request.GetAcceptLanguages();

        return Ok(languages.Count > 0 ? languages : ["en"]);
    }
}

WARNING

Rejecting a malformed tag is a security boundary. Each value is used to build a filesystem path when message files are resolved, so an unvalidated header would allow directory traversal out of the messages directory.

Returns

The accepted languages in client preference order, without duplicates, or an empty list when the header is absent or names nothing that parses as a language tag.

Type signature ​

csharp
public IReadOnlyList<string> GetAcceptLanguages();

All packages are released under the MIT License.