You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

41 rivejä
1.2 KiB

  1. const walk = require('walk');
  2. const fs = require('fs');
  3. const path = require('path');
  4. const parser = require('vue-polyglot-utils');
  5. const SOURCES_DIR = '../src';
  6. const walker = walk.walk(SOURCES_DIR, {followLinks: false, filters: ["i18n"]});
  7. const locale = {};
  8. walker.on("file", (root, fileStats, next) => {
  9. const file = path.join(root, fileStats.name);
  10. fs.readFile(file, (err, data) => {
  11. const pieceOfLocale = parser.extractLocales(data);
  12. if (Object.keys(pieceOfLocale).length > 0) {
  13. Object.assign(locale, pieceOfLocale);
  14. }
  15. next();
  16. });
  17. });
  18. const I18N_DIR = '../src/i18n';
  19. const LANGUAGES_AVAILABLE = ['zh', 'zh-CN', 'fr', 'es', 'de', 'en'];
  20. walker.on("end", () => {
  21. LANGUAGES_AVAILABLE.forEach(lang => {
  22. const localeFile = path.join(I18N_DIR, `${lang}.json`);
  23. let existingLocale = {};
  24. if (fs.existsSync(localeFile)) {
  25. existingLocale = require(localeFile);
  26. }
  27. const localeOrdered = {};
  28. Object.keys(locale).sort().forEach(function(key) {
  29. if (key in existingLocale) {
  30. localeOrdered[key] = existingLocale[key];
  31. } else {
  32. localeOrdered[key] = locale[key];
  33. }
  34. });
  35. fs.writeFileSync(localeFile, JSON.stringify(localeOrdered, null, 2));
  36. });
  37. });