index.js 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. 'use strict';
  2. function _interopDefault (ex) { return (ex && (typeof ex === 'object') && 'default' in ex) ? ex['default'] : ex; }
  3. var fs = _interopDefault(require('fs'));
  4. var path = _interopDefault(require('path'));
  5. var File = _interopDefault(require('vinyl'));
  6. var through = _interopDefault(require('through2'));
  7. var PluginError = _interopDefault(require('plugin-error'));
  8. var CharCodes;
  9. (function (CharCodes) {
  10. CharCodes[CharCodes["LEFT_CURLY_BRACE"] = 123] = "LEFT_CURLY_BRACE";
  11. CharCodes[CharCodes["RIGHT_CURLY_BRACE"] = 125] = "RIGHT_CURLY_BRACE";
  12. CharCodes[CharCodes["SINGLE_QUOTE"] = 39] = "SINGLE_QUOTE";
  13. CharCodes[CharCodes["DOUBLE_QUOTE"] = 34] = "DOUBLE_QUOTE";
  14. CharCodes[CharCodes["BACK_QUOTE"] = 96] = "BACK_QUOTE";
  15. CharCodes[CharCodes["BACK_SLASH"] = 92] = "BACK_SLASH";
  16. CharCodes[CharCodes["SLASH"] = 47] = "SLASH";
  17. CharCodes[CharCodes["LESS_THAN"] = 60] = "LESS_THAN";
  18. CharCodes[CharCodes["GREATER_THAN"] = 62] = "GREATER_THAN";
  19. CharCodes[CharCodes["MINUS"] = 45] = "MINUS";
  20. CharCodes[CharCodes["UNDER_LINE"] = 95] = "UNDER_LINE";
  21. CharCodes[CharCodes["EQUALS"] = 61] = "EQUALS";
  22. CharCodes[CharCodes["EXCLAMATION"] = 33] = "EXCLAMATION";
  23. CharCodes[CharCodes["COLON"] = 58] = "COLON";
  24. CharCodes[CharCodes["LEFT_PAREN"] = 40] = "LEFT_PAREN";
  25. CharCodes[CharCodes["RIGHT_PAREN"] = 41] = "RIGHT_PAREN";
  26. CharCodes[CharCodes["DOT"] = 46] = "DOT";
  27. CharCodes[CharCodes["ASTERISK"] = 42] = "ASTERISK";
  28. CharCodes[CharCodes["TAB"] = 9] = "TAB";
  29. CharCodes[CharCodes["LINE_FEED"] = 10] = "LINE_FEED";
  30. CharCodes[CharCodes["CARRIAGE_RETURN"] = 13] = "CARRIAGE_RETURN";
  31. CharCodes[CharCodes["SPACE"] = 32] = "SPACE";
  32. CharCodes[CharCodes["UPPER_A"] = 65] = "UPPER_A";
  33. CharCodes[CharCodes["UPPER_Z"] = 90] = "UPPER_Z";
  34. CharCodes[CharCodes["LOWER_A"] = 97] = "LOWER_A";
  35. CharCodes[CharCodes["LOWER_Z"] = 122] = "LOWER_Z";
  36. CharCodes[CharCodes["_0"] = 48] = "_0";
  37. CharCodes[CharCodes["_9"] = 57] = "_9";
  38. CharCodes[CharCodes["$"] = 36] = "$";
  39. })(CharCodes || (CharCodes = {}));
  40. const parse = require('format-message-parse');
  41. /**
  42. * Use parser from format-message-parse
  43. */
  44. function parseTranslation(pattern) {
  45. return parse(pattern);
  46. }
  47. var WxmlState;
  48. (function (WxmlState) {
  49. WxmlState[WxmlState["NORMAL"] = 1] = "NORMAL";
  50. WxmlState[WxmlState["WXS"] = 2] = "WXS";
  51. WxmlState[WxmlState["INT"] = 4] = "INT";
  52. })(WxmlState || (WxmlState = {}));
  53. var TranslationFunction;
  54. (function (TranslationFunction) {
  55. TranslationFunction["default"] = "t";
  56. })(TranslationFunction || (TranslationFunction = {}));
  57. var I18nModuleName;
  58. (function (I18nModuleName) {
  59. I18nModuleName["default"] = "i18n";
  60. })(I18nModuleName || (I18nModuleName = {}));
  61. var LocaleVariable;
  62. (function (LocaleVariable) {
  63. LocaleVariable["default"] = "$_locale";
  64. })(LocaleVariable || (LocaleVariable = {}));
  65. const PLUGIN_NAME = '@miniprogram-i18n/gulp-i18n-locales';
  66. const DEFAULT_WXS_FILENAME = 'locales.wxs';
  67. const DEFAULT_JS_FILENAME = 'locales.js';
  68. const DEFAULT_LOCALE = 'en-US';
  69. const DEFAULT_FALLBACK_LOCALE = 'en-US';
  70. const CORE_PATH = path.dirname(require.resolve('@miniprogram-i18n/core/package.json'));
  71. function getWxsCode() {
  72. const code = fs.readFileSync(path.join(CORE_PATH, '/wxs.js'), 'utf-8');
  73. // FIXME: function name maybe mangled
  74. const runner = `module.exports.t = Interpreter.getMessageInterpreter(translations, fallbackLocale)`;
  75. return [code, runner].join('\n');
  76. }
  77. let firstFile;
  78. const localeFile = {};
  79. function parseTranslations(object) {
  80. const keys = Object.keys(object);
  81. for (const key of keys) {
  82. const val = object[key];
  83. if (typeof val === 'string') {
  84. object[key] = parseTranslation(val);
  85. }
  86. // TODO: this is currently not supported
  87. if (typeof val === 'object') {
  88. object[key] = parseTranslations(val);
  89. }
  90. }
  91. return object;
  92. }
  93. const gulpI18nLocalesLoader = (options) => {
  94. if (!options)
  95. options = {};
  96. function buffer(file, _, cb) {
  97. // Ignore empty file or not json files
  98. if (file.isNull() || path.extname(file.path) !== '.json') {
  99. return cb(null, file);
  100. }
  101. if (file.isStream()) {
  102. return cb(new PluginError(PLUGIN_NAME, 'Streaming data is not supported'));
  103. }
  104. if (!file.contents) {
  105. return cb(null, file);
  106. }
  107. if (!firstFile)
  108. firstFile = file;
  109. const basename = path.basename(file.path);
  110. const localeName = basename.split('.')[0];
  111. let content = null;
  112. try {
  113. content = JSON.parse(file.contents.toString('utf-8'));
  114. if (!localeFile[localeName])
  115. localeFile[localeName] = {};
  116. localeFile[localeName] = Object.assign({}, localeFile[localeName], parseTranslations(content));
  117. }
  118. catch (err) {
  119. return cb(new PluginError(PLUGIN_NAME, err));
  120. }
  121. cb();
  122. }
  123. function endStream(cb) {
  124. if (!firstFile)
  125. return cb();
  126. const wxsFileName = options && options.wxsFileName || DEFAULT_WXS_FILENAME;
  127. const jsFileName = options && options.jsFileName || DEFAULT_JS_FILENAME;
  128. const localeString = JSON.stringify(localeFile);
  129. const defaultLocale = options && options.defaultLocale || DEFAULT_LOCALE;
  130. const fallbackLocale = options && options.fallbackLocale || DEFAULT_FALLBACK_LOCALE;
  131. const wxsContent = `var fallbackLocale='${fallbackLocale}';var translations=${localeString};\n${getWxsCode()}`;
  132. const jsContent = `module.exports.fallbackLocale='${fallbackLocale}';module.exports.defaultLocale='${defaultLocale}';module.exports.translations=${localeString};`;
  133. const wxsFile = new File({
  134. cwd: firstFile.cwd,
  135. base: firstFile.base,
  136. path: path.join(firstFile.base, wxsFileName),
  137. contents: Buffer.from(wxsContent),
  138. });
  139. const jsFile = new File({
  140. cwd: firstFile.cwd,
  141. base: firstFile.base,
  142. path: path.join(firstFile.base, jsFileName),
  143. contents: Buffer.from(jsContent),
  144. });
  145. this.push(wxsFile);
  146. this.push(jsFile);
  147. cb();
  148. }
  149. return through.obj(buffer, endStream);
  150. };
  151. module.exports = gulpI18nLocalesLoader;