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.

buildI18n.js 1.3 KiB

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