diff --git a/README.md b/README.md index f13cb1e..b2d0f90 100644 --- a/README.md +++ b/README.md @@ -30,7 +30,7 @@ will extract translations to .pot file --numberedExpressions boolean overrides babel-plugin-ttag setting - https://ttag.js.org/docs/plugin-api.html#confignumberedexpressions. Refer to the doc for the details. --extractLocation string - 'full' | 'file' | 'never' - https://ttag.js.org/docs/plugin-api.html#configextractlocation. Is used to format location comments in the .po file. --sortByMsgid boolean. Will sort output in alphabetically by msgid. https://ttag.js.org/docs/plugin-api.html#configsortbymsgid - + --addComments boolean | string. Will extract leading comments before a translatable string. https://ttag.js.org/docs/plugin-api.html#configaddcomments ### `check [lang] ` will check if all translations are present in .po file @@ -85,6 +85,7 @@ will update existing po file. Add/remove new translations --numberedExpressions boolean overrides babel-plugin-ttag setting - https://ttag.js.org/docs/plugin-api.html#confignumberedexpressions. Refer to the doc for the details. --extractLocation string - 'full' | 'file' | 'never' - https://ttag.js.org/docs/plugin-api.html#configextractlocation. Is used to format location comments in the .po file. --sortByMsgid boolean. Will sort output in alphabetically by msgid. https://ttag.js.org/docs/plugin-api.html#configsortbymsgid + --addComments boolean | string. Will extract leading comments before a translatable string. https://ttag.js.org/docs/plugin-api.html#configaddcomments --foldLength number. Output .po file line width. ### `replace [options] ` diff --git a/package-lock.json b/package-lock.json index 660593f..df4e45e 100644 --- a/package-lock.json +++ b/package-lock.json @@ -4049,9 +4049,19 @@ } }, "node_modules/caniuse-lite": { - "version": "1.0.30001157", - "resolved": "https://registry.npmjs.org/caniuse-lite/-/caniuse-lite-1.0.30001157.tgz", - "integrity": "sha512-gOerH9Wz2IRZ2ZPdMfBvyOi3cjaz4O4dgNwPGzx8EhqAs4+2IL/O+fJsbt+znSigujoZG8bVcIAUM/I/E5K3MA==" + "version": "1.0.30001447", + "resolved": "https://registry.npmjs.org/caniuse-lite/-/caniuse-lite-1.0.30001447.tgz", + "integrity": "sha512-bdKU1BQDPeEXe9A39xJnGtY0uRq/z5osrnXUw0TcK+EYno45Y+U7QU9HhHEyzvMDffpYadFXi3idnSNkcwLkTw==", + "funding": [ + { + "type": "opencollective", + "url": "https://opencollective.com/browserslist" + }, + { + "type": "tidelift", + "url": "https://tidelift.com/funding/github/npm/caniuse-lite" + } + ] }, "node_modules/capture-exit": { "version": "2.0.0", @@ -15479,9 +15489,9 @@ "integrity": "sha512-L28STB170nwWS63UjtlEOE3dldQApaJXZkOI1uMFfzf3rRuPegHaHesyee+YxQ+W6SvRDQV6UrdOdRiR153wJg==" }, "caniuse-lite": { - "version": "1.0.30001157", - "resolved": "https://registry.npmjs.org/caniuse-lite/-/caniuse-lite-1.0.30001157.tgz", - "integrity": "sha512-gOerH9Wz2IRZ2ZPdMfBvyOi3cjaz4O4dgNwPGzx8EhqAs4+2IL/O+fJsbt+znSigujoZG8bVcIAUM/I/E5K3MA==" + "version": "1.0.30001447", + "resolved": "https://registry.npmjs.org/caniuse-lite/-/caniuse-lite-1.0.30001447.tgz", + "integrity": "sha512-bdKU1BQDPeEXe9A39xJnGtY0uRq/z5osrnXUw0TcK+EYno45Y+U7QU9HhHEyzvMDffpYadFXi3idnSNkcwLkTw==" }, "capture-exit": { "version": "2.0.0", diff --git a/src/lib/extract.ts b/src/lib/extract.ts index 3c72676..a0652e0 100644 --- a/src/lib/extract.ts +++ b/src/lib/extract.ts @@ -23,7 +23,7 @@ export async function extractAll( const tmpFile = tmp.fileSync(); let ttagOpts: ttagTypes.TtagOpts = { extract: { output: tmpFile.name }, - sortByMsgid: overrideOpts && overrideOpts.sortByMsgid, + sortByMsgid: false, addComments: true }; if (lang !== "en") { diff --git a/src/lib/ttagPluginOverride.ts b/src/lib/ttagPluginOverride.ts index ce1acef..6fe13a2 100644 --- a/src/lib/ttagPluginOverride.ts +++ b/src/lib/ttagPluginOverride.ts @@ -24,11 +24,16 @@ const sortByMsgidDescr = `boolean - The resulting output will be sorted alphabet "#configsortbymsgid" )}`; +const addCommentsDescr = `boolean | string - Extract leading comments before a translatable string. ${doc( + "#configaddcomments" +)}`; + const OPTS: { [k: string]: { description: string; boolean?: boolean } } = { discover: { description: discoverDescription }, numberedExpressions: { description: numberedExpressionsDescr }, extractLocation: { description: extractLocationDescr }, - sortByMsgid: { description: sortByMsgidDescr, boolean: true } + sortByMsgid: { description: sortByMsgidDescr, boolean: true }, + addComments: { description: addCommentsDescr } }; function hasOverrides(argv: yargs.Arguments): boolean { @@ -57,6 +62,12 @@ export function parseTtagPluginOpts( extendedOpts["extract"] = { location: argv[opt] }; } else if (opt === "sortByMsgid") { extendedOpts.sortByMsgid = true; + } else if (opt === "addComments") { + let value = argv[opt]; + extendedOpts.addComments = + value === "true" || value === "false" + ? JSON.parse(value) + : value; } else { extendedOpts[opt] = argv[opt]; } @@ -82,6 +93,12 @@ export function mergeOpts(opts1: TtagOpts, opts2: TtagOpts): TtagOpts { newOpts.extract = opts2.extract; } } + if (opts2.hasOwnProperty("sortByMsgid")) { + newOpts.sortByMsgid = opts2.sortByMsgid; + } + if (opts2.hasOwnProperty("addComments")) { + newOpts.addComments = opts2.addComments; + } return newOpts; } diff --git a/src/types.ts b/src/types.ts index dd26013..d758ed7 100644 --- a/src/types.ts +++ b/src/types.ts @@ -10,7 +10,7 @@ export type TtagOpts = { discover?: string[]; numberedExpressions?: boolean; sortByMsgid?: boolean; - addComments?: boolean; + addComments?: boolean | string; }; export type Progress = { diff --git a/tests/commands/test_update.ts b/tests/commands/test_update.ts index dcd4034..98a1ce0 100644 --- a/tests/commands/test_update.ts +++ b/tests/commands/test_update.ts @@ -69,11 +69,35 @@ test("should extract comments by default", () => { fs.writeFileSync(tmpFile.name, originalPo); execSync(`ts-node src/index.ts update ${tmpFile.name} ${commentsTest}`); const result = fs.readFileSync(tmpFile.name).toString(); - expect(result).toContain("#. translator: test comment"); + expect(result).toContain("#. test comment"); expect(result).toContain("#. translator: jsx test comment"); tmpFile.removeCallback(); }); +test("should not extract comments, via addComments option", () => { + const tmpFile = tmp.fileSync(); + fs.writeFileSync(tmpFile.name, originalPo); + execSync( + `ts-node src/index.ts update --addComments=false ${tmpFile.name} ${commentsTest}` + ); + const result = fs.readFileSync(tmpFile.name).toString(); + expect(result).not.toContain("#. test comment"); + expect(result).not.toContain("#. translator: jsx test comment"); + tmpFile.removeCallback(); +}); + +test("should extract comments with prefix, via addComments option", () => { + const tmpFile = tmp.fileSync(); + fs.writeFileSync(tmpFile.name, originalPo); + execSync( + `ts-node src/index.ts update --addComments=translator: ${tmpFile.name} ${commentsTest}` + ); + const result = fs.readFileSync(tmpFile.name).toString(); + expect(result).not.toContain("#. test comment"); + expect(result).toContain("#. jsx test comment"); + tmpFile.removeCallback(); +}); + const contextTest = path.resolve( __dirname, "../fixtures/updateTest/context.jsx" diff --git a/tests/fixtures/updateTest/comments.jsx b/tests/fixtures/updateTest/comments.jsx index 98e6954..ba36cde 100644 --- a/tests/fixtures/updateTest/comments.jsx +++ b/tests/fixtures/updateTest/comments.jsx @@ -1,6 +1,6 @@ import { jt , t } from "ttag"; -// translator: test comment +// test comment t`test` const Component = () => {