2016-07-16 19:16:17 +02:00
|
|
|
(function () {
|
|
|
|
'use strict';
|
|
|
|
|
2018-11-18 05:19:21 +01:00
|
|
|
angular.module('ariaNg').factory('ariaNgLanguageLoader', ['$http', '$q', 'ariaNgConstants', 'ariaNgLanguages', 'ariaNgAssetsCacheService', 'ariaNgNotificationService', 'ariaNgLogService', 'ariaNgStorageService', function ($http, $q, ariaNgConstants, ariaNgLanguages, ariaNgAssetsCacheService, ariaNgNotificationService, ariaNgLogService, ariaNgStorageService) {
|
2016-08-01 18:59:19 +02:00
|
|
|
var getKeyValuePair = function (line) {
|
2017-03-05 16:12:57 +01:00
|
|
|
for (var i = 0; i < line.length; i++) {
|
|
|
|
if (i > 0 && line.charAt(i - 1) !== '\\' && line.charAt(i) === '=') {
|
|
|
|
return {
|
|
|
|
key: line.substring(0, i).replace('\\=', '='),
|
|
|
|
value: line.substring(i + 1, line.length).replace('\\=', '=')
|
|
|
|
};
|
|
|
|
}
|
2016-08-01 18:59:19 +02:00
|
|
|
}
|
2017-03-26 06:41:07 +02:00
|
|
|
|
2017-03-05 16:12:57 +01:00
|
|
|
return {
|
|
|
|
value: line
|
|
|
|
};
|
2016-08-01 18:59:19 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
var getCategory = function (langObj, category) {
|
|
|
|
var currentCategory = langObj;
|
|
|
|
|
|
|
|
if (!category) {
|
|
|
|
return currentCategory;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (category[0] === '[' && category[category.length - 1] === ']') {
|
|
|
|
category = category.substring(1, category.length - 1);
|
|
|
|
}
|
|
|
|
|
2018-07-01 16:53:20 +02:00
|
|
|
if (category === 'global') {
|
2016-08-01 18:59:19 +02:00
|
|
|
return currentCategory;
|
|
|
|
}
|
|
|
|
|
|
|
|
var categoryNames = category.split('.');
|
|
|
|
|
|
|
|
for (var i = 0; i < categoryNames.length; i++) {
|
|
|
|
var categoryName = categoryNames[i];
|
|
|
|
|
|
|
|
if (!currentCategory[categoryName]) {
|
|
|
|
currentCategory[categoryName] = {};
|
|
|
|
}
|
|
|
|
|
|
|
|
currentCategory = currentCategory[categoryName];
|
|
|
|
}
|
|
|
|
|
|
|
|
return currentCategory;
|
|
|
|
};
|
|
|
|
|
|
|
|
var getLanguageObject = function (languageContent) {
|
|
|
|
var langObj = {};
|
|
|
|
|
|
|
|
if (!languageContent) {
|
|
|
|
return langObj;
|
|
|
|
}
|
|
|
|
|
|
|
|
var lines = languageContent.split('\n');
|
|
|
|
var currentCatagory = langObj;
|
|
|
|
|
|
|
|
for (var i = 0; i < lines.length; i++) {
|
|
|
|
var line = lines[i];
|
|
|
|
|
|
|
|
if (!line) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2017-03-26 06:41:07 +02:00
|
|
|
line = line.replace('\r', '');
|
|
|
|
|
2016-08-01 18:59:19 +02:00
|
|
|
if (/^\[.+\]$/.test(line)) {
|
|
|
|
currentCatagory = getCategory(langObj, line);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
var pair = getKeyValuePair(line);
|
|
|
|
|
|
|
|
if (pair && pair.key) {
|
|
|
|
currentCatagory[pair.key] = pair.value;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return langObj;
|
|
|
|
};
|
|
|
|
|
2016-07-16 19:16:17 +02:00
|
|
|
return function (options) {
|
|
|
|
var deferred = $q.defer();
|
|
|
|
|
|
|
|
if (!ariaNgLanguages[options.key]) {
|
|
|
|
deferred.reject(options.key);
|
|
|
|
return deferred.promise;
|
|
|
|
}
|
|
|
|
|
|
|
|
var languageKey = ariaNgConstants.languageStorageKeyPrefix + '.' + options.key;
|
2018-08-12 14:47:48 +02:00
|
|
|
var languageResource = ariaNgStorageService.get(languageKey);
|
2016-07-16 19:16:17 +02:00
|
|
|
|
|
|
|
if (languageResource) {
|
|
|
|
deferred.resolve(languageResource);
|
|
|
|
}
|
|
|
|
|
2018-11-18 05:19:21 +01:00
|
|
|
if (ariaNgAssetsCacheService.getLanguageAsset(options.key)) {
|
|
|
|
var languageObject = getLanguageObject(ariaNgAssetsCacheService.getLanguageAsset(options.key));
|
|
|
|
ariaNgStorageService.set(languageKey, languageObject);
|
|
|
|
deferred.resolve(languageObject);
|
|
|
|
|
|
|
|
return deferred.promise;
|
|
|
|
}
|
|
|
|
|
2016-07-16 19:16:17 +02:00
|
|
|
var languagePath = ariaNgConstants.languagePath + '/' + options.key + ariaNgConstants.languageFileExtension;
|
|
|
|
|
|
|
|
$http({
|
|
|
|
url: languagePath,
|
|
|
|
method: 'GET'
|
2017-05-19 17:36:37 +02:00
|
|
|
}).then(function onSuccess(response) {
|
|
|
|
var languageObject = getLanguageObject(response.data);
|
2018-08-12 14:47:48 +02:00
|
|
|
ariaNgStorageService.set(languageKey, languageObject);
|
2016-08-01 18:59:19 +02:00
|
|
|
return deferred.resolve(languageObject);
|
2017-05-19 17:36:37 +02:00
|
|
|
}).catch(function onError(response) {
|
2018-08-12 14:27:51 +02:00
|
|
|
ariaNgLogService.warn('[ariaNgLanguageLoader] cannot get language resource');
|
2018-08-12 15:17:51 +02:00
|
|
|
ariaNgNotificationService.notifyInPage('', 'AriaNg cannot get language resources, and will display in default language.', {
|
2018-08-12 14:27:51 +02:00
|
|
|
type: 'error',
|
|
|
|
delay: false
|
|
|
|
});
|
2016-07-16 19:16:17 +02:00
|
|
|
return deferred.reject(options.key);
|
|
|
|
});
|
|
|
|
|
|
|
|
return deferred.promise;
|
|
|
|
};
|
|
|
|
}]);
|
2016-08-01 16:49:16 +02:00
|
|
|
}());
|