Skip to content

Extracts string messages from TypeScript React components or ts files that use React Intl.

License

Notifications You must be signed in to change notification settings

Trendays/typescript-react-intl

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

30 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

typescript-react-intl

Extracts string messages from TypeScript React components or ts files that use React Intl.

Build Status

Usage

If you have many files to processes,you can get contents use node-glob with fs module

npm i typescript-react-intl -D

var parse = require('typescript-react-intl').default;

// results is an array
// contents is your tsx file 
var results = parse(contents)

React-intl

Only support <FormattedMessage/> and defineMessages We don't use <FormattedHtmlMessage/>

Examples

var fs = require('fs');
var glob = require('glob');
var parser = require('typescript-react-intl').default;

function runner (pattern, cb) {
  var results = [];
  pattern = pattern || 'src/**/*.@(tsx|ts)';
  glob(pattern, function (err, files) {   
    if (err) {
      throw new Error(err);
    }
    files.forEach(f => {
      var contents = fs.readFileSync(f).toString();
      var res = parser(contents);
      results = results.concat(res);
    });
   
    cb && cb(results);
  });
}

// demo 
runner(null, function (res) {

  var locale = {};

  res.forEach(r => {
    locale[r.id] = r.defaultMessage;
  });


  var locales = {
    en: locale,  
  };

  // save file to disk。you can save as a json file,just change the ext and contents as you want.
  fs.writeFileSync(`src/translations/all.ts`, `export default ${JSON.stringify(locales, null, 2)}\r`);

});

About

Extracts string messages from TypeScript React components or ts files that use React Intl.

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • TypeScript 70.3%
  • JavaScript 29.7%