fix_i18n_files.js 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. /**
  2. * Changes the language in the translation files retrieved from Transifex.
  3. */
  4. const fs = require('fs');
  5. const I18N_DIRS = [ "./src/config/locales/ui", "./src/config/locales/presets" ];
  6. const JSON_RGX = /^[A-Za-z0-9_\-]+\.json$/;
  7. //Read translation files
  8. I18N_DIRS.forEach(i18n_dir => {
  9. fs.readdirSync(i18n_dir).forEach((file) => {
  10. if(JSON_RGX.test(file)) {
  11. const lng = file.substring(0, file.length - 5).replace("_", "-");
  12. try {
  13. //Read file
  14. const lngData = JSON.parse(fs.readFileSync(i18n_dir+"/"+file, 'utf8'));
  15. //If not already fixed
  16. if(lngData[lng] === undefined) {
  17. //Check if en available
  18. if(lngData["en"] !== undefined) {
  19. //Edit object, put en into lng locale
  20. const outData = {};
  21. outData[lng] = lngData.en;
  22. //Overwrite file
  23. fs.writeFile(i18n_dir+"/"+file, JSON.stringify(outData, null, 2), function(err) {
  24. if(err) {
  25. throw new Error(err);
  26. }
  27. else {
  28. console.log("[INFO] Translation file "+i18n_dir+"/"+file+" updated");
  29. }
  30. });
  31. }
  32. else {
  33. throw new Error("Unknown translation locale: "+i18n_dir+"/"+file);
  34. }
  35. }
  36. else {
  37. console.log("[INFO] Translation file "+i18n_dir+"/"+file+" already OK");
  38. }
  39. }
  40. catch(e) {
  41. if(e instanceof SyntaxError) {
  42. throw new Error("Invalid translation file: "+i18n_dir+"/"+file+" ("+e.message+")");
  43. }
  44. else {
  45. throw e;
  46. }
  47. }
  48. }
  49. else {
  50. console.log("[INFO] Ignored file "+i18n_dir+"/"+file);
  51. }
  52. });
  53. });