PeterO.Mail.LanguageTags
## PeterO.Mail.LanguageTags
public static class LanguageTags
Contains methods for parsing and matching language tags.
Member Summary
[GetLanguageList(string)](#GetLanguageList_string)
- Parses a language list from a Content-Language header field.[GetRangeListWithQuality(string)](#GetRangeListWithQuality_string)
- Parses a language range list from an Accept-Language header field.[IsLanguageRange(string)](#IsLanguageRange_string)
- Returns whether the given string is a basic language range under RFC 4647.[IsLanguageRange(string, bool)](#IsLanguageRange_string_bool)
- Returns whether the given string is a basic or extended language range under RFC 4647.[IsPotentiallyValidLanguageTag(string)](#IsPotentiallyValidLanguageTag_string)
- Returns true if (1) the given string is a well-formed language tag under RFC 5646 (that is, the string follows the syntax given in section 2.[LanguageTagCase(string)](#LanguageTagCase_string)
- Sets the given language tag to the case combination recommended by RFC 5646.[LanguageTagFilter(System.Collections.Generic.IList, System.Collections.Generic.IList)](#LanguageTagFilter_System_Collections_Generic_IList_System_Collections_Generic_IList)
- Finds the language tags that match a priority list of basic language ranges.[LanguageTagFilter(System.Collections.Generic.IList, System.Collections.Generic.IList, bool, bool)](#LanguageTagFilter_System_Collections_Generic_IList_System_Collections_Generic_IList_bool_bool)
- Finds the language tags that match a priority list of language ranges.[LanguageTagLookup(string, System.Collections.Generic.IList, string)](#LanguageTagLookup_string_System_Collections_Generic_IList_string)
- Does a language tag lookup (under RFC 4647) for a matching language tag.[LanguageTagLookup(string, System.Collections.Generic.IList, string, bool)](#LanguageTagLookup_string_System_Collections_Generic_IList_string_bool)
- Does a language tag lookup (under RFC 4647) for a matching language tag.[LanguageTagLookup(System.Collections.Generic.IList, System.Collections.Generic.IList, string)](#LanguageTagLookup_System_Collections_Generic_IList_System_Collections_Generic_IList_string)
- Does a language tag lookup (under RFC 4647) for a matching language tag.[LanguageTagLookup(System.Collections.Generic.IList, System.Collections.Generic.IList, string, bool)](#LanguageTagLookup_System_Collections_Generic_IList_System_Collections_Generic_IList_string_bool)
- Does a language tag lookup (under RFC 4647) for a matching language tag.[MatchesLanguageTag(string, string)](#MatchesLanguageTag_string_string)
- Determines whether the given language tag matches the given language range.
public static System.Collections.Generic.IList GetLanguageList( string str);
Parses a language list from a Content-Language header field.
Parameters:
- str: A string following the syntax of a Content-Language header field (see RFC 3282). This is a comma-separated list of language tags. RFC 5322 comments (in parentheses) can appear. This parameter can be null.
Return Value:
A list of language tags. Returns an empty list if str is null or the empty string, or null if str syntactically invalid.
public static System.Collections.Generic.IList GetRangeListWithQuality( string str);
Parses a language range list from an Accept-Language header field.
Parameters:
- str: A string following the syntax of an Accept-Language header field (see RFC 3282). This is a comma-separated list of language ranges, with an optional “quality” after the language tag (examples include “en; q=0.5” or “de-DE”). RFC 5322 comments (in parentheses) can appear. This parameter can be null.
Return Value:
A list of language ranges with their associated qualities. The list will be sorted in descending order by quality; if two or more language ranges have the same quality, they will be sorted in the order in which they appeared in the given string. Returns null if str is null or syntactically invalid.
public static bool IsLanguageRange( string str);
Returns whether the given string is a basic language range under RFC 4647. Examples include “*”, “en-us”, and “fr”.
Parameters:
- str: The string to check. Can be null.
Return Value:
true
if the given string is a basic language range; otherwise, false
.
public static bool IsLanguageRange( string str, bool extended);
Returns whether the given string is a basic or extended language range under RFC 4647. Examples of basic (and extended) language ranges include “”, “en-us”, and “fr”. Examples of extended language ranges include “-de” and “it-*”.
Parameters:
-
str: The string to check. Can be null.
-
extended: Check whether the string is a basic language range if “false”, or an extended language range if “true”.
Return Value:
true
if the given string is a basic language range (depending on the extended
parameter); otherwise, false
.
### IsPotentiallyValidLanguageTag
public static bool IsPotentiallyValidLanguageTag( string str);
Returns true if (1) the given string is a well-formed language tag under RFC 5646 (that is, the string follows the syntax given in section 2.1 of that RFC), and (2) the language tag contains at most one extended language subtag, no variant subtags with the same value, and no extension singleton subtags with the same value.
Parameters:
- str: The string to check.
Return Value:
true
, if the string meets the conditions given in the summary, false
otherwise.
public static string LanguageTagCase( string str);
Sets the given language tag to the case combination recommended by RFC 5646. For example, “en-us” becomes “en-US”, and “zh-hant” becomes “zh-Hant”.
Parameters:
- str: A string of a language tag. Can be null.
Return Value:
A text string in the recommended case combination, or null if str is null.
public static System.Collections.Generic.IList LanguageTagFilter( System.Collections.Generic.IList ranges, System.Collections.Generic.IList languages);
Finds the language tags that match a priority list of basic language ranges.
Parameters:
-
ranges: A list of basic language ranges (see documentation for the “IsLanguageRange” method), which should be given in order of descending preference.
-
languages: A list of language tags, which should be given in order of descending preference.
Return Value:
A list of language tags that match the given range, in descending order of preference.
Exceptions:
-
System.ArgumentNullException: The parameter languages or ranges is null.
-
System.ArgumentException: The parameter ranges contains a value that is not a basic language range, or languages contains a value that is not a potentially valid language tag.
public static System.Collections.Generic.IList LanguageTagFilter( System.Collections.Generic.IList ranges, System.Collections.Generic.IList languages, bool extended, bool matchStarAtEnd);
Finds the language tags that match a priority list of language ranges.
Parameters:
-
ranges: A list of language ranges (see documentation for the “IsLanguageRange” method), which should be given in order of descending preference.
-
languages: A list of language tags, which should be given in order of descending preference.
-
extended: If true, the ranges in “ranges” are extended language ranges; otherwise, they are basic language ranges.
-
matchStarAtEnd: If true, treats any range equaling “*” as appearing at the end of the language priority list, no matter where it appears on that list.
Return Value:
A list of language tags that match the given range, in descending order of preference.
Exceptions:
-
System.ArgumentNullException: The parameter languages or ranges is null.
-
System.ArgumentException: The parameter ranges contains a value that is not a basic or extended language range, or languages contains a value that is not a potentially valid language tag.
public static string LanguageTagLookup( string range, System.Collections.Generic.IList languages, string defaultValue);
Does a language tag lookup (under RFC 4647) for a matching language tag.
Parameters:
-
range: A basic language range (see the documentation for “IsLanguageRange”).
-
languages: A list of language tags, which should be given in order of descending preference.
-
defaultValue: The value to return if no matching language tag was found.
Return Value:
The matching language tag, or the parameter defaultValue if there is no matching language tag.
Exceptions:
-
System.ArgumentNullException: The parameter languages is null.
-
System.ArgumentException: The parameter range is not a basic language range, or languages contains a value that is not a potentially valid language tag.
public static string LanguageTagLookup( string range, System.Collections.Generic.IList languages, string defaultValue, bool extended);
Does a language tag lookup (under RFC 4647) for a matching language tag.
Parameters:
-
range: A language range (see the documentation for “IsLanguageRange”).
-
languages: A list of language tags, which should be given in order of descending preference.
-
defaultValue: The value to return if no matching language tag was found.
-
extended: If true, “range” is an extended language range; otherwise, it’s a are basic language range.
Return Value:
The matching language tag, or the parameter defaultValue if there is no matching language tag.
Exceptions:
-
System.ArgumentNullException: The parameter languages is null.
-
System.ArgumentException: The parameter range is not a basic or extended language range, or languages contains a value that is not a potentially valid language tag.
public static string LanguageTagLookup( System.Collections.Generic.IList ranges, System.Collections.Generic.IList languages, string defaultValue);
Does a language tag lookup (under RFC 4647) for a matching language tag.
Parameters:
-
ranges: A list of basic language ranges (see documentation for the “IsLanguageRange” method), which should be given in order of descending preference.
-
languages: A list of language tags, which should be given in order of descending preference.
-
defaultValue: The value to return if no matching language tag was found.
Return Value:
The matching language tag, or the parameter defaultValue if there is no matching language tag.
Exceptions:
-
System.ArgumentNullException: The parameter languages or ranges is null.
-
System.ArgumentException: The parameter ranges contains a value that is not a basic language range, or languages contains a value that is not a potentially valid language tag.
public static string LanguageTagLookup( System.Collections.Generic.IList ranges, System.Collections.Generic.IList languages, string defaultValue, bool extended);
Does a language tag lookup (under RFC 4647) for a matching language tag.
Parameters:
-
ranges: A list of language ranges (see documentation for the “IsLanguageRange” method), which should be given in order of descending preference.
-
languages: A list of language tags, which should be given in order of descending preference.
-
defaultValue: The value to return if no matching language tag was found.
-
extended: If true, the ranges in “ranges” are extended language ranges; otherwise, they are basic language ranges.
Return Value:
The matching language tag, or the parameter defaultValue if there is no matching language tag.
Exceptions:
-
System.ArgumentNullException: The parameter languages or ranges is null.
-
System.ArgumentException: The parameter ranges contains a value that is not a basic or extended language range, or languages contains a value that is not a potentially valid language tag.
public static bool MatchesLanguageTag( string range, string tag);
Determines whether the given language tag matches the given language range.
Parameters:
-
range: A basic language range (see the documentation for “IsLanguageRange”).
-
tag: A language tag.
Return Value:
true
if the language tag matches the language range by the filtering method under RFC 4647; otherwise, false
.
Exceptions:
- System.ArgumentException: The parameter range is not a basic language range, or tag is not a potentially valid language tag.