backupApp.directive('languageSelector', function() {
    return {
        restrict: 'E',
        templateUrl: 'login/templates/language-selector.html',
        controller: function($scope, $rootScope, $cookies, gettextCatalog, SystemInfo) {
            $scope.SystemInfo = SystemInfo.watch($scope);

            $scope.uiLanguage = $cookies.get('ui-locale');

            $scope.setUILanguage = () => {
                if (($scope.uiLanguage || '').trim().length == 0) {
                    $cookies.remove('ui-locale');
                    gettextCatalog.setCurrentLanguage($scope.SystemInfo.BrowserLocale.Code.replace("-", "_"));
                } else {
                    var now = new Date();
                    var exp = new Date(now.getFullYear()+10, now.getMonth(), now.getDate());
                    $cookies.put('ui-locale', $scope.uiLanguage, { expires: exp });

                    gettextCatalog.setCurrentLanguage($scope.uiLanguage.replace("-", "_"));
                }
                $rootScope.$broadcast('ui_language_changed');
            };
        }
    }
});