This repository was archived by the owner on Sep 9, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathangular-localizer.js
More file actions
143 lines (125 loc) · 4.77 KB
/
angular-localizer.js
File metadata and controls
143 lines (125 loc) · 4.77 KB
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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
/*
* Angular-Localizer
* Copyright (C)2014 2Toad, LLC.
* http://2toad.github.io/Angular-Localizer
*
* Version: 1.1.0
* License: MIT
*/
(function () {
"use strict";
angular.module("ttLocalizer", [])
.factory("localizer", ["$q", "$http", function ($q, $http) {
var service = {
translationsUrl: "/App/i18n/",
defaultLocale: "en-US",
locale: undefined,
floodDelay: 5000,
init: initializeService,
translate: lookupTranslation,
translateAll: lookupMultipleTranslations,
errorHandler: undefined
},
cache = {},
lastError = {
url: undefined,
time: undefined
};
return service;
function initializeService(config) {
angular.extend(service, config);
service.locale = service.defaultLocale;
}
function getTranslations(category) {
var key = category + "." + service.locale;
var loaded = cache[key];
return loaded ? $q.when(loaded) : loadTranslations(key);
function loadTranslations(key) {
var fileUrl = service.translationsUrl + key + ".js";
var deferred = $q.defer();
$http({ url: fileUrl, cache: true })
.success(function (data) {
cache[key] = data;
deferred.resolve(data);
})
.error(function (data, status) {
defaultErrorHandler(fileUrl, data, status);
deferred.reject();
});
return deferred.promise;
}
}
function lookupTranslation(key) {
return function (category, name, callback) {
return getTranslations(category).then(
function (data) {
var translation = data.filter(function (t) {
return t.name === name;
})[0];
return translation ? translation[key || "value"] : "{TRANSLATION_MISSING}";
}
).then(callback || $q.when);
}
}
function lookupMultipleTranslations(category, names, callback) {
var promises = names.map(function (n) {
return lookupTranslation()(category, n);
});
return $q.all(promises).then(function (t) {
callback(convertToObject(names, t));
});
function convertToObject(names, values) {
var result = {};
for (var i = 0; i < names.length; i++) {
result[names[i]] = values[i];
}
return result;
}
}
function defaultErrorHandler(fileUrl, data, status) {
if (!preventMessageFlooding()) {
service.errorHandler
? service.errorHandler(fileUrl, data, status)
: console.error("Angular-Localizer Default Error Handler\r\n\r\n" +
"Unable to load localization file: " + fileUrl + "\r\n" +
"Status:" + status);
}
function preventMessageFlooding() {
if (lastError.url === fileUrl && lastError.time > now()) {
return true;
} else {
lastError.url = fileUrl;
lastError.time = now(service.floodDelay);
return false;
}
function now(duration) {
return (new Date()).getTime() + (duration || 0);
}
}
}
}])
.directive("i18n", ["localizer", function (localizer) {
return {
restrict: "E",
link: function (scope, $element, attrs) {
localizer.translate()(attrs.category, attrs.name, function (t) {
$element.append(t);
});
}
};
}])
.directive("i18n", ["localizer", function (localizer) {
return {
restrict: "A",
link: function (scope, $element, attrs) {
var cn = attrs.i18n.split(".");
var output = attrs.i18nOutput || "text";
localizer.translate()(cn[0], cn[1], function (t) {
if (attrs.i18nTarget) scope[attrs.i18nTarget] = t;
else if (output == "text") $element.append(t);
else $element.attr(output, t);
});
}
};
}]);
}());