sync_translations.js 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. const fs = require('fs');
  2. const path = require('path');
  3. const translationsPath = path.join('d:', 'workspace', 'AuraK', 'web', 'utils', 'translations.ts');
  4. const usedKeysPath = path.join('d:', 'workspace', 'AuraK', 'all_used_keys.txt');
  5. const usedKeys = fs.readFileSync(usedKeysPath, 'utf8').split('\n').filter(Boolean);
  6. const translationsContent = fs.readFileSync(translationsPath, 'utf8');
  7. const lines = translationsContent.split('\n');
  8. let currentLang = null;
  9. let resultLines = [];
  10. let keysSeen = new Set();
  11. const langStartRegex = /^\s+(\w+): \{/;
  12. const keyRegex = /^(\s+)([a-zA-Z0-9_-]+):(.*)/;
  13. function isValidIdentifier(id) {
  14. return /^[a-zA-Z_$][a-zA-Z0-9_$]*$/.test(id);
  15. }
  16. for (let i = 0; i < lines.length; i++) {
  17. const line = lines[i];
  18. const langMatch = line.match(langStartRegex);
  19. if (langMatch) {
  20. if (currentLang) {
  21. addMissingUsedKeys(currentLang, resultLines, keysSeen);
  22. }
  23. currentLang = langMatch[1];
  24. keysSeen = new Set();
  25. resultLines.push(line);
  26. continue;
  27. }
  28. if (currentLang) {
  29. const keyMatch = line.match(keyRegex);
  30. if (keyMatch) {
  31. const indent = keyMatch[1];
  32. let key = keyMatch[2];
  33. const rest = keyMatch[3];
  34. // Note: keyMatch[2] might already be quoted if it was fixed in a previous run
  35. const actualKey = (key.startsWith('"') && key.endsWith('"')) || (key.startsWith("'") && key.endsWith("'"))
  36. ? key.slice(1, -1)
  37. : key;
  38. keysSeen.add(actualKey);
  39. if (!isValidIdentifier(actualKey)) {
  40. resultLines.push(`${indent}"${actualKey}":${rest}`);
  41. continue;
  42. }
  43. }
  44. if (line.trim() === '},' || (line.trim() === '}' && i > lines.length - 5)) {
  45. addMissingUsedKeys(currentLang, resultLines, keysSeen);
  46. currentLang = null;
  47. resultLines.push(line);
  48. continue;
  49. }
  50. }
  51. resultLines.push(line);
  52. }
  53. function addMissingUsedKeys(lang, targetLines, seen) {
  54. for (const key of usedKeys) {
  55. if (!seen.has(key)) {
  56. const val = key;
  57. const quotedKey = isValidIdentifier(key) ? key : `"${key}"`;
  58. targetLines.push(` ${quotedKey}: ${JSON.stringify(val)},`);
  59. seen.add(key);
  60. }
  61. }
  62. }
  63. fs.writeFileSync(translationsPath, resultLines.join('\n'), 'utf8');
  64. console.log('Final sync (with proper identifier quoting) complete!');