Skip to content

Commit

Permalink
Make source file reference verbosity configurable (#27)
Browse files Browse the repository at this point in the history
  • Loading branch information
ThiefMaster authored Sep 26, 2024
1 parent fdaa36a commit 7993ad2
Show file tree
Hide file tree
Showing 6 changed files with 281 additions and 22 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "react-jsx-i18n",
"version": "0.7.0",
"version": "0.8.0",
"description": "Provides gettext-enhanced React components, a babel plugin for extracting the strings and a script to compile translated strings to a format usable by the components.",
"main": "client/index.js",
"types": "client/index.d.ts",
Expand Down
12 changes: 11 additions & 1 deletion src/tools/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,10 +51,20 @@ yargs
type: 'string',
describe: 'base dir to generate relative file paths; default to current dir',
});
y.option('add-location', {
alias: 'L',
type: 'string',
choices: ['full', 'file', 'never'],
default: 'full',
});
},
argv => {
const files = flattenPaths(argv.paths, argv.ext);
const {pot, errors} = extractFromFiles(files, argv.base || process.cwd());
const cfg = {
base: argv.base || process.cwd(),
addLocation: argv.addLocation,
};
const {pot, errors} = extractFromFiles(files, cfg);
if (errors) {
errors.forEach(err => console.error(err));
process.exit(1);
Expand Down
36 changes: 20 additions & 16 deletions src/tools/extract-plugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -99,9 +99,13 @@ const processElement = (path, types, allowParam = false) => {
return string;
};

const getLocation = (base, path, state) => {
const filename = relative(base, state.file.opts.filename);
return `${filename}:${path.node.loc.start.line}`;
const getLocation = (cfg, path, state) => {
const filename = relative(cfg.base, state.file.opts.filename);
if (cfg.addLocation === 'full') {
return `${filename}:${path.node.loc.start.line}`;
} else if (cfg.addLocation === 'file') {
return filename;
}
};

const getContext = path => {
Expand All @@ -110,16 +114,16 @@ const getContext = path => {
return contextAttr ? contextAttr.value.value : undefined;
};

const processTranslate = (base, path, state, types) => {
const processTranslate = (cfg, path, state, types) => {
const translatableString = processElement(path, types, true);
return {
msgid: translatableString,
msgctxt: getContext(path),
reference: getLocation(base, path, state),
reference: getLocation(cfg, path, state),
};
};

const processTranslateString = (base, path, state, funcName, types) => {
const processTranslateString = (cfg, path, state, funcName, types) => {
const args = path.node.arguments;
if (args.length === 0) {
throw path.buildCodeFrameError('Translate.string() called with no arguments');
Expand All @@ -131,11 +135,11 @@ const processTranslateString = (base, path, state, funcName, types) => {
return {
msgid,
msgctxt,
reference: getLocation(base, path, state),
reference: getLocation(cfg, path, state),
};
};

const processPluralTranslate = (base, path, state, types) => {
const processPluralTranslate = (cfg, path, state, types) => {
let singularPath, pluralPath;
path
.get('children')
Expand Down Expand Up @@ -167,11 +171,11 @@ const processPluralTranslate = (base, path, state, types) => {
msgid: processElement(singularPath, types, true),
msgid_plural: processElement(pluralPath, types, true),
msgctxt: getContext(path),
reference: getLocation(base, path, state),
reference: getLocation(cfg, path, state),
};
};

const processPluralTranslateString = (base, path, state, funcName, types) => {
const processPluralTranslateString = (cfg, path, state, funcName, types) => {
const args = path.node.arguments;
if (args.length < 2) {
throw path.buildCodeFrameError('PluralTranslate.string() called with less than 2 arguments');
Expand All @@ -186,21 +190,21 @@ const processPluralTranslateString = (base, path, state, funcName, types) => {
msgid,
msgid_plural,
msgctxt,
reference: getLocation(base, path, state),
reference: getLocation(cfg, path, state),
};
};

const makeI18nPlugin = base => {
const makeI18nPlugin = cfg => {
const entries = [];
const i18nPlugin = ({types}) => {
return {
visitor: {
JSXElement(path, state) {
const elementName = path.node.openingElement.name.name;
if (elementName === 'Translate') {
entries.push(processTranslate(base, path, state, types));
entries.push(processTranslate(cfg, path, state, types));
} else if (elementName === 'PluralTranslate') {
entries.push(processPluralTranslate(base, path, state, types));
entries.push(processPluralTranslate(cfg, path, state, types));
}
},
CallExpression(path, state) {
Expand All @@ -224,9 +228,9 @@ const makeI18nPlugin = base => {
// we got a proper call of one of our translation functions
const qualifiedFuncName = `${elementName}.${funcName}`;
if (elementName === 'Translate') {
entries.push(processTranslateString(base, path, state, qualifiedFuncName, types));
entries.push(processTranslateString(cfg, path, state, qualifiedFuncName, types));
} else if (elementName === 'PluralTranslate') {
entries.push(processPluralTranslateString(base, path, state, qualifiedFuncName, types));
entries.push(processPluralTranslateString(cfg, path, state, qualifiedFuncName, types));
}
},
},
Expand Down
4 changes: 2 additions & 2 deletions src/tools/extract.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ import moment from 'moment-timezone';
import {mergeEntries} from 'babel-plugin-extract-text/src/builders';
import makeI18nPlugin from './extract-plugin';

const extractFromFiles = (files, base, headers = undefined, highlightErrors = true) => {
const extractFromFiles = (files, cfg, headers = undefined, highlightErrors = true) => {
const errors = [];
const {i18nPlugin, entries} = makeI18nPlugin(base);
const {i18nPlugin, entries} = makeI18nPlugin(cfg);

files.forEach(file => {
try {
Expand Down
243 changes: 243 additions & 0 deletions tests/__snapshots__/extract.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -652,6 +652,249 @@ msgstr \\"\\"",
}
`;
exports[`Messages are properly extracted 4`] = `
Object {
"pot": "msgid \\"\\"
msgstr \\"\\"
\\"POT-Creation-Date: 2018-04-18 22:20+0000\\\\n\\"
\\"Content-Type: text/plain; charset=utf-8\\\\n\\"
\\"Content-Transfer-Encoding: 8bit\\\\n\\"
\\"MIME-Version: 1.0\\\\n\\"
\\"Generated-By: react-jsx-i18n-extract\\\\n\\"
#: test-data/example.jsx
msgid \\"\\\\\\"<strong>Rats.</strong>\\\\\\"\\"
msgstr \\"\\"
#: test-data/example.jsx
msgid \\"foobar\\"
msgstr \\"\\"
#: test-data/example.jsx
msgid \\"You are ugly!\\"
msgstr \\"\\"
#: test-data/example.jsx
msgid \\"space invader\\"
msgstr \\"\\"
#: test-data/example.jsx
msgid \\"cat\\"
msgid_plural \\"cats\\"
msgstr[0] \\"\\"
msgstr[1] \\"\\"
#: test-data/example.jsx
msgid \\"one {foo}\\"
msgid_plural \\"many {foo}\\"
msgstr[0] \\"\\"
msgstr[1] \\"\\"
#: test-data/example.jsx
msgid \\"foo {weird} bar {hello}{test} xxx\\"
msgstr \\"\\"
#: test-data/example.jsx
msgid \\"foo: {foo}\\"
msgstr \\"\\"
#: test-data/example.jsx
msgid \\"mixed: {mixedCase}\\"
msgstr \\"\\"
#: test-data/example.jsx
msgid \\"You have {count} rat.\\"
msgid_plural \\"You have {count} rats.\\"
msgstr[0] \\"\\"
msgstr[1] \\"\\"
#: test-data/example.jsx
msgid \\"Hello & World\\"
msgstr \\"\\"
#: test-data/example.jsx
msgid \\"Hey {name}, you want to {link}click me{/link} and you know it!\\"
msgstr \\"\\"
#: test-data/example.jsx
msgid \\"Bye World\\"
msgstr \\"\\"
#: test-data/example.jsx
msgid \\"Some <HTML> & entities: → &\\"
msgstr \\"\\"
#: test-data/example.jsx
msgid \\"Hover me\\"
msgstr \\"\\"
#: test-data/example.jsx
msgid \\"<HTML> is unescaped when extracted\\"
msgstr \\"\\"
#: test-data/example.jsx
msgid \\"xxx foo bar{test}{x} y{/x} moo\\"
msgstr \\"\\"
#: test-data/example.jsx
msgid \\"Little cats:\\"
msgstr \\"\\"
#: test-data/example.jsx
msgid \\"Little dogs:\\"
msgstr \\"\\"
#: test-data/example.jsx
msgid \\"This is an emphasized dynamic value: {number}\\"
msgstr \\"\\"
#: test-data/example.jsx
msgid \\"This is an emphasized translated value: {tag}hello{/tag}\\"
msgstr \\"\\"
#: test-data/example.jsx
msgid \\"This param is using string literals: {emphasize}hello world{/emphasize}\\"
msgstr \\"\\"
#: test-data/example.jsx
msgctxt \\"cat\\"
msgid \\"offspring\\"
msgstr \\"\\"
#: test-data/example.jsx
msgctxt \\"big\\"
msgid \\"cat\\"
msgid_plural \\"cats\\"
msgstr[0] \\"\\"
msgstr[1] \\"\\"
#: test-data/example.jsx
msgctxt \\"c\\"
msgid \\"one {foo}\\"
msgid_plural \\"many {foo}\\"
msgstr[0] \\"\\"
msgstr[1] \\"\\"
#: test-data/example.jsx
msgctxt \\"params-test\\"
msgid \\"foo: {foo}\\"
msgstr \\"\\"
#: test-data/example.jsx
msgctxt \\"dog\\"
msgid \\"offspring\\"
msgstr \\"\\"",
}
`;
exports[`Messages are properly extracted 5`] = `
Object {
"pot": "msgid \\"\\"
msgstr \\"\\"
\\"POT-Creation-Date: 2018-04-18 22:20+0000\\\\n\\"
\\"Content-Type: text/plain; charset=utf-8\\\\n\\"
\\"Content-Transfer-Encoding: 8bit\\\\n\\"
\\"MIME-Version: 1.0\\\\n\\"
\\"Generated-By: react-jsx-i18n-extract\\\\n\\"
msgid \\"\\\\\\"<strong>Rats.</strong>\\\\\\"\\"
msgstr \\"\\"
msgid \\"foobar\\"
msgstr \\"\\"
msgid \\"You are ugly!\\"
msgstr \\"\\"
msgid \\"space invader\\"
msgstr \\"\\"
msgid \\"cat\\"
msgid_plural \\"cats\\"
msgstr[0] \\"\\"
msgstr[1] \\"\\"
msgid \\"one {foo}\\"
msgid_plural \\"many {foo}\\"
msgstr[0] \\"\\"
msgstr[1] \\"\\"
msgid \\"foo {weird} bar {hello}{test} xxx\\"
msgstr \\"\\"
msgid \\"foo: {foo}\\"
msgstr \\"\\"
msgid \\"mixed: {mixedCase}\\"
msgstr \\"\\"
msgid \\"You have {count} rat.\\"
msgid_plural \\"You have {count} rats.\\"
msgstr[0] \\"\\"
msgstr[1] \\"\\"
msgid \\"Hello & World\\"
msgstr \\"\\"
msgid \\"Hey {name}, you want to {link}click me{/link} and you know it!\\"
msgstr \\"\\"
msgid \\"Bye World\\"
msgstr \\"\\"
msgid \\"Some <HTML> & entities: → &\\"
msgstr \\"\\"
msgid \\"Hover me\\"
msgstr \\"\\"
msgid \\"<HTML> is unescaped when extracted\\"
msgstr \\"\\"
msgid \\"xxx foo bar{test}{x} y{/x} moo\\"
msgstr \\"\\"
msgid \\"Little cats:\\"
msgstr \\"\\"
msgid \\"Little dogs:\\"
msgstr \\"\\"
msgid \\"This is an emphasized dynamic value: {number}\\"
msgstr \\"\\"
msgid \\"This is an emphasized translated value: {tag}hello{/tag}\\"
msgstr \\"\\"
msgid \\"This param is using string literals: {emphasize}hello world{/emphasize}\\"
msgstr \\"\\"
msgctxt \\"cat\\"
msgid \\"offspring\\"
msgstr \\"\\"
msgctxt \\"big\\"
msgid \\"cat\\"
msgid_plural \\"cats\\"
msgstr[0] \\"\\"
msgstr[1] \\"\\"
msgctxt \\"c\\"
msgid \\"one {foo}\\"
msgid_plural \\"many {foo}\\"
msgstr[0] \\"\\"
msgstr[1] \\"\\"
msgctxt \\"params-test\\"
msgid \\"foo: {foo}\\"
msgstr \\"\\"
msgctxt \\"dog\\"
msgid \\"offspring\\"
msgstr \\"\\"",
}
`;
exports[`Non-string call ignored 1`] = `
Object {
"pot": "msgid \\"\\"
Expand Down
Loading

0 comments on commit 7993ad2

Please sign in to comment.