HttpLanguageHeaderValue Class

Represents the value of an HTTP Accept-Language header.

Summary

The HttpLanguageHeaderValue class provides a way to represent and manipulate the value of an HTTP Accept-Language header. This header is used by clients to indicate the natural language preferred by the user, such as "en-US" for American English or "fr-FR" for French.

This class allows for parsing, creating, and modifying language tag values, including support for quality values (q-values) to specify the preference weight of different languages.

Inheritance

Constructors

public HttpLanguageHeaderValue(string value)

Initializes a new instance of the HttpLanguageHeaderValue class with the specified string value.

Parameters:
value
A that represents the language tag value.
public HttpLanguageHeaderValue(string language, double quality)

Initializes a new instance of the HttpLanguageHeaderValue class with the specified language tag and quality value.

Parameters:
language
A that represents the language tag (e.g., "en-US").
quality
A that represents the quality value (q-value), ranging from 0.0 to 1.0.

Properties

public string Language

Gets the language tag of the header value.

Example: "en-US", "fr"

public double Quality

Gets the quality value (q-value) of the header value. This indicates the preference weight of the language. A value of 1.0 is the highest preference.

Default value is 1.0 if not specified.

Methods

public static HttpLanguageHeaderValue Parse(string value)

Parses a string representation of an HTTP language header value.

Parameters:
value
The string to parse.
Returns:
An instance of HttpLanguageHeaderValue parsed from the string.
Throws:
ArgumentNullException
If value is null.
FormatException
If value is not a valid HTTP language header value.
public override string ToString()

Converts the current object to its string representation.

Returns:
A string that represents the current object.

Examples

The following examples demonstrate how to create and use the HttpLanguageHeaderValue class.

Creating a basic language header

var englishUS = new HttpLanguageHeaderValue("en-US");
Console.WriteLine(englishUS.ToString()); // Output: en-US
Console.WriteLine(englishUS.Language); // Output: en-US
Console.WriteLine(englishUS.Quality); // Output: 1

Creating a language header with a quality value

var frenchFR = new HttpLanguageHeaderValue("fr-FR", 0.8);
Console.WriteLine(frenchFR.ToString()); // Output: fr-FR;q=0.8
Console.WriteLine(frenchFR.Language); // Output: fr-FR
Console.WriteLine(frenchFR.Quality); // Output: 0.8

Parsing a language header string

string headerString = "es;q=0.7,en;q=0.9";
HttpLanguageHeaderValue spanish = HttpLanguageHeaderValue.Parse("es;q=0.7");
Console.WriteLine(spanish.ToString()); // Output: es;q=0.7

See Also