add locale service to GUI. minor cleanup of controller.

This commit is contained in:
Dennis Wilson 2014-11-28 19:39:33 +01:00
parent 1219423091
commit ee9c109f07
5 changed files with 166 additions and 78 deletions

View File

@ -868,6 +868,7 @@
<script src="scripts/syncthing/core/filters/basenameFilter.js"></script> <script src="scripts/syncthing/core/filters/basenameFilter.js"></script>
<script src="scripts/syncthing/core/filters/binaryFilter.js"></script> <script src="scripts/syncthing/core/filters/binaryFilter.js"></script>
<script src="scripts/syncthing/core/filters/naturalFilter.js"></script> <script src="scripts/syncthing/core/filters/naturalFilter.js"></script>
<script src="scripts/syncthing/core/services/localeService.js"></script>
<script src="assets/lang/valid-langs.js"></script> <script src="assets/lang/valid-langs.js"></script>
<script src="scripts/syncthing/app.js"></script> <script src="scripts/syncthing/app.js"></script>

View File

@ -25,7 +25,7 @@ var syncthing = angular.module('syncthing', [
var urlbase = 'rest'; var urlbase = 'rest';
var guiVersion = null; var guiVersion = null;
syncthing.config(function ($httpProvider, $translateProvider) { syncthing.config(function ($httpProvider, $translateProvider, LocaleServiceProvider) {
$httpProvider.defaults.xsrfHeaderName = 'X-CSRF-Token'; $httpProvider.defaults.xsrfHeaderName = 'X-CSRF-Token';
$httpProvider.defaults.xsrfCookieName = 'CSRF-Token'; $httpProvider.defaults.xsrfCookieName = 'CSRF-Token';
$httpProvider.interceptors.push(function () { $httpProvider.interceptors.push(function () {
@ -42,11 +42,16 @@ syncthing.config(function ($httpProvider, $translateProvider) {
}; };
}); });
// language and localisation
$translateProvider.useStaticFilesLoader({ $translateProvider.useStaticFilesLoader({
prefix: 'assets/lang/lang-', prefix: 'assets/lang/lang-',
suffix: '.json' suffix: '.json'
}); });
LocaleServiceProvider.setAvailableLocales(validLangs);
LocaleServiceProvider.setDefaultLocale('en');
}); });
// @TODO: extract global level functions into seperate service(s) // @TODO: extract global level functions into seperate service(s)

View File

@ -1,13 +1,43 @@
angular.module('syncthing.core') angular.module('syncthing.core')
.controller('SyncthingController', function ($scope, $http, $translate, $location) { .controller('SyncthingController', function ($scope, $http, $location, LocaleService) {
'use strict'; 'use strict';
// private/helper definitions
var prevDate = 0; var prevDate = 0;
var getOK = true;
var navigatingAway = false; var navigatingAway = false;
var online = false; var online = false;
var restarting = false; var restarting = false;
function initController() {
LocaleService.autoConfigLocale();
refreshSystem();
refreshConfig();
refreshConnectionStats();
refreshDeviceStats();
$http.get(urlbase + '/version').success(function (data) {
$scope.version = data.version;
});
$http.get(urlbase + '/report').success(function (data) {
$scope.reportData = data;
});
$http.get(urlbase + '/upgrade').success(function (data) {
$scope.upgradeInfo = data;
}).error(function () {
$scope.upgradeInfo = null;
});
setInterval($scope.refresh, 10000);
}
// pubic/scope definitions
$scope.completion = {}; $scope.completion = {};
$scope.config = {}; $scope.config = {};
$scope.configInSync = true; $scope.configInSync = true;
@ -25,47 +55,12 @@ angular.module('syncthing.core')
$scope.stats = {}; $scope.stats = {};
$scope.progress = {}; $scope.progress = {};
$http.get(urlbase + "/lang").success(function (langs) {
// Find the first language in the list provided by the user's browser
// that is a prefix of a language we have available. That is, "en"
// sent by the browser will match "en" or "en-US", while "zh-TW" will
// match only "zh-TW" and not "zh-CN".
var lang, matching;
for (var i = 0; i < langs.length; i++) {
lang = langs[i];
if (lang.length < 2) {
continue;
}
matching = validLangs.filter(function (possibleLang) {
// The langs returned by the /rest/langs call will be in lower
// case. We compare to the lowercase version of the language
// code we have as well.
possibleLang = possibleLang.toLowerCase();
if (possibleLang.length > lang.length) {
return possibleLang.indexOf(lang) === 0;
} else {
return lang.indexOf(possibleLang) === 0;
}
});
if (matching.length >= 1) {
$translate.use(matching[0]);
return;
}
}
// Fallback if nothing matched
$translate.use("en");
});
$(window).bind('beforeunload', function () { $(window).bind('beforeunload', function () {
navigatingAway = true; navigatingAway = true;
}); });
$scope.$on("$locationChangeSuccess", function () { $scope.$on("$locationChangeSuccess", function () {
var lang = $location.search().lang; LocaleService.useLocale($location.search().lang);
if (lang) {
$translate.use(lang);
}
}); });
$scope.needActions = { $scope.needActions = {
@ -87,7 +82,7 @@ angular.module('syncthing.core')
} }
console.log('UIOnline'); console.log('UIOnline');
$scope.init(); initController();
online = true; online = true;
restarting = false; restarting = false;
$('#networkError').modal('hide'); $('#networkError').modal('hide');
@ -181,10 +176,10 @@ angular.module('syncthing.core')
$scope.$on('DownloadProgress', function (event, arg) { $scope.$on('DownloadProgress', function (event, arg) {
var stats = arg.data; var stats = arg.data;
var progress = {}; var progress = {};
for(var folder in stats){ for (var folder in stats) {
refreshFolder(folder); refreshFolder(folder);
progress[folder] = {}; progress[folder] = {};
for(var file in stats[folder]){ for (var file in stats[folder]) {
var s = stats[folder][file]; var s = stats[folder][file];
var reused = Math.floor(100 * s.Reused / s.Total); var reused = Math.floor(100 * s.Reused / s.Total);
var copiedFromOrigin = Math.floor(100 * s.CopiedFromOrigin / s.Total); var copiedFromOrigin = Math.floor(100 * s.CopiedFromOrigin / s.Total);
@ -207,14 +202,14 @@ angular.module('syncthing.core')
}; };
} }
} }
for(var folder in $scope.progress){ for (var folder in $scope.progress) {
if (!(folder in progress)) { if (!(folder in progress)) {
refreshFolder(folder); refreshFolder(folder);
if ($scope.neededFolder == folder) { if ($scope.neededFolder == folder) {
refreshNeed(folder); refreshNeed(folder);
} }
} else if ($scope.neededFolder == folder) { } else if ($scope.neededFolder == folder) {
for(file in $scope.progress[folder]){ for (file in $scope.progress[folder]) {
if (!(file in progress[folder])) { if (!(file in progress[folder])) {
refreshNeed(folder); refreshNeed(folder);
break; break;
@ -379,27 +374,6 @@ angular.module('syncthing.core')
}); });
}, 500); }, 500);
$scope.init = function () {
refreshSystem();
refreshConfig();
refreshConnectionStats();
refreshDeviceStats();
$http.get(urlbase + '/version').success(function (data) {
$scope.version = data.version;
});
$http.get(urlbase + '/report').success(function (data) {
$scope.reportData = data;
});
$http.get(urlbase + '/upgrade').success(function (data) {
$scope.upgradeInfo = data;
}).error(function () {
$scope.upgradeInfo = null;
});
};
$scope.refresh = function () { $scope.refresh = function () {
refreshSystem(); refreshSystem();
refreshConnectionStats(); refreshConnectionStats();
@ -987,7 +961,7 @@ angular.module('syncthing.core')
$scope.showNeed = function (folder) { $scope.showNeed = function (folder) {
$scope.neededFolder = folder; $scope.neededFolder = folder;
refreshNeed(folder); refreshNeed(folder);
$('#needed').modal().on('hidden.bs.modal', function(){ $('#needed').modal().on('hidden.bs.modal', function () {
$scope.neededFolder = undefined; $scope.neededFolder = undefined;
$scope.needed = undefined; $scope.needed = undefined;
}); });
@ -1024,6 +998,6 @@ angular.module('syncthing.core')
$http.post(urlbase + "/scan?folder=" + encodeURIComponent(folder)); $http.post(urlbase + "/scan?folder=" + encodeURIComponent(folder));
}; };
$scope.init(); // pseudo main. called on all definitions assigned
setInterval($scope.refresh, 10000); initController();
}); });

View File

@ -0,0 +1,83 @@
angular.module('syncthing.core')
.provider('LocaleService', function () {
var _defaultLocale,
_availableLocales;
this.setDefaultLocale = function (locale) {
_defaultLocale = locale;
};
this.setAvailableLocales = function (locales) {
_availableLocales = locales;
};
this.$get = ['$http', '$translate', function ($http, $translate) {
/**
* Requests the server in order to get the browser's requested locale strings.
*
* @returns promise which on success resolves with a locales array
*/
function readBrowserLocales() {
// @TODO: check if there is nice way to utilize window.navigator.languages or similiar api.
return $http.get(urlbase + "/lang");
}
function autoConfigLocale() {
return readBrowserLocales().success(function (langs) {
// Find the first language in the list provided by the user's browser
// that is a prefix of a language we have available. That is, "en"
// sent by the browser will match "en" or "en-US", while "zh-TW" will
// match only "zh-TW" and not "zh-CN".
var i,
lang,
matching;
for (i = 0; i < langs.length; i++) {
lang = langs[i];
if (lang.length < 2) {
continue;
}
matching = _availableLocales.filter(function (possibleLang) {
// The langs returned by the /rest/langs call will be in lower
// case. We compare to the lowercase version of the language
// code we have as well.
possibleLang = possibleLang.toLowerCase();
if (possibleLang.length > lang.length) {
return possibleLang.indexOf(lang) === 0;
} else {
return lang.indexOf(possibleLang) === 0;
}
});
if (matching.length >= 1) {
$translate.use(matching[0]);
return;
}
}
// Fallback if nothing matched
$translate.use(_defaultLocale);
});
}
function useLocale(language) {
// @TODO: eventually check for valid locale format
if (language) {
$translate.use(language);
}
}
return {
autoConfigLocale: autoConfigLocale,
useLocale: useLocale
}
}];
});

File diff suppressed because one or more lines are too long