arb-convert
Version:
Convert Application Resource Bundle (ARB) translation files to other translation formats and back
57 lines (56 loc) • 2.36 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
var commander_1 = __importDefault(require("commander"));
var fs_1 = __importDefault(require("fs"));
var index_1 = require("../index");
var parseLocale_1 = __importDefault(require("../util/parseLocale"));
var program = new commander_1.default.Command();
program
.name('arb2po')
.option('--sourcefile <filename>', 'source ARB file (required)')
.option('--targetfile <filename>', 'target ARB file')
.option('--original <value>', 'where the translations come from')
.option('--sourcelang <locale>', 'source locale override, e.g. en-US, in case it cannot be determined from file content or file name')
.option('--targetlang <locale>', 'target locale override, e.g. de-DE, in case it cannot be determined from file content or file name')
.option('--out <filename>', 'write PO to file if given or stdout if omitted')
.parse(process.argv);
// No params
if (process.argv.length <= 2) {
program.help(); // shows help and exits
}
try {
var options = program.opts();
if (!options.sourcefile) {
throw new Error("option '--sourcefile <filename>' is required");
}
var sourceContent = fs_1.default.readFileSync(options.sourcefile, 'utf8');
var targetContent = options.targetfile && fs_1.default.readFileSync(options.targetfile, 'utf8');
var result = index_1.convertFromArb('gettext', {
source: sourceContent,
target: targetContent,
original: options.original,
sourceLanguage: parseLocale_1.default(options.sourcelang, determineArbLocale(sourceContent), options.sourcefile),
targetLanguage: options.targetfile && parseLocale_1.default(options.targetlang, determineArbLocale(targetContent), options.targetfile),
});
if (options.out) {
fs_1.default.writeFileSync(options.out, result.content);
}
else {
process.stdout.write(result.content);
}
}
catch (error) {
process.stdout.write("error: " + error.message);
process.exit(1);
}
function determineArbLocale(content) {
var matches = content.match(/"@@locale":\s*"(.+)"/);
if (matches) {
return matches[1];
}
return '';
}