forked from ember-intl/ember-intl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
239 lines (195 loc) · 6.41 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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
/* eslint-env node */
/**
* Copyright 2015, Yahoo! Inc.
* Copyrights licensed under the New BSD License. See the accompanying LICENSE file for terms.
*/
'use strict';
const fs = require('fs');
const path = require('path');
const walkSync = require('walk-sync');
const mergeTrees = require('broccoli-merge-trees');
const stringify = require('json-stable-stringify');
const extract = require('@ember-intl/broccoli-cldr-data');
const buildTree = require('./lib/broccoli/build-translation-tree');
const TranslationReducer = require('./lib/broccoli/translation-reducer');
const isValidLocaleFormat = require('./lib/utils/is-valid-locale-format');
const DEFAULT_CONFIG = {
locales: null,
publicOnly: false,
fallbackLocale: null,
autoPolyfill: false,
disablePolyfill: false,
inputPath: 'translations',
outputPath: 'translations',
errorOnMissingTranslations: false,
errorOnNamedArgumentMismatch: false,
requiresTranslation(/* key, locale */) {
return true;
}
};
module.exports = {
name: 'ember-intl',
opts: null,
isLocalizationFramework: true,
included() {
this._super.included.apply(this, arguments);
this.app = this._findHost();
this.opts = this.createOptions(this.app.env, this.app.project);
this.isSilent = this.app.options.intl && this.app.options.intl.silent;
this.locales = this.findAvailableLocales();
},
generateTranslationTree(bundlerOptions) {
const translationTree = buildTree(this.project, this.opts.inputPath, this.treeGenerator);
const _bundlerOptions = bundlerOptions || {};
const addon = this;
if ('throwMissingTranslations' in this.opts) {
this.log('DEPRECIATION: `throwMissingTranslations` has been renamed to `errorOnMissingTranslations`', {
warning: true
});
}
return new TranslationReducer(translationTree, {
verbose: !this.isSilent,
outputPath: this.opts.outputPath,
fallbackLocale: this.opts.fallbackLocale,
filename: _bundlerOptions.filename,
wrapEntry: _bundlerOptions.wrapEntry,
requiresTranslation: this.opts.requiresTranslation,
errorOnMissingTranslations: this.opts.throwMissingTranslations || this.opts.errorOnMissingTranslations,
errorOnNamedArgumentMismatch: this.opts.errorOnNamedArgumentMismatch,
stripEmptyTranslations: this.opts.stripEmptyTranslations,
log() {
return addon.log.apply(addon, arguments);
}
});
},
findAssetPath(appOptions) {
if (appOptions.app && appOptions.app.intl) {
return appOptions.app.intl;
}
return 'assets/intl';
},
contentFor(name, config) {
if (name === 'head' && !this.opts.disablePolyfill && this.opts.autoPolyfill) {
let assetPath = this.findAssetPath(this.app.options);
let locales = this.locales;
let prefix = '';
if (config.rootURL) {
prefix += config.rootURL;
}
if (assetPath) {
prefix += assetPath;
}
let localeScripts = locales.map(function(locale) {
return `<script src="${prefix}/locales/${locale}.js"></script>`;
});
return [`<script src="${prefix}/intl.min.js"></script>`].concat(localeScripts).join('\n');
}
},
treeForApp(tree) {
let trees = [tree];
if (!this.opts.publicOnly) {
trees.push(
this.generateTranslationTree({
outputPath: 'translations',
filename(key) {
return `${key}.js`;
},
wrapEntry(obj) {
return `export default ${stringify(obj)};`;
}
})
);
}
if (tree && this.locales.length) {
let cldrTree = extract(tree, {
locales: this.locales,
relativeFields: true,
destDir: 'cldrs',
prelude: '/*jslint eqeq: true*/\n',
moduleType: 'es6'
});
trees.push(cldrTree);
}
return mergeTrees(trees, { overwrite: true });
},
treeForPublic() {
let trees = [];
if (!this.opts.disablePolyfill) {
let appOptions = this.app.options || {};
trees.push(
require('./lib/broccoli/intl-polyfill')({
locales: this.locales,
destDir: (appOptions.app && appOptions.app.intl) || 'assets/intl'
})
);
}
if (this.opts.publicOnly) {
trees.push(this.generateTranslationTree());
}
return mergeTrees(trees, { overwrite: true });
},
log(msg, options) {
if (this.isSilent) {
return;
}
if (options && options.warning && this.ui.writeWarnLine) {
this.ui.writeWarnLine(`[ember-intl] ${msg}`);
} else {
this.ui.writeLine(`[ember-intl] ${msg}`);
}
},
readConfig(environment, project) {
// NOTE: For ember-cli >= 2.6.0-beta.3, project.configPath() returns absolute path
// while older ember-cli versions return path relative to project root
let configPath = path.dirname(project.configPath());
let config = path.join(configPath, 'ember-intl.js');
if (!path.isAbsolute(config)) {
config = path.join(project.root, config);
}
if (fs.existsSync(config)) {
return require(config)(environment);
}
return {};
},
createOptions(environment, project) {
let config = Object.assign({}, DEFAULT_CONFIG, this.readConfig(environment, project));
if (typeof config.requiresTranslation !== 'function') {
this.log('Configured `requiresTranslation` is not a function. Using default implementation.');
config.requiresTranslation = DEFAULT_CONFIG.requiresTranslation;
}
if (config.locales && !Array.isArray(config.locales)) {
config.locales = [config.locales];
}
return config;
},
findAvailableLocales() {
let locales = [];
let projectInputPath = path.join(this.app.project.root, this.opts.inputPath);
if (fs.existsSync(projectInputPath)) {
locales.push(
...walkSync(projectInputPath, {
globs: ['**/*.{yml,yaml,json}'],
directories: false
}).map(function(filename) {
return path.basename(filename, path.extname(filename));
})
);
}
if (Array.isArray(this.opts.locales)) {
locales.push(...this.opts.locales);
}
let normalizedLocales = locales.map(locale => {
return locale
.trim()
.toLowerCase()
.replace(/_/g, '-');
});
return [...new Set(normalizedLocales).values()].filter(locale => {
if (isValidLocaleFormat(locale)) {
return true;
}
this.log(`'${locale}' is not a valid locale name`);
return false;
});
}
};