Allow targeting .properties files directly

This commit is contained in:
Eemeli Aro 2021-11-02 19:39:48 +02:00
Родитель 102cb38be0
Коммит a03ec9d81f
3 изменённых файлов: 52 добавлений и 5 удалений

Просмотреть файл

@ -75,6 +75,10 @@ XHTML may include `<stringbundle>` 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?

12
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 <jsPath>',
'Convert JS files to use Fluent Localization rather than string bundles',
'$0 <path>',
'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(

Просмотреть файл

@ -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)
}