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

116 lines
3.5 KiB
JavaScript
Raw Normal View History

2016-07-16 19:16:17 +02:00
(function () {
'use strict';
angular.module('ariaNg').factory('ariaNgLanguageLoader', ['$http', '$q', 'localStorageService', 'ariaNgConstants', 'ariaNgLanguages', function ($http, $q, localStorageService, ariaNgConstants, ariaNgLanguages) {
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;
var languageResource = localStorageService.get(languageKey);
if (languageResource) {
deferred.resolve(languageResource);
}
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);
2016-08-01 18:59:19 +02:00
localStorageService.set(languageKey, languageObject);
return deferred.resolve(languageObject);
2017-05-19 17:36:37 +02:00
}).catch(function onError(response) {
2016-07-16 19:16:17 +02:00
return deferred.reject(options.key);
});
return deferred.promise;
};
}]);
2016-08-01 16:49:16 +02:00
}());