-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* (bluefox) Added search for i18n keys in the code
- Loading branch information
1 parent
8a92bef
commit 0a8f4f7
Showing
2 changed files
with
71 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
let name; | ||
try { | ||
name = require('../../../../package.json').name.replace(/-/g, '_').replace(/^iobroker\./g, ''); | ||
} catch (e) { | ||
if (process.argv[2]) { | ||
name = process.argv[2]; | ||
} | ||
} | ||
|
||
const dir = require('node-dir'); | ||
// Extend Acorn parser with JSX | ||
const acorn = require('acorn'); | ||
const jsx = require('acorn-jsx'); | ||
const parser = acorn.Parser.extend(jsx()); | ||
|
||
// Extend Acorn walk with JSX | ||
const walk = require('acorn-walk'); | ||
const { extend } = require('acorn-jsx-walk'); | ||
extend(walk.base); | ||
|
||
const keys = []; | ||
|
||
dir.readFiles(process.argv[3] || __dirname + '/../../../src', | ||
{ | ||
match: /\.jsx?$/, | ||
}, | ||
(err, content, next) => { | ||
const result = parser.parse(content, { | ||
sourceType: 'module', | ||
ecmaVersion: 'latest' | ||
}); | ||
|
||
walk.full(result, node => { | ||
if (node.type === 'CallExpression' && node.callee.property?.type === 'Identifier' && node.callee.property?.name === 't') { | ||
if (node.arguments.length === 1 && node.arguments[0].type === 'Literal' && typeof node.arguments[0].value === 'string') { | ||
// This is for the case `t` is called with a single string | ||
// literal as argument. | ||
if (!keys.includes(node.arguments[0].value)) { | ||
keys.push(node.arguments[0].value); | ||
} | ||
} else { | ||
// In case you have things like template literals as well, | ||
// or multiple arguments, you'd need to handle them here too. | ||
console.log(`Cannot calculate: "${content.slice(node.arguments[0].start, node.arguments[0].end)}"`); | ||
} | ||
} | ||
if (node.type === 'Property' && node.key.name === 'visAttrs') { | ||
const visAttrs = parser.parse(content.slice(node.value.start, node.value.end), { | ||
sourceType: 'module', | ||
ecmaVersion: 'latest' | ||
}); | ||
walk.full(visAttrs, attrNode => { | ||
if (attrNode.type === 'Property' && attrNode.key.name === 'label') { | ||
if (!keys.includes(attrNode.value.value)) { | ||
keys.push(attrNode.value.value); | ||
} | ||
} | ||
}); | ||
} | ||
}); | ||
next(); | ||
}, | ||
() => { | ||
const result = {} | ||
keys.forEach(key => result[key] = key.replace(name + '_', '')); | ||
console.log(JSON.stringify(result, null, 2)); | ||
}, | ||
); |