Skip to content

Commit

Permalink
Merge pull request lvgl#52 from bubeck/feature/dump-sourceref
Browse files Browse the repository at this point in the history
Add option --dump-sourceref to generate a file containing sourceKeys
  • Loading branch information
kisvegabor authored Oct 16, 2024
2 parents c36ae9f + 46f6f5b commit faa96d2
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 1 deletion.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,9 @@ Example:
one: One user is logged in
other: '%d users are logged in'
```

If translators want to know where a message comes from, then use `lv_i18n extract --dump-sourceref sr.json ...` to generate the file `sr.json` containing file names and line number of each message.

## Run compile to convert the yml files to a C and H file

Once you have the translations in the `yml` files you only need to run the `compile` to generate a C and H files from the `yml` files. No other library will be required to get the translation with `_()` and `_p`.
Expand Down
12 changes: 12 additions & 0 deletions lib/cmd_extract.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,14 @@ module.exports.subparserArgsList = [
action: 'store_true',
default: false
}
},
{
args: [ '--dump-sourceref' ],
options: {
dest: 'dump_sourceref',
help: 'dump the location of the extracted message keys to the given file',
metavar: '<filename>'
}
}
];

Expand Down Expand Up @@ -143,4 +151,8 @@ ${phraseObj.fileName}
});

translationKeys.saveFiles();

if (args.dump_sourceref) {
sourceKeys.dumpSourceRef(args.dump_sourceref);
}
};
16 changes: 15 additions & 1 deletion lib/source_keys.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ const debug = require('debug')('sourcee_keys');
const AppError = require('./app_error');
const parse = require('./parser');

const { readFileSync } = require('fs');
const { readFileSync, writeFileSync } = require('fs');


module.exports = class SourceKeys {
Expand Down Expand Up @@ -61,4 +61,18 @@ ${this.uniques[key].fileName}, line ${this.uniques[key].line}
glob(p, { nodir: true }).forEach(name => this.loadFile(name));
});
}

dumpSourceRef(filename) {
let result = [];

Object.values(this.uniques).forEach(k1 => {
Object.values(this.keys).forEach(k2 => {
if (k1.key === k2.key) {
result.push(k2);
}
});
});

writeFileSync(filename, JSON.stringify(result, null, 2));
}
};
4 changes: 4 additions & 0 deletions test/js/test_cli_extract.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,10 @@ describe('CLI extract', function () {
run([ 'extract', '-s', join(__dirname, 'fixtures/empty_src.c'), '-t', 'any-value' ]);
});

it('Should exit without error with sourceref', function () {
run([ 'extract', '-s', join(fixtures, 'src_*.c'), '--dump-sourceref', join(fixtures, 'sourceref'),
'-t', join(fixtures, 'empty_en-GB.yml') ]);
});

it('Should fill empty locales', function () {
run([ 'extract', '-s', join(fixtures, 'src_*.c'), '-t', join(fixtures, 'empty_*.yml') ]);
Expand Down

0 comments on commit faa96d2

Please sign in to comment.