I am using angularjs 1.5.5 version and I want to detect the browser language automatically and translate the application language into the local browser language
Asked
Active
Viewed 1,944 times
2
-
3Possible duplicate of [AngularJS how to use browser language detection?](https://stackoverflow.com/questions/30864478/angularjs-how-to-use-browser-language-detection) – Dec 27 '18 at 03:37
1 Answers
0
You can use NavigatorLaguage in order to detect language of your client's browser. Problem with NavigatorLanguage is browser compatibility. You can also localizing your app using IP of your client, for example if IP of your client is from Germany, you can deliver your content in German or if IP of your client is from Iran, you can deliver your content in Persian.
angular.module('app', []).controller('localization', function ($scope) {
$scope.language = window.navigator.language;
});
.localization-container{
padding: 2em;
background: linear-gradient(rgb(239, 239, 239),rgba(177, 177, 177, 0.1));
}
.hello-container{
padding: .5em;
background-color: rgb(10, 192, 159);
color: #fff;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.7.5/angular.min.js"></script>
<div ng-app="app">
<div ng-controller="localization" class="localization-container">
<p>Your browser languge is: {{language}}</p>
<hr>
<p>Sayin hello base on language of your browser:</p>
<div ng-show="{{IsAvailableLocalLanguage=language=='fa'||language=='fa-IR'}}" >
<span class="hello-container">سلام</span>
</div>
<div ng-show="{{IsAvailableLocalLanguage=language=='de'}}">
<span class="hello-container">Hallo</span>
</div>
<div ng-show="!IsAvailableLocalLanguage">
<span class="hello-container">Hello</span>
</div>
<p>Note: I only supose your local languages beside English be Persian(fa), Persian(fa-IR), and German(de)</p>
</div>
</div>

Alireza Alipour
- 11
- 4