Erdem Çetin Erdem Çetin - 4 months ago 22
AngularJS Question

AngularJS how to use browser language detection?

Hi I get text from database with http.get .How to get with browser detection ? How to use if clause ? I don't want to use translate.toogle()

var mainCtrl = function ($scope, $http, $sce, $location,$timeout,$routeParams, toastr) {

$scope.showPage = false;
$timeout(function(){
$scope.showPage = true;
},2000);


$scope.trustedHtml = function (plainText) {
return $sce.trustAsHtml(plainText);
}

$http.get('admin/api/?s=getAbout').success(

##### if browser En_en
function(r){
$scope.about = r.text_en;

##### if browser De_de
function(r){
$scope.about = r.text_de;
}
);

};

Answer

You can get the browser language using navigator.language:

var lang = $window.navigator.language || $window.navigator.userLanguage; 
if (lang === 'en-US') {
  console.log("language is english");
}