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

Is there any simple steps that I can follow in order to use this? #2

Open
ensecoz opened this issue Jul 6, 2017 · 12 comments
Open

Comments

@ensecoz
Copy link

ensecoz commented Jul 6, 2017

Hi,

I am new to typescript world and couldn't find the document how to use this ts-transform-react-intl. Do you have any simple steps? It would be really helpful.

I couldn't find the way to add plugin to typescript like babel. I guess I need to implement my own version of compiler to complete the generate translation. Please help.

Thanks,

@longlho
Copy link
Owner

longlho commented Jul 6, 2017

I'll update the README.md today w/ more instructions. But in short if you use webpack & ts-loader you can specify it as part of the config (https://github.com/TypeStrong/ts-loader#getcustomtransformers-----before-transformerfactory-after-transformerfactory---).

If you use tsc you'd have to write a simple compiler like the one I included in the repo (https://github.com/longlho/ts-transform-react-intl/blob/master/compile.ts)

@fengshenhai-0727
Copy link

fengshenhai-0727 commented Mar 2, 2018

I am new to typescript world and couldn't find the document how to use this ts-transform-react-intl.
It's difficult to use.

@longlho
Copy link
Owner

longlho commented Mar 2, 2018

unfortunately you'd need to a custom compiler wrapper like the one linked above and some knowledge of TypeScript to use this. TS doesn't support custom transformers officially

@Kronuz
Copy link

Kronuz commented Jul 10, 2018

Apparently, ts-loader supports custom transformes using getCustomTransformers (https://github.com/TypeStrong/ts-loader/blob/master/README.md#getcustomtransformers-----before-transformerfactory-after-transformerfactory--) and there is also https://github.com/cevek/ttypescript, which allows transformers in tsconfig.json.

Can someone with more experience add examples for those please?

@Kronuz
Copy link

Kronuz commented Jul 10, 2018

Could it be as simple as this (with ts-loader)?:

const intl = require('ts-transform-react-intl');

module.exports = {
  module: {
    rules: [
      {
        test: /\.ts$/,
        loader: 'ts-loader',
        options: {
          getCustomTransformers: () => ({
            before: [
              intl.transform({
                idPrefix: "prefix",
                onMsgExtracted: intl.aggregate(msgs),
              }),
            ]
          })
        }
      }
    ],
  }
}

@longlho
Copy link
Owner

longlho commented Jul 10, 2018

@Kronuz PR's welcome :)

@Kronuz
Copy link

Kronuz commented Jul 11, 2018

@longlho, what is the idPrefix for?

@longlho
Copy link
Owner

longlho commented Jul 11, 2018

it's the prefix for all message ids. This is basically namespacing. By default it uses the package name from package.json.

e.g: If you run this on a project called foo, the result json of all your messages will be like foo_message1, foo_message2

@martinmckenna
Copy link

@Kronuz Where is the msgs argument coming from in the example you posted?

@Kronuz
Copy link

Kronuz commented Oct 4, 2018

That’s simply an empty object of type Messages where the final messages will be aggregated to, after parsing (it could have default messages, if needed)

@martinmckenna
Copy link

martinmckenna commented Oct 4, 2018

@Kronuz ah okay. Have you been successful in writing that object to a JSON file? Looking for examples, if you have any. I’m using webpack and ts-loader as well

@Kronuz
Copy link

Kronuz commented Oct 4, 2018

I haven’t tried... you just need to make a small webpack plugin which writes the object after everything has been processed.

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

5 participants