-
Notifications
You must be signed in to change notification settings - Fork 152
Expand file tree
/
Copy pathcatalog.js
More file actions
140 lines (120 loc) · 4.76 KB
/
catalog.js
File metadata and controls
140 lines (120 loc) · 4.76 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
angular.module('gettext').factory('gettextCatalog', function (gettextPlurals, $http, $cacheFactory, $interpolate, $rootScope) {
var catalog;
var noContext = '$$noContext';
// IE8 returns UPPER CASE tags, even though the source is lower case.
// This can causes the (key) string in the DOM to have a different case to
// the string in the `po` files.
// IE9, IE10 and IE11 reorders the attributes of tags.
var test = '<span id="test" title="test" class="tested">test</span>';
var isHTMLModified = (angular.element('<span>' + test + '</span>').html() !== test);
var prefixDebug = function (string) {
if (catalog.debug && catalog.currentLanguage !== catalog.baseLanguage) {
return catalog.debugPrefix + string;
} else {
return string;
}
};
var addTranslatedMarkers = function (string) {
if (catalog.showTranslatedMarkers) {
return catalog.translatedMarkerPrefix + string + catalog.translatedMarkerSuffix;
} else {
return string;
}
};
// Trim polyfill for old browsers (instead of jQuery)
// Based on AngularJS-v1.2.2 (angular.js#620)
var trim = (function () {
if (!String.prototype.trim) {
return function (value) {
return (typeof value === 'string') ? value.replace(/^\s*/, '').replace(/\s*$/, '') : value;
};
}
return function (value) {
return (typeof value === 'string') ? value.trim() : value;
};
})();
function broadcastUpdated() {
$rootScope.$broadcast('gettextLanguageChanged');
}
catalog = {
debug: false,
debugPrefix: '[MISSING]: ',
showTranslatedMarkers: false,
translatedMarkerPrefix: '[',
translatedMarkerSuffix: ']',
idTransform: function (s) { return trim(s); },
strings: {},
baseLanguage: 'en',
currentLanguage: 'en',
cache: $cacheFactory('strings'),
setIdTransform: function (idTransform) {
this.idTransform = idTransform;
broadcastUpdated();
},
setCurrentLanguage: function (lang) {
this.currentLanguage = lang;
broadcastUpdated();
},
getCurrentLanguage: function () {
return this.currentLanguage;
},
setStrings: function (language, strings) {
if (!this.strings[language]) {
this.strings[language] = {};
}
for (var key in strings) {
var val = strings[key];
if (isHTMLModified) {
// Use the DOM engine to render any HTML in the key (#131).
key = angular.element('<span>' + key + '</span>').html();
}
if (angular.isString(val) || angular.isArray(val)) {
// No context, wrap it in $$noContext.
var obj = {};
obj[noContext] = val;
val = obj;
}
// Expand single strings for each context.
for (var context in val) {
var str = val[context];
val[context] = angular.isArray(str) ? str : [str];
}
this.strings[language][key] = val;
}
broadcastUpdated();
},
getStringForm: function (string, n, context) {
var msgId = (typeof string === 'string') ? this.idTransform(string) : string;
var stringTable = this.strings[this.currentLanguage] || {};
var contexts = stringTable[msgId] || {};
var plurals = contexts[context || noContext] || [];
return plurals[n];
},
getString: function (string, scope, context) {
string = this.getStringForm(string, 0, context) || prefixDebug(string);
string = scope ? $interpolate(string)(scope) : string;
return addTranslatedMarkers(string);
},
getPlural: function (n, string, stringPlural, scope, context) {
var form = gettextPlurals(this.currentLanguage, n);
string = this.getStringForm(string, form, context) || prefixDebug(n === 1 ? string : stringPlural);
if (scope) {
scope.$count = n;
string = $interpolate(string)(scope);
}
return addTranslatedMarkers(string);
},
loadRemote: function (url) {
return $http({
method: 'GET',
url: url,
cache: catalog.cache
}).success(function (data) {
for (var lang in data) {
catalog.setStrings(lang, data[lang]);
}
});
}
};
return catalog;
});