1

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.

2
  • you should have an headers object or array where you can add your header information. On Angular2/4 you have something like this: var headers = new Headers(); headers.append('Accept-Language', ''xyz); this.http.post(url, data, { headers: headers }) Commented Jun 16, 2017 at 12:54
  • @moohkooh When I do a request browser sets the language code to the header and I need to read that. I dont need to set it, I need to know the value that browser set up. Commented Jun 19, 2017 at 5:51

1 Answer 1

2

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");
}]);
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.