// Quote any object key that starts with a digit (invalid as unquoted JS key). // Flat key string (ns.123abc) is unchanged, so t('ns.123abc') still resolves. const fs = require('fs'); function fix(file) { let s = fs.readFileSync(file, 'utf8'); let n = 0; s = s.replace(/^(\s*)([0-9][A-Za-z0-9_]*)(:\s)/gm, (m, ind, key, colon) => { n++; return ind + "'" + key + "'" + colon; }); fs.writeFileSync(file, s, 'utf8'); console.log(file + ' -> quoted ' + n + ' digit-leading keys'); } fix('src/locales/zh/index.ts'); fix('src/locales/en/index.ts'); // verify both parse function load(p) { let s = fs.readFileSync(p, 'utf8'); s = s.replace(/const\s+\w+\s*=\s*\{/, 'var obj = {'); s = s.replace(/export\s+default\s+\w+;?/, ''); s = s.replace(/\bconst\b/g, 'var'); return new Function(s + '\nreturn obj;')(); } try { const z = load('src/locales/zh/index.ts'); console.log('zh parses OK, top keys:', Object.keys(z).length); } catch (e) { console.log('zh PARSE FAIL:', e.message.split('\n')[0]); } try { const e2 = load('src/locales/en/index.ts'); console.log('en parses OK, top keys:', Object.keys(e2).length); } catch (e) { console.log('en PARSE FAIL:', e.message.split('\n')[0]); }