Skip to content

Commit 5384782

Browse files
committed
feat: add addComments option
1 parent e4fa051 commit 5384782

File tree

7 files changed

+63
-12
lines changed

7 files changed

+63
-12
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ will extract translations to .pot file
3030
--numberedExpressions boolean overrides babel-plugin-ttag setting - https://ttag.js.org/docs/plugin-api.html#confignumberedexpressions. Refer to the doc for the details.
3131
--extractLocation string - 'full' | 'file' | 'never' - https://ttag.js.org/docs/plugin-api.html#configextractlocation. Is used to format location comments in the .po file.
3232
--sortByMsgid boolean. Will sort output in alphabetically by msgid. https://ttag.js.org/docs/plugin-api.html#configsortbymsgid
33-
33+
--addComments boolean | string. Will extract leading comments before a translatable string. https://ttag.js.org/docs/plugin-api.html#configaddcomments
3434

3535
### `check [lang] <pofile> <src...>`
3636
will check if all translations are present in .po file

package-lock.json

Lines changed: 16 additions & 6 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/lib/extract.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ export async function extractAll(
2323
const tmpFile = tmp.fileSync();
2424
let ttagOpts: ttagTypes.TtagOpts = {
2525
extract: { output: tmpFile.name },
26-
sortByMsgid: overrideOpts && overrideOpts.sortByMsgid,
26+
sortByMsgid: false,
2727
addComments: true
2828
};
2929
if (lang !== "en") {

src/lib/ttagPluginOverride.ts

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,16 @@ const sortByMsgidDescr = `boolean - The resulting output will be sorted alphabet
2424
"#configsortbymsgid"
2525
)}`;
2626

27+
const addCommentsDescr = `boolean | string - The resulting output will be sorted alphabetically. ${doc(
28+
"#configaddcomments"
29+
)}`;
30+
2731
const OPTS: { [k: string]: { description: string; boolean?: boolean } } = {
2832
discover: { description: discoverDescription },
2933
numberedExpressions: { description: numberedExpressionsDescr },
3034
extractLocation: { description: extractLocationDescr },
31-
sortByMsgid: { description: sortByMsgidDescr, boolean: true }
35+
sortByMsgid: { description: sortByMsgidDescr, boolean: true },
36+
addComments: { description: addCommentsDescr }
3237
};
3338

3439
function hasOverrides(argv: yargs.Arguments): boolean {
@@ -57,6 +62,12 @@ export function parseTtagPluginOpts(
5762
extendedOpts["extract"] = { location: argv[opt] };
5863
} else if (opt === "sortByMsgid") {
5964
extendedOpts.sortByMsgid = true;
65+
} else if (opt === "addComments") {
66+
let value = argv[opt];
67+
extendedOpts.addComments =
68+
value === "true" || value === "false"
69+
? JSON.parse(value)
70+
: value;
6071
} else {
6172
extendedOpts[opt] = argv[opt];
6273
}
@@ -82,6 +93,12 @@ export function mergeOpts(opts1: TtagOpts, opts2: TtagOpts): TtagOpts {
8293
newOpts.extract = opts2.extract;
8394
}
8495
}
96+
if (opts2.hasOwnProperty("sortByMsgid")) {
97+
newOpts.sortByMsgid = opts2.sortByMsgid;
98+
}
99+
if (opts2.hasOwnProperty("addComments")) {
100+
newOpts.addComments = opts2.addComments;
101+
}
85102
return newOpts;
86103
}
87104

src/types.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ export type TtagOpts = {
1010
discover?: string[];
1111
numberedExpressions?: boolean;
1212
sortByMsgid?: boolean;
13-
addComments?: boolean;
13+
addComments?: boolean | string;
1414
};
1515

1616
export type Progress = {

tests/commands/test_update.ts

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,11 +69,35 @@ test("should extract comments by default", () => {
6969
fs.writeFileSync(tmpFile.name, originalPo);
7070
execSync(`ts-node src/index.ts update ${tmpFile.name} ${commentsTest}`);
7171
const result = fs.readFileSync(tmpFile.name).toString();
72-
expect(result).toContain("#. translator: test comment");
72+
expect(result).toContain("#. test comment");
7373
expect(result).toContain("#. translator: jsx test comment");
7474
tmpFile.removeCallback();
7575
});
7676

77+
test("should not extract comments, via addComments option", () => {
78+
const tmpFile = tmp.fileSync();
79+
fs.writeFileSync(tmpFile.name, originalPo);
80+
execSync(
81+
`ts-node src/index.ts update --addComments=false ${tmpFile.name} ${commentsTest}`
82+
);
83+
const result = fs.readFileSync(tmpFile.name).toString();
84+
expect(result).not.toContain("#. test comment");
85+
expect(result).not.toContain("#. translator: jsx test comment");
86+
tmpFile.removeCallback();
87+
});
88+
89+
test("should extract comments with prefix, via addComments option", () => {
90+
const tmpFile = tmp.fileSync();
91+
fs.writeFileSync(tmpFile.name, originalPo);
92+
execSync(
93+
`ts-node src/index.ts update --addComments=translator: ${tmpFile.name} ${commentsTest}`
94+
);
95+
const result = fs.readFileSync(tmpFile.name).toString();
96+
expect(result).not.toContain("#. test comment");
97+
expect(result).toContain("#. jsx test comment");
98+
tmpFile.removeCallback();
99+
});
100+
77101
const contextTest = path.resolve(
78102
__dirname,
79103
"../fixtures/updateTest/context.jsx"

tests/fixtures/updateTest/comments.jsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { jt , t } from "ttag";
22

3-
// translator: test comment
3+
// test comment
44
t`test`
55

66
const Component = () => {

0 commit comments

Comments
 (0)