Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AngularJS how to use browser language detection?

Tags:

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;             }         );  }; 
like image 379
Erdem Çetin Avatar asked Jun 16 '15 10:06

Erdem Çetin


People also ask

How does Angularjs detect browser?

This should be solved by checking for WebSocket support, and applying a polyfill if there is no native support. This should be done with a library like Modernizr. That being said, you can easily create service that would return the browser.

How do I check my browser language?

Open the browser settings, and in the advanced section scroll down to find Languages . Open Language and Input Settings and add the language or language+region choice you want from the list available. Order the resulting list so that it is in descending order of preference. You don't need to restart Chrome.

How does website identify language?

Detecting browser preference is the most common and most effective way used by websites to detect your preferred language automatically. Very simply put, when you install and set up the browser on your device, you choose the language that you want to use.


2 Answers

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"); } 
like image 77
alisabzevari Avatar answered Sep 22 '22 11:09

alisabzevari


navigator.languages     ? navigator.languages[0]     : (navigator.language || navigator.userLanguage) 

is the best way.

ref: JavaScript for detecting browser language preference

like image 40
Anushree Acharjee Avatar answered Sep 19 '22 11:09

Anushree Acharjee