lukasgeiter/gettext-extractor

Comments don't get extracted

Closed this issue · 7 comments

StasD commented

Hi Lukas!
This is great utility, but could you please tweak an algorithm for extracting comments a bit.
E.g. in this situation:

<Text style={styles.listitemtextbold} numberOfLines={1}>
  {/* Translators: 'grade' in the meaning of a school class, e.g. 'sixth grade'. */
  gettext('Select grade')}
</Text>

comments don't get extracted at all.
If I put them on the same line, it works, but eslint / prettier doesn't like it and starts complaining (underlining)...
I think you need to include comments which are on the line above, if no comments found on the same line.
Thanks!

StasD commented

Figured out how to put comments on the same line so that 'prettier' is happy:

<Text style={styles.listitemtextbold} numberOfLines={1}>
  {gettext(
    'Select grade',
  ) /* Translators: 'grade' in the meaning of a school class, e.g. 'sixth grade'. */}
</Text>

Though tweaking comments extraction algorithm would still be nice.

The extractor already supports pulling comments from the previous line, however the feature is disabled by default.

You can enable it using the comments option object:

JsExtractors.callExpression('gettext', {
    arguments: { ... },
    comments: {
        otherLineLeading: true
    }
})

Note: If you want to keep extracting comments on the same line (like it does by default) set sameLineLeading and sameLineTrailing to true as well.

See the API Reference for documentation of all comment options.

StasD commented

Cool! Thanks!

StasD commented

They (comments) still required to be in the same scope where the function call happens?
E.g. this works:

    return {
      topBar: {
        title: super.getTitle(
          // Translators: 'grade' in the meaning of a school class, e.g. 'sixth grade'
          gettext('Grades'),
        ),
      },
    };

While this doesn't:

    return {
      topBar: {
        // Translators: 'grade' in the meaning of a school class, e.g. 'sixth grade'
        title: super.getTitle(gettext('Grades')),
      },
    };

Yes that's how it currently works. The intention behind this is to avoid false positive matches which would result in extracting comments that have nothing to do with the translation.

I realize that if you're using a marker a the beginning of the comment together with the regex option, this wouldn't really be necessary. Maybe it would be possible to loosen the comment extraction criteria a bit when a regex is used or a new option is enabled.

StasD commented

OK, I understand. I guess as soon as I know how the comments work, I will be able to use them.
Thanks for the explanation! And for the utility! Yesterday I spent some time trying to figure out how to do translation of my app, which file format to use etc. As soon as I found your program, everything became very clear. :)

I'm glad to hear that 👍