Skip to content

Commit fd32e8e

Browse files
committed
Update source file with change to handle resource file refresh
1 parent 865d1e8 commit fd32e8e

File tree

1 file changed

+31
-36
lines changed

1 file changed

+31
-36
lines changed

src/localizejs.js

Lines changed: 31 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,11 @@ angular.module('localization', []).
2727
$rootScope.$broadcast('localizeResourcesUpdates');
2828
},
2929

30+
setLanguage: function(value) {
31+
localize.language = value;
32+
localize.initLocalizedResources();
33+
},
34+
3035
initLocalizedResources:function () {
3136
// build the url to retrieve the localized resource file
3237
var url = '/i18n/resources-locale_' + localize.language + '.js';
@@ -69,47 +74,37 @@ angular.module('localization', []).
6974
return localize.getLocalizedString(input);
7075
};
7176
}]).directive('i18n', ['localize', function(localize){
72-
return {
73-
restrict: "EAC",
74-
link: function (scope, elm, attrs) {
75-
scope.$on('localizeResourcesUpdates', function() {
76-
var token = attrs.i18n;
77-
var values = token.split('|');
78-
if (values.length >= 1) {
79-
// construct the tag to insert into the element
80-
var tag = localize.getLocalizedString(values[0]);
81-
// update the element only if data was returned
82-
if ((tag !== null) && (tag !== undefined) && (tag !== '')) {
83-
if (values.length > 1) {
84-
for (var index = 1; index < values.length; index++) {
85-
var target = '{' + (index - 1) + '}';
86-
tag = tag.replace(target, values[index]);
87-
}
77+
var i18nDirective = {
78+
restrict:"EAC",
79+
updateText:function(elm, token){
80+
var values = token.split('|');
81+
if (values.length >= 1) {
82+
// construct the tag to insert into the element
83+
var tag = localize.getLocalizedString(values[0]);
84+
// update the element only if data was returned
85+
if ((tag !== null) && (tag !== undefined) && (tag !== '')) {
86+
if (values.length > 1) {
87+
for (var index = 1; index < values.length; index++) {
88+
var target = '{' + (index - 1) + '}';
89+
tag = tag.replace(target, values[index]);
8890
}
89-
// insert the text into the element
90-
elm.text(tag);
91-
};
92-
}
91+
}
92+
// insert the text into the element
93+
elm.text(tag);
94+
};
95+
}
96+
},
97+
98+
link:function (scope, elm, attrs) {
99+
scope.$on('localizeResourcesUpdates', function() {
100+
i18nDirective.updateText(elm, attrs.i18n);
93101
});
94102

95103
attrs.$observe('i18n', function (value) {
96-
var values = value.split('|');
97-
if (values.length >= 1) {
98-
// construct the tag to insert into the element
99-
var tag = localize.getLocalizedString(values[0]);
100-
// update the element only if data was returned
101-
if ((tag !== null) && (tag !== undefined) && (tag !== '')) {
102-
if (values.length > 1) {
103-
for (var index = 1; index < values.length; index++) {
104-
var target = '{' + (index - 1) + '}';
105-
tag = tag.replace(target, values[index]);
106-
}
107-
}
108-
// insert the text into the element
109-
elm.text(tag);
110-
};
111-
}
104+
i18nDirective.updateText(elm, attrs.i18n);
112105
});
113106
}
114107
};
108+
109+
return i18nDirective;
115110
}]);

0 commit comments

Comments
 (0)