forked from messageformat/messageformat
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
47 lines (45 loc) · 1.53 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
const loaderUtils = require('loader-utils');
const MessageFormat = require('messageformat');
const compileModule = require('messageformat/compile-module');
const convert = require('messageformat-convert');
const path = require('path');
const YAML = require('yaml');
function localeFromResourcePath(resourcePath, locales) {
const parts = resourcePath.split(/[._/\\]+/);
let locale = null;
let lcPos = -1;
for (const lc of locales) {
const idx = parts.indexOf(lc);
if (idx > lcPos) {
locale = lc;
lcPos = idx;
}
}
return locale || locales;
}
module.exports = function loadMessages(content) {
var messages = YAML.parse(content);
var options = loaderUtils.getOptions(this) || {};
var locale = options.locale;
if (options.convert) {
var cm = convert(messages, options.convert);
if (!locale) {
locale = cm.locales;
}
messages = cm.translations;
}
if (typeof locale === 'string' && locale.indexOf(',') !== -1)
locale = locale.split(',');
if (Array.isArray(locale) && locale.length > 1) {
var relPath = path.relative(process.cwd(), this.resourcePath);
locale = localeFromResourcePath(relPath, locale);
}
const mfOpt = {};
if (options.biDiSupport) mfOpt.biDiSupport = true;
if (options.customFormatters || options.formatters) {
mfOpt.customFormatters = options.customFormatters || options.formatters;
}
if (options.strictNumberSign) mfOpt.strictNumberSign = true;
var messageFormat = new MessageFormat(locale, mfOpt);
return compileModule(messageFormat, messages);
};