lang-swicher.directive.js 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. (function(){
  2. 'use strict';
  3. app.directive('langSwitcher', langSwitcher);
  4. function langSwitcher(){
  5. return {
  6. restrict: 'E',
  7. templateUrl: './app/directive/lang-swicher/lang-swicher.template.html',
  8. scope: {},
  9. controller: ['$scope', '$translate', '$rootScope', function($scope, $translate, $rootScope){
  10. $scope.languages = [
  11. {key: 'en', value: 'English'},
  12. {key: 'ru', value: 'Russian'}
  13. ];
  14. $scope.showMenu = false;
  15. $scope.toggleMenu = function(){
  16. $scope.showMenu = !$scope.showMenu
  17. }
  18. var key = localStorage.getItem('preferredLanguage') || 'en'
  19. $scope.currentLang = $scope.languages.filter(function(item){
  20. return item.key === key;
  21. })[0];
  22. $scope.changeLanguage = function(key){
  23. $scope.currentLang = $scope.languages.filter(function(item){
  24. return item.key === key;
  25. })[0];
  26. $scope.showMenu = false;
  27. $translate.use(key)
  28. localStorage.setItem('preferredLanguage', key);
  29. $rootScope.$broadcast('changeLang', key);
  30. };
  31. $scope.$on('changeLang', function(event, value){
  32. if($scope.currentLang.key !== value ){
  33. $scope.currentLang = $scope.languages.filter(function(item){
  34. return item.key === value;
  35. })[0];
  36. }
  37. })
  38. }]
  39. }
  40. }
  41. })()