1234567891011121314151617181920212223242526272829303132333435363738394041424344 |
- (function() {
- 'use strict';
- app.directive('langSwitcher', langSwitcherController);
- function getCurrentlanguage(key) {
- return this.languages.filter((function(item) { return item.key === key }))[0];
- }
- function langSwitcherController() {
- return {
- restrict: 'E',
- templateUrl: './app/directives/lang-switcher/lang-switcher.template.html',
- scope: {},
- link: function(scope, element, attr) {},
- controller: ['$scope', '$translate', '$rootScope', function($scope, $translate, $rootScope) {
- $scope.languages = [
- { key: 'en', value: 'En' },
- { key: 'ru', value: 'Ru' },
- { key: 'ua', value: 'Ua' }
- ];
- $scope.showMenu = false;
- $scope.toggleMenu = function() {
- $scope.showMenu = !$scope.showMenu;
- }
- var key = localStorage.getItem('preferredLanguage') || 'en';
- $scope.currentLang = getCurrentlanguage.call($scope, key);
- $scope.changeLanguage = function(key) {
- $scope.currentLang = getCurrentlanguage.call($scope, key);
- $translate.use(key);
- localStorage.setItem('preferredLanguage', key);
- $rootScope.$broadcast('changeLang', key);
- $scope.showMenu = !$scope.showMenu;
- };
- $scope.$on('changeLang', function(event, value) {
- if ($scope.currentLang.key !== value) {
- $scope.currentLang = getCurrentlanguage.call($scope, value);
- }
- });
- }],
- }
- }
- langSwitcherController.$inject = [];
- })();
|