check-translations.js 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. // METADATA // {"ai-commented":{"service":"claude"}}
  2. /*
  3. * Copyright (C) 2024-present Puter Technologies Inc.
  4. *
  5. * This file is part of Puter.
  6. *
  7. * Puter is free software: you can redistribute it and/or modify
  8. * it under the terms of the GNU Affero General Public License as published
  9. * by the Free Software Foundation, either version 3 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU Affero General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Affero General Public License
  18. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  19. */
  20. import translations from '../src/gui/src/i18n/translations/translations.js';
  21. import fs from 'fs';
  22. let hadError = false;
  23. function reportError(message) {
  24. hadError = true;
  25. process.stderr.write(`❌ ${message}\n`);
  26. }
  27. /**
  28. * Verifies that all translation files in the translations directory are properly registered
  29. * in the translations object. Checks for required properties like name, code, and dictionary.
  30. * Reports errors if translations are missing, improperly configured, or have mismatched codes.
  31. * @async
  32. * @returns {Promise<void>}
  33. */
  34. async function checkTranslationRegistrations() {
  35. const files = await fs.promises.readdir('./src/gui/src/i18n/translations');
  36. for (const fileName of files) {
  37. if (!fileName.endsWith('.js')) continue;
  38. const translationName = fileName.substring(0, fileName.length - 3);
  39. if (translationName === 'translations') continue;
  40. const translation = translations[translationName];
  41. if (!translation) {
  42. reportError(`Translation '${translationName}' is not listed in translations.js, please add it!`);
  43. continue;
  44. }
  45. if (!translation.name) {
  46. reportError(`Translation '${translationName}' is missing a name!`);
  47. }
  48. if (!translation.code) {
  49. reportError(`Translation '${translationName}' is missing a code!`);
  50. } else if (translation.code !== translationName) {
  51. reportError(`Translation '${translationName}' has code '${translation.code}', which should be '${translationName}'!`);
  52. }
  53. if (typeof translation.dictionary !== 'object') {
  54. reportError(`Translation '${translationName}' is missing a translations dictionary! Should be an object.`);
  55. }
  56. }
  57. }
  58. /**
  59. * Validates that translation dictionaries only contain keys present in en.js
  60. *
  61. * Iterates through all translations (except English) and checks that each key in their
  62. * dictionary exists in the en.js dictionary. Reports errors for any keys that don't exist.
  63. * Skips validation if the translation dictionary is missing or invalid.
  64. */
  65. function checkTranslationKeys() {
  66. const enDictionary = translations.en.dictionary;
  67. for (const translation of Object.values(translations)) {
  68. // We compare against the en translation, so checking it doesn't make sense.
  69. if (translation.code === 'en') continue;
  70. // If the dictionary is missing, we already reported that in checkTranslationRegistrations().
  71. if (typeof translation.dictionary !== "object") continue;
  72. for (const [key, value] of Object.entries(translation.dictionary)) {
  73. if (!enDictionary[key]) {
  74. reportError(`Translation '${translation.code}' has key '${key}' that doesn't exist in 'en'!`);
  75. }
  76. }
  77. }
  78. }
  79. /**
  80. * Checks for usage of i18n() calls in source files and verifies that all translation keys exist in en.js
  81. *
  82. * Scans JavaScript files in specified source directories for i18n() function calls using regex.
  83. * Validates that each key used in these calls exists in the English translation dictionary.
  84. *
  85. * @async
  86. * @returns {Promise<void>}
  87. */
  88. async function checkTranslationUsage() {
  89. const enDictionary = translations.en.dictionary;
  90. const sourceDirectories = [
  91. './src/gui/src/helpers',
  92. './src/gui/src/UI',
  93. ];
  94. // Looks for i18n() calls using either ' or " for the key string.
  95. // The key itself is at index 2 of the result.
  96. const i18nRegex = /i18n\((['"])(.*?)\1\)/g;
  97. for (const dir of sourceDirectories) {
  98. const files = await fs.promises.readdir(dir, { recursive: true });
  99. for (const relativeFileName of files) {
  100. if (!relativeFileName.endsWith('.js')) continue;
  101. const fileName = `${dir}/${relativeFileName}`;
  102. const fileContents = await fs.promises.readFile(fileName, { encoding: 'utf8' });
  103. const i18nUses = fileContents.matchAll(i18nRegex);
  104. for (const use of i18nUses) {
  105. const key = use[2];
  106. if (!enDictionary.hasOwnProperty(key)) {
  107. reportError(`Unrecognized i18n key: call ${use[0]} in ${fileName}`);
  108. }
  109. }
  110. }
  111. }
  112. }
  113. await checkTranslationRegistrations();
  114. checkTranslationKeys();
  115. await checkTranslationUsage();
  116. if (hadError) {
  117. process.stdout.write('Errors were found in translation files.\n');
  118. process.exit(1);
  119. }
  120. process.stdout.write('✅ Translations appear valid.\n');
  121. process.exit(0);