Skip to content
This repository has been archived by the owner on Jun 8, 2019. It is now read-only.

Add an option to include original line numbers within extracted message information #71

Closed
antoinepm opened this issue Sep 2, 2016 · 6 comments

Comments

@antoinepm
Copy link

Now that this plugin helps me extract the messages I need to translate (and it works great, thanks!), I am trying to generate a list of message-related issues (untranslated messages, etc.) further in my process. This is why I could use an additional "originalLineNumber" field in the extracted JSON data (or maybe "sourceLineNumber", or any other self-explaining name, of course). That information would let me find more quickly where, in the source code, the message is declared. Of course, the original file name would be useful too, but I can guess it from the JSON file path. And since not everybody is interested in the information, it could be made optional (maybe an "includeOriginalLineNumbers" option?).

Do you think it would be both possible and relevant?

A quick look at the plugin source code makes me think that the function storeMessage() could get the information from the property path.node.loc.start.line, when available. But then I may not be aware of some Babel subtleties.

@ericf
Copy link
Collaborator

ericf commented Sep 2, 2016

Yeah this is totally possible to add. What do you want to consider as the start of the message being defined? I'm thinking it should be the nested object in defineMessages(), and the <FormattedMessage> element.

I think it makes sense to only save this information to the JSON files when an option is enabled on the Babel plugin.

@antoinepm
Copy link
Author

I agree. Considering the nested object in defineMessages() and the <FormattedMessage> element itself as the start of each message being defined looks good to me.

@ericf
Copy link
Collaborator

ericf commented Sep 2, 2016

@antoinepm I have something working. Let me know your thoughts on whether you want all of this data and if you think it should be structured differently. Here's an example from running it on this file:

[
  {
    "id": "app.title",
    "defaultMessage": "React Intl Translations Example",
    "file": "src/client/components/app.js",
    "start": {
      "line": 23,
      "column": 20
    },
    "end": {
      "line": 26,
      "column": 22
    }
  },
  {
    "id": "app.locales_menu_heading",
    "defaultMessage": "Locales:",
    "file": "src/client/components/app.js",
    "start": {
      "line": 32,
      "column": 20
    },
    "end": {
      "line": 35,
      "column": 22
    }
  }
]

The name of the option is: "extractSourceLocation": true.

ericf added a commit that referenced this issue Sep 2, 2016
This option will provide metadata in the Message Description for the
file and location in the file where the message was defined. When
enabled and running it on [this file][1] you'll get this result:

```json
[
  {
    "id": "app.title",
    "defaultMessage": "React Intl Translations Example",
    "file": "src/client/components/app.js",
    "start": {
      "line": 23,
      "column": 20
    },
    "end": {
      "line": 26,
      "column": 22
    }
  },
  {
    "id": "app.locales_menu_heading",
    "defaultMessage": "Locales:",
    "file": "src/client/components/app.js",
    "start": {
      "line": 32,
      "column": 20
    },
    "end": {
      "line": 35,
      "column": 22
    }
  }
]
```

Fixes #71

[1]: https://github.com/yahoo/react-intl/blob/25f3f64d86ac86281157117b5bdb9fa30d1d615f/examples/translations/src/client/components/app.js
@antoinepm
Copy link
Author

The data presented above looks great to me. Although I would use only some parts for my own purposes, I think that all of it should be included, as it is more self-explanatory than my initial suggestion. I like the extractSourceLocation parameter name, too.
Thanks!

@ericf
Copy link
Collaborator

ericf commented Sep 2, 2016

Cool! I'll get this out soon… but maybe after the long weekend here in the US.

ericf added a commit that referenced this issue Sep 3, 2016
This option will provide metadata in the Message Description for the
file and location in the file where the message was defined. When
enabled and running it on [this file][1] you'll get this result:

```json
[
  {
    "id": "app.title",
    "defaultMessage": "React Intl Translations Example",
    "file": "src/client/components/app.js",
    "start": {
      "line": 23,
      "column": 20
    },
    "end": {
      "line": 26,
      "column": 22
    }
  },
  {
    "id": "app.locales_menu_heading",
    "defaultMessage": "Locales:",
    "file": "src/client/components/app.js",
    "start": {
      "line": 32,
      "column": 20
    },
    "end": {
      "line": 35,
      "column": 22
    }
  }
]
```

Fixes #71

[1]: https://github.com/yahoo/react-intl/blob/25f3f64d86ac86281157117b5bdb9fa30d1d615f/examples/translations/src/client/components/app.js
@ericf ericf closed this as completed in #73 Sep 3, 2016
@antoinepm
Copy link
Author

I see that the change has been published. Thanks a lot!

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants