forked from waldur/waldur-homeport
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathangular-gettext-compiler.js
More file actions
105 lines (89 loc) · 2.91 KB
/
Copy pathangular-gettext-compiler.js
File metadata and controls
105 lines (89 loc) · 2.91 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
// Based on https://raw.githubusercontent.com/rubenv/angular-gettext-tools/master/lib/compile.js
// It supports one locale per file
'use strict';
var po = require('pofile');
var _ = require('lodash');
var noContext = '$$noContext';
var Compiler = (function () {
function Compiler() {}
Compiler.browserConvertedHTMLEntities = {
'hellip': '…',
'cent': '¢',
'pound': '£',
'euro': '€',
'laquo': '«',
'raquo': '»',
'rsaquo': '›',
'lsaquo': '‹',
'copy': '©',
'reg': '®',
'trade': '™',
'sect': '§',
'deg': '°',
'plusmn': '±',
'para': '¶',
'middot': '·',
'ndash': '–',
'mdash': '—',
'lsquo': '‘',
'rsquo': '’',
'sbquo': '‚',
'ldquo': '“',
'rdquo': '”',
'bdquo': '„',
'dagger': '†',
'Dagger': '‡',
'bull': '•',
'prime': '′',
'Prime': '″',
'asymp': '≈',
'ne': '≠',
'le': '≤',
'ge': '≥',
'sup2': '²',
'sup3': '³',
'frac12': '½',
'frac14': '¼',
'frac13': '⅓',
'frac34': '¾'
};
Compiler.hasFormat = function (format) {
return format === 'json';
};
Compiler.prototype.convertPo = function (content) {
var catalog = po.parse(content);
if (!catalog.headers.Language) {
throw new Error('No Language header found!');
}
var strings = {};
for (var i = 0; i < catalog.items.length; i++) {
var item = catalog.items[i];
var ctx = item.msgctxt || noContext;
var msgid = item.msgid;
var convertedEntity;
var unconvertedEntity;
var unconvertedEntityPattern;
for ( unconvertedEntity in Compiler.browserConvertedHTMLEntities ) {
convertedEntity = Compiler.browserConvertedHTMLEntities[ unconvertedEntity ];
unconvertedEntityPattern = new RegExp( '&' + unconvertedEntity + ';?', 'g' );
msgid = msgid.replace( unconvertedEntityPattern, convertedEntity );
}
if (item.msgstr[0].length > 0 && !item.flags.fuzzy && !item.obsolete) {
if (!strings[msgid]) {
strings[msgid] = {};
}
// Add array for plural, single string for signular.
strings[msgid][ctx] = item.msgstr.length === 1 ? item.msgstr[0] : item.msgstr;
}
}
// Strip context from strings that have no context.
for (var key in strings) {
if (Object.keys(strings[key]).length === 1 && strings[key][noContext]) {
strings[key] = strings[key][noContext];
}
}
return JSON.stringify(strings);
};
return Compiler;
})();
module.exports = Compiler;