This repository has been archived on 2022-01-02. You can view files and clone it, but cannot push or open issues or pull requests.
AriaNg/src/scripts/services/ariaNgLanguageLoader.js

129 lines
4.4 KiB
JavaScript
Raw Normal View History

2016-07-16 19:16:17 +02:00
(function () {
'use strict';
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-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;
}
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);
}
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) {
ariaNgLogService.warn('[ariaNgLanguageLoader] cannot get language resource');
ariaNgNotificationService.notifyInPage('', 'AriaNg cannot get language resources, and will display in default language.', {
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
}());