AcceptLanguage

View Source

Summary

The value of a Accept-Language HTTP header.

MDN Accept-Language Reference

HTTP/1.1 Specification

Signature

class AcceptLanguage {
  constructor(init: string | AcceptLanguageInit): AcceptLanguage;

  // Accessors
  get languages(): string[];
  get size(): number;
  get weights(): number[];

  // Methods
  [iterator](): IterableIterator<[string, number]>;
  accepts(language: string): boolean;
  clear(): void;
  delete(language: string): void;
  entries(): IterableIterator<[string, number]>;
  forEach(
    callback: (
      language: string,
      weight: number,
      header: AcceptLanguage,
    ) => void,
    thisArg: any,
  ): void;
  get(language: string): number | null;
  getPreferred<language extends string>(
    languages: readonly language[],
  ): language | null;
  getWeight(language: string): number;
  has(language: string): boolean;
  set(language: string, weight: number): void;
  toString(): string;
  from(value: string | AcceptLanguageInit | null): AcceptLanguage;
}

Constructor Params

init

Accessors

languages

An array of all languages in the header.

size

The number of languages in the header.

weights

An array of all weights (q values) in the header.

Methods

iterator: IterableIterator<[string, number]>

Iterates over language and weight pairs in preference order.

accepts(language: string): boolean

Returns true if the header matches the given language (i.e. it is "acceptable").

language

The locale identifier of the language to check

clear(): void

Removes all languages from the header.

delete(language: string): void

Removes a language with the given locale identifier.

language

The locale identifier of the language to remove

entries(): IterableIterator<[string, number]>

Returns an iterator of all language and weight pairs.

forEach(callback: (language: string, weight: number, header: AcceptLanguage) => void, thisArg: any): void

Invokes the callback for each language and weight pair.

callback

The function to call for each pair

thisArg

The value to use as this when calling the callback

get(language: string): number | null

Gets the weight of a language with the given locale identifier. If it is not in the header verbatim, this returns null.

language

The locale identifier of the language to get

getPreferred(languages: readonly language[]): language | null

Returns the most preferred language from the given list of languages.

languages

The locale identifiers of the languages to choose from

getWeight(language: string): number

Gets the weight of a language with the given locale identifier. Performs wildcard and subtype matching, so en matches en-US and en-GB, and * matches all languages.

language

The locale identifier of the language to get

has(language: string): boolean

Checks if the header contains a language with the given locale identifier.

language

The locale identifier of the language to check

set(language: string, weight: number): void

Sets a language with the given weight.

language

The locale identifier of the language to set

weight

The weight of the language (default: 1)

toString(): string

Returns the string representation of the header value.

from(value: string | AcceptLanguageInit | null): AcceptLanguage

Parse an Accept-Language header value.

value

The header value (string, init object, or null)