i18n.js 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. const fs = require('fs');
  2. const path = require('path');
  3. const util = require('util');
  4. const Vinyl = require('vinyl');
  5. const PluginError = require('plugin-error');
  6. const through = require('through2');
  7. const yaml = require('js-yaml');
  8. const readFile = util.promisify(fs.readFile);
  9. const readdir = util.promisify(fs.readdir);
  10. const transformers = {
  11. '.yml': data => yaml.safeLoad(data),
  12. '.json': data => JSON.parse(data),
  13. };
  14. class Locale {
  15. constructor(lang, base) {
  16. this.defaultLocale = 'messages.yml';
  17. this.lang = lang;
  18. this.base = base;
  19. this.data = {};
  20. }
  21. load() {
  22. const localeDir = `${this.base}/${this.lang}`;
  23. const data = {};
  24. return readdir(localeDir)
  25. .then(files => [this.defaultLocale].concat(files.filter(file => file !== this.defaultLocale)))
  26. .then(files => files.reduce((promise, file) => promise.then(() => {
  27. const ext = path.extname(file);
  28. const transformer = transformers[ext];
  29. if (transformer) {
  30. return readFile(`${localeDir}/${file}`, 'utf8')
  31. .then(res => { Object.assign(data, transformer(res)); }, err => {});
  32. }
  33. }), Promise.resolve()))
  34. .then(() => Object.keys(data).reduce((desc, key) => {
  35. this.data[key] = data[key].message;
  36. desc[key] = desc[key] || data[key].description;
  37. return desc;
  38. }, {}));
  39. }
  40. get(key, def) {
  41. return this.data[key] || def;
  42. }
  43. dump(data, { extension }) {
  44. if (extension === '.json') {
  45. data = JSON.stringify(data, null, 2);
  46. } else if (extension === '.yml') {
  47. data = yaml.safeDump(data, { sortKeys: true });
  48. } else {
  49. throw 'Unknown extension name!';
  50. }
  51. return {
  52. path: `${this.lang}/messages${extension}`,
  53. data,
  54. };
  55. }
  56. }
  57. class Locales {
  58. constructor(base) {
  59. this.defaultLang = 'en';
  60. this.newLocaleItem = 'NEW_LOCALE_ITEM';
  61. this.base = base || '.';
  62. this.langs = [];
  63. this.data = {};
  64. this.desc = {};
  65. }
  66. load() {
  67. return readdir(this.base)
  68. .then(langs => {
  69. this.langs = langs;
  70. return Promise.all(langs.map(lang => {
  71. const locale = this.data[lang] = new Locale(lang, this.base);
  72. return locale.load();
  73. }));
  74. })
  75. .then(data => {
  76. const desc = data[this.langs.indexOf(this.defaultLang)];
  77. Object.keys(desc).forEach(key => {
  78. this.desc[key] = {
  79. touched: false,
  80. value: desc[key],
  81. };
  82. });
  83. });
  84. }
  85. getData(lang, options) {
  86. options = options || {};
  87. const data = {};
  88. const langData = this.data[lang];
  89. const defaultData = options.useDefaultLang && lang != this.defaultLang && this.data[this.defaultLang];
  90. Object.keys(this.desc).forEach(key => {
  91. if (options.touchedOnly && !this.desc[key].touched) return;
  92. data[key] = {
  93. description: this.desc[key].value || this.newLocaleItem,
  94. message: langData.get(key) || defaultData && defaultData.get(key) || '',
  95. };
  96. if (options.markUntouched && !this.desc[key].touched) data[key].touched = false;
  97. });
  98. return data;
  99. }
  100. dump(options) {
  101. return this.langs.map(lang => {
  102. const data = this.getData(lang, options);
  103. const locale = this.data[lang];
  104. const out = locale.dump(data, options);
  105. return new Vinyl({
  106. path: out.path,
  107. contents: new Buffer(out.data),
  108. });
  109. });
  110. }
  111. touch(key) {
  112. let item = this.desc[key];
  113. if (!item) item = this.desc[key] = {
  114. value: this.newLocaleItem,
  115. };
  116. item.touched = true;
  117. }
  118. }
  119. function extract(options) {
  120. const keys = new Set();
  121. const patterns = {
  122. default: ['\\b(?:i18n\\(\'|i18n-key=")(\\w+)[\'"]', 1],
  123. json: ['__MSG_(\\w+)__', 1],
  124. };
  125. const types = {
  126. '.js': 'default',
  127. '.json': 'json',
  128. '.html': 'default',
  129. '.vue': 'default',
  130. };
  131. const locales = new Locales(options.base);
  132. function extract(data, types) {
  133. if (!Array.isArray(types)) types = [types];
  134. data = String(data);
  135. types.forEach(function (type) {
  136. const patternData = patterns[type];
  137. const pattern = new RegExp(patternData[0], 'g');
  138. const groupId = patternData[1];
  139. let groups;
  140. while (groups = pattern.exec(data)) {
  141. keys.add(groups[groupId]);
  142. }
  143. });
  144. }
  145. function bufferContents(file, enc, cb) {
  146. if (file.isNull()) return cb();
  147. if (file.isStream()) return this.emit('error', new PluginError('VM-i18n', 'Stream is not supported.'));
  148. const extname = path.extname(file.path);
  149. const type = types[extname];
  150. type && extract(file.contents, type);
  151. cb();
  152. }
  153. function endStream(cb) {
  154. locales.load()
  155. .then(() => {
  156. keys.forEach(key => {
  157. locales.touch(key);
  158. });
  159. return locales.dump({
  160. touchedOnly: options.touchedOnly,
  161. useDefaultLang: options.useDefaultLang,
  162. markUntouched: options.markUntouched,
  163. extension: options.extension,
  164. });
  165. })
  166. .then(files => {
  167. files.forEach(file => {
  168. this.push(file);
  169. });
  170. cb();
  171. })
  172. .catch(cb);
  173. }
  174. return through.obj(bufferContents, endStream);
  175. }
  176. module.exports = {
  177. extract,
  178. };