Skip to content

Commit

Permalink
* (bluefox) Added search for i18n keys in the code
Browse files Browse the repository at this point in the history
  • Loading branch information
GermanBluefox committed Jul 23, 2022
1 parent 8a92bef commit 0a8f4f7
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 0 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
68 changes: 68 additions & 0 deletions searchI18n.js
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));
},
);

0 comments on commit 0a8f4f7

Please sign in to comment.