mirror of
https://github.com/octoleo/syncthing.git
synced 2024-11-09 14:50:56 +00:00
Merge pull request #1037 from snnd/locale-service
Added Locale Service. Minor Controller Refactoring.
This commit is contained in:
commit
bb50b677c7
@ -868,6 +868,7 @@
|
||||
<script src="scripts/syncthing/core/filters/basenameFilter.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/services/localeService.js"></script>
|
||||
|
||||
<script src="assets/lang/valid-langs.js"></script>
|
||||
<script src="scripts/syncthing/app.js"></script>
|
||||
|
@ -25,7 +25,7 @@ var syncthing = angular.module('syncthing', [
|
||||
var urlbase = 'rest';
|
||||
var guiVersion = null;
|
||||
|
||||
syncthing.config(function ($httpProvider, $translateProvider) {
|
||||
syncthing.config(function ($httpProvider, $translateProvider, LocaleServiceProvider) {
|
||||
$httpProvider.defaults.xsrfHeaderName = 'X-CSRF-Token';
|
||||
$httpProvider.defaults.xsrfCookieName = 'CSRF-Token';
|
||||
$httpProvider.interceptors.push(function () {
|
||||
@ -42,11 +42,16 @@ syncthing.config(function ($httpProvider, $translateProvider) {
|
||||
};
|
||||
});
|
||||
|
||||
// language and localisation
|
||||
|
||||
$translateProvider.useStaticFilesLoader({
|
||||
prefix: 'assets/lang/lang-',
|
||||
suffix: '.json'
|
||||
});
|
||||
|
||||
LocaleServiceProvider.setAvailableLocales(validLangs);
|
||||
LocaleServiceProvider.setDefaultLocale('en');
|
||||
|
||||
});
|
||||
|
||||
// @TODO: extract global level functions into seperate service(s)
|
||||
|
@ -1,13 +1,43 @@
|
||||
angular.module('syncthing.core')
|
||||
.controller('SyncthingController', function ($scope, $http, $translate, $location) {
|
||||
.controller('SyncthingController', function ($scope, $http, $location, LocaleService) {
|
||||
'use strict';
|
||||
|
||||
// private/helper definitions
|
||||
|
||||
var prevDate = 0;
|
||||
var getOK = true;
|
||||
var navigatingAway = false;
|
||||
var online = 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.config = {};
|
||||
$scope.configInSync = true;
|
||||
@ -25,47 +55,12 @@ angular.module('syncthing.core')
|
||||
$scope.stats = {};
|
||||
$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 () {
|
||||
navigatingAway = true;
|
||||
});
|
||||
|
||||
$scope.$on("$locationChangeSuccess", function () {
|
||||
var lang = $location.search().lang;
|
||||
if (lang) {
|
||||
$translate.use(lang);
|
||||
}
|
||||
LocaleService.useLocale($location.search().lang);
|
||||
});
|
||||
|
||||
$scope.needActions = {
|
||||
@ -87,7 +82,7 @@ angular.module('syncthing.core')
|
||||
}
|
||||
|
||||
console.log('UIOnline');
|
||||
$scope.init();
|
||||
initController();
|
||||
online = true;
|
||||
restarting = false;
|
||||
$('#networkError').modal('hide');
|
||||
@ -379,27 +374,6 @@ angular.module('syncthing.core')
|
||||
});
|
||||
}, 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 () {
|
||||
refreshSystem();
|
||||
refreshConnectionStats();
|
||||
@ -1024,6 +998,6 @@ angular.module('syncthing.core')
|
||||
$http.post(urlbase + "/scan?folder=" + encodeURIComponent(folder));
|
||||
};
|
||||
|
||||
$scope.init();
|
||||
setInterval($scope.refresh, 10000);
|
||||
// pseudo main. called on all definitions assigned
|
||||
initController();
|
||||
});
|
||||
|
83
gui/scripts/syncthing/core/services/localeService.js
Normal file
83
gui/scripts/syncthing/core/services/localeService.js
Normal 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
Loading…
Reference in New Issue
Block a user