i18n.js 5.1 KB

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