AngularJS how to use browser language detection?

All we need is an easy explanation of the problem, so here it is.

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;
            }
        );

};

How to solve :

I know you bored from this bug, So we are here to help you! Take a deep breath and look at the explanation of your problem. We have many solutions to this problem, But we recommend you to use the first method because it is tested & true method that will 100% work for you.

Method 1

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");
}

Method 2

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

is the best way.

ref: JavaScript for detecting browser language preference

Note: Use and implement method 1 because this method fully tested our system.
Thank you 🙂

All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply