Repository navigation
Expand file tree
/
Copy pathrepair_i18n.js
More file actions
131 lines (111 loc) · 4.79 KB
/
Copy pathrepair_i18n.js
File metadata and controls
131 lines (111 loc) · 4.79 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
const fs = require('fs');
const path = require('path');
// Bad keys to revert!
// These are keys that replaced valid code syntax.
const BAD_KEYS = [
'authSystem.promise',
'authContext.promise',
'backendContext.promise',
'feedContext.promise',
'groupContext.promise',
'liveStreamContext.promise',
'playlistContext.promise',
'postCard.promise',
'searchContext.promise',
'storyContext.promise',
'subscriptionContext.promise',
'unifiedNotificationContext.promise',
'demoAccounts.promise',
'emailVerificationStatus.promise',
// Likely others but let's start with 'promise' as it's the main breaker
// Other common types?
// 'boolean' -> check if there's a key for it.
// 'string' -> check
// 'void' -> check
];
// Map of Key -> Original Text to restore (approximation)
const RESTORE_MAP = {
'authSystem.promise': 'Promise',
'authContext.promise': 'Promise',
'backendContext.promise': 'Promise',
'feedContext.promise': 'Promise',
'groupContext.promise': 'Promise',
'liveStreamContext.promise': 'Promise',
'playlistContext.promise': 'Promise',
'postCard.promise': 'Promise',
'searchContext.promise': 'Promise',
'storyContext.promise': 'Promise',
'subscriptionContext.promise': 'Promise',
'unifiedNotificationContext.promise': 'Promise',
'demoAccounts.promise': 'Promise',
'emailVerificationStatus.promise': 'Promise'
};
const TRANSLATIONS_FILE = 'proposed_translations.json';
const data = JSON.parse(fs.readFileSync(TRANSLATIONS_FILE, 'utf8'));
// 1. Remove bad keys from LanguageContext.tsx
console.log('Restoring LanguageContext.tsx...');
const LANG_FILE = 'src/contexts/LanguageContext.tsx';
let langContent = fs.readFileSync(LANG_FILE, 'utf8');
BAD_KEYS.forEach(key => {
// Regex to remove line: 'key': '...',\n
const escapedKey = key.replace(/\./g, '\\.');
const regex = new RegExp(`^\\s*'${escapedKey}':.*,$\\n`, 'gm');
langContent = langContent.replace(regex, '');
});
fs.writeFileSync(LANG_FILE, langContent);
// 2. Revert files
console.log('Reverting bad replacements in files...');
const filesToProcess = [...new Set(data.replacements.map(r => r.file))];
filesToProcess.forEach(filePath => {
let content = fs.readFileSync(filePath, 'utf8');
let modified = false;
BAD_KEYS.forEach(key => {
// Look for {t('key')}
const search = `{t('${key}')}`;
if (content.includes(search)) {
// Restore to original text
const original = RESTORE_MAP[key];
content = content.replace(new RegExp(search.replace(/[.*+?^${}()|[\]\\]/g, '\\$&'), 'g'), original);
modified = true;
}
});
// FIX FOR AnalyticsDashboard.tsx typo
// Replaces: `const { t } = useLanguage();i}` -> `i}`
// Wait, the context was `key={...}`
// Original: `key={i}` inside map
// Current: `key={const { t } = useLanguage();i}`
// Correction: `const { t } = useLanguage();` should NOT be there.
// Revert pattern logic: remove `const { t } = useLanguage();` if it is inside `key={...}` or similar.
// Specially target the known failure:
if (content.includes('const { t } = useLanguage();i}')) {
content = content.replace('const { t } = useLanguage();i}', 'i}');
modified = true;
}
// FIX IMPORTS
// Replace `@/contexts/LanguageContext` with relative paths using path.relative
// ... OR just use `src/contexts/LanguageContext` if absolute imports are shaky?
// Let's try to calculate relative path.
if (content.includes("@/contexts/LanguageContext")) {
// Calculate relative path from this file's dir to src/contexts/LanguageContext
const fileDir = path.dirname(filePath); // Absolute dir of file
const contextDir = path.join(process.cwd(), 'src/contexts/LanguageContext');
// This is tricky on Windows vs Posix paths for simple replacements.
// Let's stick to the alias being BROKEN as the hypothesis.
// Let's replace `@/contexts/` with `../../contexts/` blindly for components?
// No, standard relative.
let rel = path.relative(fileDir, contextDir).replace(/\\/g, '/');
// If it starts with 'LanguageContext', needs './'
if (!rel.startsWith('.')) rel = './' + rel;
// Remove .tsx extension if present (path.relative might keep it if target had it?)
// contextDir usually shouldn't have ext for import... wait `LanguageContext` is the file.
// path.join(..., 'LanguageContext') -> d:/.../LanguageContext
// if fileDir is d:/.../components
// rel is ../contexts/LanguageContext
content = content.replace(/@\/contexts\/LanguageContext/g, rel);
modified = true;
}
if (modified) {
fs.writeFileSync(filePath, content);
}
});
console.log('Repair complete.');