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
2 changes: 1 addition & 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

--exclude -e exclude files or directories matching the given glob pattern(s) from extraction

### `check [lang] <pofile> <src...>`
will check if all translations are present in .po file
Expand Down
9 changes: 7 additions & 2 deletions src/commands/extract.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,23 @@ async function extract(
paths: string[],
lang: string = "en",
ttagOverrideOpts?: c3poTypes.TtagOpts,
ttagRcOpts?: c3poTypes.TtagRc
ttagRcOpts?: c3poTypes.TtagRc,
exclude?: string[]
) {
const progress: c3poTypes.Progress = ora(
`[ttag] extracting translations to ${output} ...`
);
progress.start();

const excludeRegexp = exclude ? new RegExp(exclude.join("|")) : undefined;

const result = await extractAll(
paths,
lang,
progress,
ttagOverrideOpts,
ttagRcOpts
ttagRcOpts,
excludeRegexp
);
fs.writeFileSync(output, result);
progress.succeed(`[ttag] translations extracted to ${output}`);
Expand Down
8 changes: 7 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,11 @@ yargs
default: "en",
description: "sets default lang (ISO format)"
},
exclude: {
alias: "e",
type: "array",
description: "exclude files or directories from extraction"
},
...getTtagOptsForYargs()
},
argv => {
Expand All @@ -92,7 +97,8 @@ yargs
argv.src,
argv.lang,
parseTtagPluginOpts(argv),
parseTtagRcOpts()
parseTtagRcOpts(),
argv.exclude // pass the exclude option value to the extract function
);
}
)
Expand Down
10 changes: 8 additions & 2 deletions src/lib/extract.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ export async function extractAll(
lang: string,
progress: ttagTypes.Progress,
overrideOpts?: ttagTypes.TtagOpts,
rcOpts?: ttagTypes.TtagRc
rcOpts?: ttagTypes.TtagRc,
excludeRegexp?: RegExp
): Promise<string> {
const tmpFile = tmp.fileSync();
let ttagOpts: ttagTypes.TtagOpts = {
Expand All @@ -34,6 +35,11 @@ export async function extractAll(
}
const babelOptions = makeBabelConf(ttagOpts);
const transformFn: TransformFn = filepath => {
// exclude paths
if (excludeRegexp && excludeRegexp.test(filepath)) {
return;
}

try {
switch (extname(filepath)) {
case ".vue": {
Expand Down Expand Up @@ -116,9 +122,9 @@ export async function extractAll(
}
progress.fail("Failed to extract translations");
process.exit(1);
return;
}
};

await pathsWalk(
getWalkingPaths(paths, rcOpts),
progress,
Expand Down
43 changes: 43 additions & 0 deletions tests/commands/__snapshots__/test_extract.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,49 @@ msgstr \\"\\"
"
`;

exports[`extract with e alias 1`] = `
"msgid \\"\\"
msgstr \\"\\"
\\"Content-Type: text/plain; charset=utf-8\\\\n\\"
\\"Plural-Forms: nplurals=2; plural=(n!=1);\\\\n\\"

#: tests/fixtures/extractExcludeTest/test1.js:4
#, javascript-format
msgid \\"test translation \${ name }\\"
msgstr \\"\\"
"
`;

exports[`extract with exclude by regexp 1`] = `
"msgid \\"\\"
msgstr \\"\\"
\\"Content-Type: text/plain; charset=utf-8\\\\n\\"
\\"Plural-Forms: nplurals=2; plural=(n!=1);\\\\n\\"

#: tests/fixtures/extractExcludeTest/test1.js:4
#, javascript-format
msgid \\"test translation \${ name }\\"
msgstr \\"\\"
"
`;

exports[`extract with excludePaths 1`] = `
"msgid \\"\\"
msgstr \\"\\"
\\"Content-Type: text/plain; charset=utf-8\\\\n\\"
\\"Plural-Forms: nplurals=2; plural=(n!=1);\\\\n\\"

#: tests/fixtures/extractExcludeTest/test1.js:4
#, javascript-format
msgid \\"test translation \${ name }\\"
msgstr \\"\\"

#: tests/fixtures/extractExcludeTest/excluded/test2.js:4
msgid \\"test translation 2 \${ name }\\"
msgstr \\"\\"
"
`;

exports[`should extract in the alphabetical order (sortByMsgid) 1`] = `
"msgid \\"\\"
msgstr \\"\\"
Expand Down
28 changes: 28 additions & 0 deletions tests/commands/test_extract.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@ const sveltePath = path.resolve(
__dirname,
"../fixtures/testSvelteParse.svelte"
);
const extractExcludePath = path.resolve(
__dirname,
"../fixtures/extractExcludeTest"
);
const globalFn = path.resolve(__dirname, "../fixtures/globalFunc.js");
const tsPath = path.resolve(__dirname, "../fixtures/tSParse.ts");
const tsChaning = path.resolve(__dirname, "../fixtures/tsOptionalChaning.ts");
Expand Down Expand Up @@ -132,3 +136,27 @@ test("extract from ts with const enum", () => {
const result = fs.readFileSync(potPath).toString();
expect(result).toMatchSnapshot();
});

test("extract with excludePaths", () => {
execSync(
`ts-node src/index.ts extract --exclude="nested" -o ${potPath} ${extractExcludePath}`
);
const result = fs.readFileSync(potPath).toString();
expect(result).toMatchSnapshot();
});

test("extract with exclude by regexp", () => {
execSync(
`ts-node src/index.ts extract --exclude="nested|excluded" -o ${potPath} ${extractExcludePath}`
);
const result = fs.readFileSync(potPath).toString();
expect(result).toMatchSnapshot();
});

test("extract with e alias", () => {
execSync(
`ts-node src/index.ts extract -e "nested" "excluded" -o ${potPath} ${extractExcludePath}`
);
const result = fs.readFileSync(potPath).toString();
expect(result).toMatchSnapshot();
});
4 changes: 4 additions & 0 deletions tests/fixtures/extractExcludeTest/excluded/nested/test2.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import { t, c } from 'ttag';

const name = 'Mike';
t`test translation 2 ${name}`;
4 changes: 4 additions & 0 deletions tests/fixtures/extractExcludeTest/excluded/test2.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import { t, c } from 'ttag';

const name = 'Mike';
t`test translation 2 ${name}`;
4 changes: 4 additions & 0 deletions tests/fixtures/extractExcludeTest/nested/test2.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import { t, c } from 'ttag';

const name = 'Mike';
t`test translation 2 ${name}`;
3 changes: 3 additions & 0 deletions tests/fixtures/extractExcludeTest/test.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<test>
<shouldnottransform/>
</test>
4 changes: 4 additions & 0 deletions tests/fixtures/extractExcludeTest/test1.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import { t } from 'ttag';

const name = 'Mike';
t`test translation ${name}`;