check-translations.js 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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/gui/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/gui/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. // Ensure that translations only contain keys that exist in the en dictionary
  52. function checkTranslationKeys() {
  53. const enDictionary = translations.en.dictionary;
  54. for (const translation of Object.values(translations)) {
  55. // We compare against the en translation, so checking it doesn't make sense.
  56. if (translation.code === 'en') continue;
  57. // If the dictionary is missing, we already reported that in checkTranslationRegistrations().
  58. if (typeof translation.dictionary !== "object") continue;
  59. for (const [key, value] of Object.entries(translation.dictionary)) {
  60. if (!enDictionary[key]) {
  61. reportError(`Translation '${translation.code}' has key '${key}' that doesn't exist in 'en'!`);
  62. }
  63. }
  64. }
  65. }
  66. // Ensure that all keys passed to i18n() exist in the en dictionary
  67. async function checkTranslationUsage() {
  68. const enDictionary = translations.en.dictionary;
  69. const sourceDirectories = [
  70. './src/gui/src/helpers',
  71. './src/gui/src/UI',
  72. ];
  73. // Looks for i18n() calls using either ' or " for the key string.
  74. // The key itself is at index 2 of the result.
  75. const i18nRegex = /i18n\((['"])(.*?)\1\)/g;
  76. for (const dir of sourceDirectories) {
  77. const files = await fs.promises.readdir(dir, { recursive: true });
  78. for (const relativeFileName of files) {
  79. if (!relativeFileName.endsWith('.js')) continue;
  80. const fileName = `${dir}/${relativeFileName}`;
  81. const fileContents = await fs.promises.readFile(fileName, { encoding: 'utf8' });
  82. const i18nUses = fileContents.matchAll(i18nRegex);
  83. for (const use of i18nUses) {
  84. const key = use[2];
  85. if (!enDictionary.hasOwnProperty(key)) {
  86. reportError(`Unrecognized i18n key: call ${use[0]} in ${fileName}`);
  87. }
  88. }
  89. }
  90. }
  91. }
  92. await checkTranslationRegistrations();
  93. checkTranslationKeys();
  94. await checkTranslationUsage();
  95. if (hadError) {
  96. process.stdout.write('Errors were found in translation files.\n');
  97. process.exit(1);
  98. }
  99. process.stdout.write('✅ Translations appear valid.\n');
  100. process.exit(0);