check-translations.js 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. /**
  2. * Copyright (C) 2024 Puter Technologies Inc.
  3. *
  4. * This file is part of Puter.
  5. *
  6. * Puter is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU Affero General Public License as published
  8. * by the Free Software Foundation, either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU Affero General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Affero General Public License
  17. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  18. */
  19. import translations from '../src/i18n/translations/translations.js';
  20. import fs from 'fs';
  21. let hadError = false;
  22. function reportError(message) {
  23. hadError = true;
  24. process.stderr.write(`❌ ${message}\n`);
  25. }
  26. // Check that each translation file is recorded in `translations`
  27. async function checkTranslationRegistrations() {
  28. const files = await fs.promises.readdir('./src/i18n/translations');
  29. for (const fileName of files) {
  30. if (!fileName.endsWith('.js')) continue;
  31. const translationName = fileName.substring(0, fileName.length - 3);
  32. if (translationName === 'translations') continue;
  33. const translation = translations[translationName];
  34. if (!translation) {
  35. reportError(`Translation '${translationName}' is not listed in translations.js, please add it!`);
  36. continue;
  37. }
  38. if (!translation.name) {
  39. reportError(`Translation '${translationName}' is missing a name!`);
  40. }
  41. if (!translation.code) {
  42. reportError(`Translation '${translationName}' is missing a code!`);
  43. } else if (translation.code !== translationName) {
  44. reportError(`Translation '${translationName}' has code '${translation.code}', which should be '${translationName}'!`);
  45. }
  46. if (typeof translation.dictionary !== 'object') {
  47. reportError(`Translation '${translationName}' is missing a translations dictionary! Should be an object.`);
  48. }
  49. }
  50. }
  51. function checkTranslationKeys() {
  52. const enDictionary = translations.en.dictionary;
  53. for (const translation of Object.values(translations)) {
  54. // We compare against the en translation, so checking it doesn't make sense.
  55. if (translation.code === 'en') continue;
  56. // If the dictionary is missing, we already reported that in checkTranslationRegistrations().
  57. if (typeof translation.dictionary !== "object") continue;
  58. for (const [key, value] of Object.entries(translation.dictionary)) {
  59. if (!enDictionary[key]) {
  60. reportError(`Translation '${translation.code}' has key '${key}' that doesn't exist in 'en'!`);
  61. }
  62. }
  63. }
  64. }
  65. await checkTranslationRegistrations();
  66. checkTranslationKeys();
  67. if (hadError) {
  68. process.stdout.write('Errors were found in translation files.\n');
  69. process.exit(1);
  70. }
  71. process.stdout.write('✅ Translations appear valid.\n');
  72. process.exit(0);