forked from ember-intl/ember-intl
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
157 lines (126 loc) · 4.14 KB
/
index.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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
/* jshint node: true */
/**
* Copyright 2015, Yahoo! Inc.
* Copyrights licensed under the New BSD License. See the accompanying LICENSE file for terms.
*/
'use strict';
var serialize = require('serialize-javascript');
var Funnel = require('broccoli-funnel');
var WatchedDir = require('broccoli-source').WatchedDir;
var UnwatchedDir = require('broccoli-source').UnwatchedDir;
var existsSync = require('exists-sync');
var stew = require('broccoli-stew');
var walkSync = require('walk-sync');
var path = require('path');
var fs = require('fs');
var utils = require('./lib/utils');
var mergeTrees = require('./lib/broccoli/merge-trees');
var CldrWriter = require('./lib/broccoli/cldr-writer');
var lowercaseTree = require('./lib/broccoli/lowercase-tree');
var TranslationPreprocessor = require('./lib/broccoli/translation-preprocessor');
function generateOptions(app) {
var addonConfig = app.project.config(app.env)['intl'] || {};
var options = utils.assign({
locales: undefined,
disablePolyfill: false,
defaultLocale: 'en-us',
allowEmpty: true,
inputPath: 'translations',
outputPath: 'translations'
}, addonConfig);
if (options.locales) {
options.locales = utils.makeArray(options.locales).filter(function(locale) {
return typeof locale === 'string';
}).map(function(locale) {
return locale.toLocaleLowerCase();
});
}
return options;
};
module.exports = {
name: 'ember-intl',
included: function(app) {
this._super.included.apply(this, arguments);
if (!this.app) {
this.app = app;
}
this.addonOptions = generateOptions(app);
this.locales = this._discoverLocales();
this.trees = {
translations: new WatchedDir(this.addonOptions.inputPath),
intl: new UnwatchedDir(path.dirname(require.resolve('intl')))
};
},
treeForApp: function(tree) {
var trees = [tree, new TranslationPreprocessor(this.trees.translations, this.addonOptions)];
if (tree && this.locales.length) {
trees.push(new CldrWriter([tree], {
path: './cldrs',
locales: this.locales,
pluralRules: true,
relativeFields: true,
prelude: '/*jslint eqeq: true*/\n',
wrapEntry: function wrapEntry(result) {
return 'export default ' + serialize(result) + ';';
}
}));
}
return mergeTrees(trees, {
overwrite: true,
annotation: 'TreeMerger (CLDR)'
});
},
treeForPublic: function() {
var publicTree = this._super.treeForPublic.apply(this, arguments);
if (this.addonOptions.disablePolyfill) {
return publicTree;
}
var intlPath = path.dirname(require.resolve('intl'));
var assetPath = path.join('assets', 'intl');
var appOptions = this.app.options;
var trees = [];
if (appOptions.app && appOptions.app.intl) {
assetPath = appOptions.app.intl;
}
if (publicTree) {
trees.push(publicTree);
}
trees.push(new Funnel(this.trees.intl, {
srcDir: 'dist',
files: ['Intl.js.map'],
destDir: path.join(assetPath)
}));
trees.push(lowercaseTree(new Funnel(this.trees.intl, {
srcDir: 'dist',
files: ['Intl.complete.js', 'Intl.js', 'Intl.min.js'],
destDir: path.join(assetPath)
})));
var localeFunnel = {
srcDir: path.join('locale-data', 'jsonp'),
destDir: path.join(assetPath, 'locales')
};
if (this.locales.length) {
localeFunnel.include = this.locales.map(function(locale) {
return new RegExp(locale, 'i');
});
}
trees.push(lowercaseTree(new Funnel(this.trees.intl, localeFunnel)));
return mergeTrees(trees, {
overwrite: true,
annotation: 'TreeMerger (Intl.js)'
});
},
_discoverLocales: function() {
var translations = path.join(this.project.root, this.addonOptions.inputPath);
var locales = [];
if (existsSync(translations)) {
locales = walkSync(translations).map(function(filename) {
return path.basename(filename, path.extname(filename));
}).filter(utils.isSupportedLocale);
}
if (this.addonOptions.locales) {
locales = locales.concat(this.addonOptions.locales);
}
return utils.uniqueByString(locales);
}
};