From a03ec9d81fa8a9a4ffe542b74349ecacef815b78 Mon Sep 17 00:00:00 2001 From: Eemeli Aro Date: Tue, 2 Nov 2021 19:39:48 +0200 Subject: [PATCH] Allow targeting .properties files directly --- README.md | 6 +++++- cli.js | 12 ++++++++---- lib/transform-properties.js | 39 +++++++++++++++++++++++++++++++++++++ 3 files changed, 52 insertions(+), 5 deletions(-) create mode 100644 lib/transform-properties.js diff --git a/README.md b/README.md index 7415f4c..1fbdc26 100644 --- a/README.md +++ b/README.md @@ -75,6 +75,10 @@ XHTML may include `` elements which are detected (and their source and properties files may include `FTL path` references, which are also parsed. All of those files are then modified in-place. +When targeting a `.properties` file, all of its strings are migrated to Fluent. +In this use, JS and XHTML files are not parsed or migrated, +and the placeholder variables are forced to use `var#` names. + ## Your Attention is Required Because so many things change, it's unlikely that the script will catch everything. @@ -86,6 +90,6 @@ Where possible, a comment `/* L10N-FIXME */` is injected immediately after point - [x] Better variable renaming - [x] Remove `.properties` files when empty - [ ] Update `jar.mn` -- [ ] Allow targeting `.properties` files directly +- [x] Allow targeting `.properties` files directly - [x] Tools for mapping `chrome://` references across the repo - [ ] Some way of FTL path autodiscovery? diff --git a/cli.js b/cli.js index 0373be7..9005bfd 100755 --- a/cli.js +++ b/cli.js @@ -1,8 +1,10 @@ #!/usr/bin/env node +import { extname } from 'path' import yargs from 'yargs' import { forEachPropertiesFile, getInfo } from './lib/get-info.js' import { transformJs } from './lib/transform-js.js' +import { transformProperties } from './lib/transform-properties.js' yargs(process.argv.slice(2)) .options({ @@ -38,11 +40,13 @@ yargs(process.argv.slice(2)) }) .command( - '$0 ', - 'Convert JS files to use Fluent Localization rather than string bundles', + '$0 ', + 'Convert files to use Fluent Localization rather than string bundles', {}, - ({ bug, jsPath, dryRun, format, root, title }) => - transformJs(jsPath, { bug, dryRun, format, root, title }) + ({ path, ...options }) => + extname(path) === '.properties' + ? transformProperties(path, options) + : transformJs(path, options) ) .command( diff --git a/lib/transform-properties.js b/lib/transform-properties.js new file mode 100644 index 0000000..588ae69 --- /dev/null +++ b/lib/transform-properties.js @@ -0,0 +1,39 @@ +import { migrateMessage } from './migrate-message.js' +import { parseMessageFiles } from './parse-message-files.js' +import { updateLocalizationFiles } from './update-localization-files.js' +import { findRoot } from './util-find-root.js' + +export async function transformProperties(path, options = {}) { + if (options.dryRun) + console.warn('--- DRY RUN: Not writing changes to disk.\n') + if (!options.bug) options.bug = 'xxxxxx' + if (!options.root) options.root = await findRoot() + if (!options.root) { + console.error('Error: Project root not found!') + process.exit(1) + } + console.warn(`Using root: ${options.root}`) + + const propData = await parseMessageFiles(path) + + if (!propData.ftl) { + console.error(` +Error: No migrations defined! + +In order to migrate strings to Fluent, the .properties file must include +FTL metadata comments: + +# FTL path: foo/bar/baz.ftl +# FTL prefix: foobar + +For more information, see: https://github.com/eemeli/properties-to-ftl#readme +`) + process.exit(1) + } + + for (const entry of propData.ast) + if (entry.type === 'PAIR') migrateMessage(propData, entry.key, []) + + console.warn('') + await updateLocalizationFiles(propData, options) +}