Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add excludeFuzzy option #4

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
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
24 changes: 20 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,30 @@ addLocale("en-GB`", en_GB);

#### Webpack rules

1. Tell webpack how to load PO files

```js
config.module.rules.push({
test: [/\.po$/],
loader: "ttag-po-loader",
});
```

To exclude translations with the fuzzy flag, add option `excludeFuzzy` to loader:

```js
// 1. Tell webpack how to load PO files
config.module.rules.push({
test: [/\.po$/],
loader: "ttag-po-loader",
test: [/\.po$/],
loader: "ttag-po-loader",
options: {
excludeFuzzy: true,
},
Comment on lines +31 to +39
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have a slight preference to documenting options in a more generic way, for example, use this code example and list the possible options:

config.module.rules.push({
  test: [/\.po$/],
  loader: "ttag-po-loader",
  options: {
    excludeFuzzy: true,
  },
});

Available options:

  • excludeFuzzy: blah blah blah long description what it does

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd also move the options bit below CRA, maybe...

  • if CRA caveat is still valid, which is then rather important for new users.
  • if excludeFuzzy is a rare case.

However, if you recon that exceludeFuzzy should be the default, then let's make exclusion default and add inclusion via a flag.

});
```

// 2. Exempt PO files from default rules if you use create-react-app
2. Exempt PO files from default rules if you use create-react-app

```js
const loaders = config.module.rules.find(r => r.oneOf).oneOf;
loaders.find(l => l?.loader?.match(/file-loader/)).exclude.push(/[.]po$/);
```
19 changes: 14 additions & 5 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,24 @@
const parser = require("ttag-cli/dist/src/lib/parser");
const utils = require("ttag-cli/dist/src/lib/utils");

module.exports = function (source) {
if (this.cacheable) this.cacheable();
const excludeFuzzy = this.getOptions().excludeFuzzy;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this.getOptions is available since Webpack 5

so says someone on stack overflow.. that should be good enough, I think?


let poData = parser.parse(source);

const messages = utils.iterateTranslations(poData.translations);
const header = messages.next().value;
delete header.comments;
for (const msg of messages) { delete msg.comments; }
for (const ctxt of Object.values(poData.translations)) {
for (const msgid of Object.keys(ctxt)) {
Comment on lines +9 to +10
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't recall why I used that specific idiom...

  • maybe it was specifically to drop header comments
  • maybe because of ES level?
  • or maybe I was lazy?

Please check that the semantics are still the same 🙇🏻

const msg = ctxt[msgid];
if (msg.comments) {
if (excludeFuzzy && msg.comments.flag
&& msg.comments.flag.includes('fuzzy')) {
Comment on lines +13 to +14
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was thinking if conditional chaining could be used, but that's only available since 2020, so maybe it's better to stay safe like this...

delete ctxt[msgid];
} else {
delete msg.comments;
}
}
}
}

value = JSON.stringify(poData)
.replace(/\u2028/g, '\\u2028')
Expand Down