Makarov Sergey
Makarov Sergey

Reputation: 932

How to get Accept-Language request header value on client

I need to know Accept-Language request header value in order to make a translation on a page. I've tried to create an interceptor like

$httpProvider.interceptors.push(requestInterceptor);

, but in the method

request: function (config) {

of my interceptor I don't see Accept-Language header. I see Accept, Cache-Control, If-Modified-Since, Pragma but in the browser I do see Accept-Language.

Upvotes: 1

Views: 2904

Answers (1)

Youness HARDI
Youness HARDI

Reputation: 532

Not all request headers are available in AngularJS request interceptor's config parameter. other header values are browser settings that are added while constructing the request.

You could use, but i'm not sure it gives you the right language.

var language = window.navigator.userLanguage or window.navigator.language;

Only the server can see the value of Accept-Language. So i think the best way is to get this value from the server response-body and memorize it in you cookies (name example AcceptLanguageCookie) and after that you can overide the Accept-Language someway like this in your interceptor.

var app = angular.module("app", []);

app.config(["$httpProvider", function($httpProvider) {
  // set Accept-Language header on all requests
  $httpProvider.defaults.headers.common["Accept-Language"] = $cookies.get("AcceptLanguageCookie");
}]);

Upvotes: 2

Related Questions