SmartIT_Extensions/BMC/smart-it-full-helix/scripts/app/i18n/i18n-directives.js

78 lines
3.6 KiB
JavaScript

"use strict";
(function () {
'use strict';
angular.module('i18nModule')
// translation directive that can handle dynamic strings
// updates the text value of the attached element
// usage <span data-i18n="TOKEN"></span>
// or
// <span data-i18n="TOKEN|VALUE1|VALUE2"></span>
.directive('i18n', ['i18nService', function (i18nService) {
var i18nDirective = {
restrict: 'EAC',
updateText: function (elm, token) {
var values = token.split('|');
if (values.length >= 1) {
// construct the tag to insert into the element
var tag = i18nService.getLocalizedString(values[0]);
// update the element only if data was returned
if ((tag !== null) && (tag !== undefined) && (tag !== '')) {
if (values.length > 1) {
for (var index = 1; index < values.length; index++) {
var target = '{' + (index - 1) + '}';
tag = tag.replace(target, values[index]);
}
}
// insert the text into the element
elm.text(tag);
}
}
},
link: function (scope, elm, attrs) {
scope.$on('i18nResourcesUpdated', function () {
i18nDirective.updateText(elm, attrs.i18n);
});
attrs.$observe('i18n', function () {
i18nDirective.updateText(elm, attrs.i18n);
});
}
};
return i18nDirective;
}])
// translation directive that can handle dynamic strings
// updates the attribute value of the attached element
// usage <span data-i18n-attr="TOKEN|ATTRIBUTE" ></span>
// or
// <span data-i18n-attr="TOKEN|ATTRIBUTE|VALUE1|VALUE2" ></span>
.directive('i18nAttr', ['i18nService', function (i18nService) {
var i18nAttrDirective = {
restrict: 'EAC',
updateText: function (elm, token) {
var values = token.split('|');
// construct the tag to insert into the element
var tag = i18nService.getLocalizedString(values[0]);
// update the element only if data was returned
if ((tag !== null) && (tag !== undefined) && (tag !== '')) {
if (values.length > 2) {
for (var index = 2; index < values.length; index++) {
var target = '{' + (index - 2) + '}';
tag = tag.replace(target, values[index]);
}
}
// insert the text into the element
elm.attr(values[1], tag);
}
},
link: function (scope, elm, attrs) {
scope.$on('i18nResourcesUpdated', function () {
i18nAttrDirective.updateText(elm, attrs.i18nAttr);
});
attrs.$observe('i18nAttr', function (value) {
i18nAttrDirective.updateText(elm, value);
});
}
};
return i18nAttrDirective;
}]);
}());