diff --git a/README.md b/README.md index 8bb2aec..64d38ce 100644 --- a/README.md +++ b/README.md @@ -11,6 +11,9 @@ See here: https://github.com/ioBroker/ioBroker.vis-widgets-react-template --> ## Changelog +### **WORK IN PROGRESS** +* (bluefox) Added search for i18n keys in the code + ### 0.1.7 (2022-07-22) * (bluefox) Added `modulefederation` and `craco` config files diff --git a/searchI18n.js b/searchI18n.js new file mode 100644 index 0000000..d5160e4 --- /dev/null +++ b/searchI18n.js @@ -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)); + }, +);