-
Notifications
You must be signed in to change notification settings - Fork 0
/
nghtmled.js
129 lines (116 loc) · 5.21 KB
/
nghtmled.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
/* ngHTMLed.js ver 0.1
* library written by Niranjan Kumar - A.K.A - N!Ru$
* this library requires Angular JS to be loaded as a dependency module
* at Config stage setup your language configurtion
* Your ".properties" file should be in JSON format.
* Your ".properties" file name should be exactly the browser returned language.
* goto http://jsfiddle.net/nirus/9FVTf/ to get the file name you want to set based on your browser eg: en-US.properties for english US language
* contact me for any queries : nirus@live.in
*/
//: Minify Task `grunt build-lib`
angular.module('ngHTMLed', ['ng'])
.provider("$HTMLed", ["$httpProvider", function($httpProvider) {
/**
* Global object that will be exposed
*/
var _promise, htmled = {
json: {},
config: {}
},
intercept = ["$q", "$templateCache", function($q, $templateCache) {
return {
"response": function(response) {
if (Object.getOwnPropertyNames(htmled.json).length === 0) {
return $q(function(resolve, reject) {
_promise
.then(function(resp) {
htmled.json = resp.data;
resolve(response);
})
.catch(function(err) {
console.error("ngHTMLed Reports: \n\tCode:" + err.status + "\n\tMessage: Error with Properties file " + err.statusText);
reject(response);
});
});
}
//Default return value
return response;
}
}
}];
//Add the interceptor
$httpProvider.interceptors.push(intercept);
this.setup = function(opt) {
var $http = angular.injector(['ng']).get('$http');
if (!opt.langBundle) {
console.error("ngHTMLed: Please specify your bundle folder.");
return;
}
if (opt.autoDetect) {
htmled.config = {
properties: opt.langBundle.toString() + '/' + (window.navigator.language || navigator.language || navigator.userLanguage).toString() + '.properties',
language: (window.navigator.language || navigator.language || navigator.userLanguage).toString()
}
} else if (opt.langPref) {
htmled.config = {
properties: opt.langBundle.toString() + '/' + opt.langPref + '.properties',
language: opt.langPref
}
} else {
console.error("ngHTMLed: Please specify langPref in your configuration object.")
return;
}
_promise = $http.get(htmled.config.properties, { responseType: "json" })
}
//Provider inteface
this.$get = function() {
return {
configuration: function() {
return htmled.config;
},
bundle: function(key) {
if(key){
return htmled.json[key];
}
return htmled.json;
}
};
}
}])
.directive("strings", ["$compile", "$HTMLed", function($compile, $HTMLed) {
return {
retrict: 'E',
link: function(scope, $el, attr) {
var $parent = $el.parent(), bundle = $HTMLed.bundle();
/**
* formats the string that supports format attribute
* @param {String} str
* @return {String} str
*/
function format(str) {
var txt = str.match(new RegExp(/%txt{([^}]+)}/g));
txt.forEach(function(el, index, arr) {
var key = (el.match("\{'(.*?)'\}") || el.match("\{\"(.*?)\"\}")).pop(), re = new RegExp(el, "gi");
if (!bundle[key]) {
console.error(key + " is missing from your properties file. Will be resolved as undefined!");
} else {
str = str.replace(re, bundle[key]);
}
});
return str;
};
$parent.children().remove();
if (bundle[attr.textId]) {
$compile($parent.text(bundle[attr.textId]))(scope);
} else if (!attr.hasOwnProperty("format")) {
if (!!$el.text()) {
$compile($parent.text($el.text()))(scope);
} else {
console.error("Please check the text-id mapping. No matching found in properties for: " + attr.textId);
}
} else {
$compile($parent.text(format($el.text())))(scope);
}
}
}
}]);