Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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] <pofile> <src...>`
will check if all translations are present in .po file
Expand Down Expand Up @@ -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] <pofile> <out> <path>`
Expand Down
22 changes: 16 additions & 6 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion src/lib/extract.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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") {
Expand Down
19 changes: 18 additions & 1 deletion src/lib/ttagPluginOverride.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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];
}
Expand All @@ -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;
}

Expand Down
2 changes: 1 addition & 1 deletion src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ export type TtagOpts = {
discover?: string[];
numberedExpressions?: boolean;
sortByMsgid?: boolean;
addComments?: boolean;
addComments?: boolean | string;
};

export type Progress = {
Expand Down
26 changes: 25 additions & 1 deletion tests/commands/test_update.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
2 changes: 1 addition & 1 deletion tests/fixtures/updateTest/comments.jsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { jt , t } from "ttag";

// translator: test comment
// test comment
t`test`

const Component = () => {
Expand Down