9

I just realized that fetch does not send Accept-Language header by default, therefore I plan to add that. However, I haven't come across any way to get the string that the browser should generate.

For example, in my browser, all normal requests have a nice Accept-Language value:

en-US,en;q=0.9,ja;q=0.8,vi;q=0.7,la;q=0.6

My problem can be solved by either option:

  • Is there a declaration that tell browser to include Accept-Language header with fetch?
  • Is there a way to get that string instead of generate it from navigator.languages? Currently I am using this method, but I made it myself, and not sure if it's overkill/bad. I would prefer to use a built in way if possible.
var getAcceptLanguageValue = () => {
    // Assume there are less than 10 languages
    let q = 1;

    return navigator.languages
        .slice(0, 10)
        .map(l => `${l};q=0.${10-q++}`)
        .join(",");
};

The result I get is not identical obviously:

en-US;q=0.9,en;q=0.8,ja;q=0.7,vi;q=0.6,la;q=0.5

Luke Vo
  • 17,859
  • 21
  • 105
  • 181

0 Answers0