| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159 |
- 'use strict';
- function _interopDefault (ex) { return (ex && (typeof ex === 'object') && 'default' in ex) ? ex['default'] : ex; }
- var fs = _interopDefault(require('fs'));
- var path = _interopDefault(require('path'));
- var File = _interopDefault(require('vinyl'));
- var through = _interopDefault(require('through2'));
- var PluginError = _interopDefault(require('plugin-error'));
- var CharCodes;
- (function (CharCodes) {
- CharCodes[CharCodes["LEFT_CURLY_BRACE"] = 123] = "LEFT_CURLY_BRACE";
- CharCodes[CharCodes["RIGHT_CURLY_BRACE"] = 125] = "RIGHT_CURLY_BRACE";
- CharCodes[CharCodes["SINGLE_QUOTE"] = 39] = "SINGLE_QUOTE";
- CharCodes[CharCodes["DOUBLE_QUOTE"] = 34] = "DOUBLE_QUOTE";
- CharCodes[CharCodes["BACK_QUOTE"] = 96] = "BACK_QUOTE";
- CharCodes[CharCodes["BACK_SLASH"] = 92] = "BACK_SLASH";
- CharCodes[CharCodes["SLASH"] = 47] = "SLASH";
- CharCodes[CharCodes["LESS_THAN"] = 60] = "LESS_THAN";
- CharCodes[CharCodes["GREATER_THAN"] = 62] = "GREATER_THAN";
- CharCodes[CharCodes["MINUS"] = 45] = "MINUS";
- CharCodes[CharCodes["UNDER_LINE"] = 95] = "UNDER_LINE";
- CharCodes[CharCodes["EQUALS"] = 61] = "EQUALS";
- CharCodes[CharCodes["EXCLAMATION"] = 33] = "EXCLAMATION";
- CharCodes[CharCodes["COLON"] = 58] = "COLON";
- CharCodes[CharCodes["LEFT_PAREN"] = 40] = "LEFT_PAREN";
- CharCodes[CharCodes["RIGHT_PAREN"] = 41] = "RIGHT_PAREN";
- CharCodes[CharCodes["DOT"] = 46] = "DOT";
- CharCodes[CharCodes["ASTERISK"] = 42] = "ASTERISK";
- CharCodes[CharCodes["TAB"] = 9] = "TAB";
- CharCodes[CharCodes["LINE_FEED"] = 10] = "LINE_FEED";
- CharCodes[CharCodes["CARRIAGE_RETURN"] = 13] = "CARRIAGE_RETURN";
- CharCodes[CharCodes["SPACE"] = 32] = "SPACE";
- CharCodes[CharCodes["UPPER_A"] = 65] = "UPPER_A";
- CharCodes[CharCodes["UPPER_Z"] = 90] = "UPPER_Z";
- CharCodes[CharCodes["LOWER_A"] = 97] = "LOWER_A";
- CharCodes[CharCodes["LOWER_Z"] = 122] = "LOWER_Z";
- CharCodes[CharCodes["_0"] = 48] = "_0";
- CharCodes[CharCodes["_9"] = 57] = "_9";
- CharCodes[CharCodes["$"] = 36] = "$";
- })(CharCodes || (CharCodes = {}));
- const parse = require('format-message-parse');
- /**
- * Use parser from format-message-parse
- */
- function parseTranslation(pattern) {
- return parse(pattern);
- }
- var WxmlState;
- (function (WxmlState) {
- WxmlState[WxmlState["NORMAL"] = 1] = "NORMAL";
- WxmlState[WxmlState["WXS"] = 2] = "WXS";
- WxmlState[WxmlState["INT"] = 4] = "INT";
- })(WxmlState || (WxmlState = {}));
- var TranslationFunction;
- (function (TranslationFunction) {
- TranslationFunction["default"] = "t";
- })(TranslationFunction || (TranslationFunction = {}));
- var I18nModuleName;
- (function (I18nModuleName) {
- I18nModuleName["default"] = "i18n";
- })(I18nModuleName || (I18nModuleName = {}));
- var LocaleVariable;
- (function (LocaleVariable) {
- LocaleVariable["default"] = "$_locale";
- })(LocaleVariable || (LocaleVariable = {}));
- const PLUGIN_NAME = '@miniprogram-i18n/gulp-i18n-locales';
- const DEFAULT_WXS_FILENAME = 'locales.wxs';
- const DEFAULT_JS_FILENAME = 'locales.js';
- const DEFAULT_LOCALE = 'en-US';
- const DEFAULT_FALLBACK_LOCALE = 'en-US';
- const CORE_PATH = path.dirname(require.resolve('@miniprogram-i18n/core/package.json'));
- function getWxsCode() {
- const code = fs.readFileSync(path.join(CORE_PATH, '/wxs.js'), 'utf-8');
- // FIXME: function name maybe mangled
- const runner = `module.exports.t = Interpreter.getMessageInterpreter(translations, fallbackLocale)`;
- return [code, runner].join('\n');
- }
- let firstFile;
- const localeFile = {};
- function parseTranslations(object) {
- const keys = Object.keys(object);
- for (const key of keys) {
- const val = object[key];
- if (typeof val === 'string') {
- object[key] = parseTranslation(val);
- }
- // TODO: this is currently not supported
- if (typeof val === 'object') {
- object[key] = parseTranslations(val);
- }
- }
- return object;
- }
- const gulpI18nLocalesLoader = (options) => {
- if (!options)
- options = {};
- function buffer(file, _, cb) {
- // Ignore empty file or not json files
- if (file.isNull() || path.extname(file.path) !== '.json') {
- return cb(null, file);
- }
- if (file.isStream()) {
- return cb(new PluginError(PLUGIN_NAME, 'Streaming data is not supported'));
- }
- if (!file.contents) {
- return cb(null, file);
- }
- if (!firstFile)
- firstFile = file;
- const basename = path.basename(file.path);
- const localeName = basename.split('.')[0];
- let content = null;
- try {
- content = JSON.parse(file.contents.toString('utf-8'));
- if (!localeFile[localeName])
- localeFile[localeName] = {};
- localeFile[localeName] = Object.assign({}, localeFile[localeName], parseTranslations(content));
- }
- catch (err) {
- return cb(new PluginError(PLUGIN_NAME, err));
- }
- cb();
- }
- function endStream(cb) {
- if (!firstFile)
- return cb();
- const wxsFileName = options && options.wxsFileName || DEFAULT_WXS_FILENAME;
- const jsFileName = options && options.jsFileName || DEFAULT_JS_FILENAME;
- const localeString = JSON.stringify(localeFile);
- const defaultLocale = options && options.defaultLocale || DEFAULT_LOCALE;
- const fallbackLocale = options && options.fallbackLocale || DEFAULT_FALLBACK_LOCALE;
- const wxsContent = `var fallbackLocale='${fallbackLocale}';var translations=${localeString};\n${getWxsCode()}`;
- const jsContent = `module.exports.fallbackLocale='${fallbackLocale}';module.exports.defaultLocale='${defaultLocale}';module.exports.translations=${localeString};`;
- const wxsFile = new File({
- cwd: firstFile.cwd,
- base: firstFile.base,
- path: path.join(firstFile.base, wxsFileName),
- contents: Buffer.from(wxsContent),
- });
- const jsFile = new File({
- cwd: firstFile.cwd,
- base: firstFile.base,
- path: path.join(firstFile.base, jsFileName),
- contents: Buffer.from(jsContent),
- });
- this.push(wxsFile);
- this.push(jsFile);
- cb();
- }
- return through.obj(buffer, endStream);
- };
- module.exports = gulpI18nLocalesLoader;
|