forked from janschoenherr/vue-intl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuild.locales.js
110 lines (81 loc) · 2.75 KB
/
build.locales.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
var fs = require('fs');
var md5 = require('md5');
var glob = require('glob');
var path = require('path');
var plural = path.join(__dirname, 'src/plural.js');
var output = path.join(__dirname, 'dist/locales/');
var relative = path.join(path.dirname(require.resolve('twitter_cldr')),'full/');
var source = false;
module.paths.forEach(function (modules) {
var p = path.join(modules,'angular-i18n/');
if (fs.existsSync(p)) {
source = p;
}
});
if (!source) {
console.log('Unable to find source data.');
return;
}
global.angular = {
locales: [],
plurals: {},
module: function (name, requires, config) {
var fn = config[1];
fn.call(fn, this);
},
value: function (name, value) {
var id = value.id.match(/^\w+/i)[0],
fn = value.pluralCat.toString(),
key = md5(fn), file, data;
if (this.locales.indexOf(id) === -1) {
if (!this.plurals[key]) {
this.plurals[key] = {fn: fn, locales: [id]};
} else if (this.plurals[key].locales.indexOf(id) === -1) {
this.plurals[key].locales.push(id);
}
this.locales.push(id);
}
delete value.pluralCat;
[value.id, id, 'en'].forEach(function(locale) {
var path = relative+locale+'.js';
if (!file && fs.existsSync(path)) {
file = path;
id = locale;
}
});
if (file) {
value.TIMESPAN_FORMATS = (require(file).TimespanFormatter).patterns;
value.TIMESPAN_FORMATS.localeID = id;
}
file = output + value.id + '.json';
data = JSON.stringify(value);
fs.writeFileSync(file, data);
}
};
console.log('> Generating locale files ...');
if (!fs.existsSync(output)) {
fs.mkdirSync(output);
}
glob.sync('angular-locale*', {cwd: source}).forEach(function (file) {
require(source + file);
});
console.log('> Updating plural.js ...');
var rules = [];
var locales = [];
var content = fs.readFileSync(plural, 'utf8');
var convert = function (value) {
return "'" + value + "'";
};
Object.keys(angular.plurals).forEach(function (key) {
var data = angular.plurals[key];
// skip same locales from 'en', because it's the default
if (data.locales.indexOf('en') !== -1) {
data.locales = ['en'];
}
locales.push('[' + data.locales.map(convert).toString() + ']');
rules.push(data.fn.replace(/opt_precision/gi, 'precision'));
});
content = content.replace(/(var PLURAL_LOCALES = \[)[^]*(\]; \/\/ END LOCALES)/gm, '$1' + locales.toString() + '$2');
content = content.replace(/(var PLURAL_RULES = \[)[^]*(\]; \/\/ END RULES)/gm, '$1' + rules.toString() + '$2');
fs.writeFileSync(plural, content);
console.log('');