0

I am looking at way to set window.navigator.language programmatically. I was wondering if there was a way to do this using angularjs ?

Currently, I am using localization service to switch get my localization with i18n.

Frank
  • 3,073
  • 5
  • 40
  • 67
  • Since it's a browser setting it's a ready only property. You could ask a user to change it via preferences or similar but it cannot be set programmatically http://stackoverflow.com/questions/3753999/how-do-i-change-the-browser-language-with-javascript – bcr Jan 17 '17 at 18:41
  • Not directly. See this [SO Q/A](http://stackoverflow.com/questions/1043339/javascript-for-detecting-browser-language-preference) for inspiration. – RamblinRose Jan 17 '17 at 18:42
  • no, but wherever your code looks there, you can look at another property (of your making) instead. – dandavis Jan 17 '17 at 18:53
  • Possible duplicate of [How do I change the browser language with Javascript](http://stackoverflow.com/questions/3753999/how-do-i-change-the-browser-language-with-javascript) – Heretic Monkey Jan 17 '17 at 18:57

2 Answers2

1

Quick answer: No

Locale should be set by the browser and is typically read only.

There may be ways to mess with the browser to change this, but if this could be done with just JavaScript that would potentially be a security vulnerability.

haelmic
  • 541
  • 4
  • 18
0

If your intention is to change the user's browser language then I think you're doomed due to all the security things... However, if your intention is to change the language for the Accept-Language header in web requests you have a better case.

For the latter - check out Angulars $httpProvider.interceptors.

$httpProvider.interceptors.push(function($q) {
    return {
     'request': function(config) {
            config.headers['Accept-Language'] = 'some locale';
            return config;
        }
    };
});

Good luck :)

maxpaj
  • 6,029
  • 5
  • 35
  • 56