You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
While it's not rocket science, I think it might be useful for others that are figuring out how to integrate react-intl in their TypeScript project. A lot of guides mention babel-plugin-react-intl, but I can't use that as I'm not using Babel, or compiling to ES6. Using typescript-react-intl is a lot easier. I still want to manage my translations with react-intl-translations-manager though.
In short, the following approach is used:
all messages are extracted using typescript-react-intl, and written to a temporary allMessages.json file.
constDEFAULT_LANGUAGE='en-US';// for the default language, everything is automatically whitelistedconstLANGUAGES=['nl-BE','fr-BE'];// translations to generate---don't include the default languageconstTARGET_DIRECTORY='src/translations';constEXTRACT_MESSAGE_FILE_PATTERN='src/**/*.@(tsx|ts)';constTEMP_DIR='./temp';constTEMP_MESSAGE_FILENAME='allMessages.json';constfs=require('fs');constpath=require('path');constglob=require('glob');constrimraf=require('rimraf');constparser=require('typescript-react-intl').default;constmanageTranslations=require('react-intl-translations-manager').default;const{readMessageFiles, getDefaultMessages}=require('react-intl-translations-manager');functionextractTranslations(pattern,cb){letresults=[];pattern=pattern||'src/**/*.@(tsx|ts)';glob(pattern,function(err,files){if(err){thrownewError(err);}files.forEach(function(f){constcontents=fs.readFileSync(f).toString();constres=parser(contents);results=results.concat(res);});cb&&cb(results);});}if(!fs.existsSync(TEMP_DIR)){fs.mkdirSync(TEMP_DIR);}consttempMessageFilePath=path.join(TEMP_DIR,TEMP_MESSAGE_FILENAME);extractTranslations(EXTRACT_MESSAGE_FILE_PATTERN,function(messages){fs.writeFileSync(tempMessageFilePath,JSON.stringify(messages,null,2));manageTranslations({messagesDirectory: TEMP_DIR,translationsDirectory: TARGET_DIRECTORY,languages: [DEFAULT_LANGUAGE, ...LANGUAGES],// avoid reporting translation issues with default language - https://github.com/GertjanReynaert/react-intl-translations-manager/issues/76overrideCoreMethods: {provideWhitelistFile: (language)=>{// Avoid reporting untranslated stuff in defaultLanguageif(language.lang===DEFAULT_LANGUAGE){constmessageFiles=readMessageFiles(TEMP_DIR);constmessages=getDefaultMessages(messageFiles).messages;returnObject.keys(messages);}else{if(!fs.existsSync(language.whitelistFilepath)){return[];}returnJSON.parse(fs.readFileSync(language.whitelistFilepath,'utf-8'));}}}});rimraf.sync(TEMP_DIR);});
For convenience, you can add this to your npm scripts:
Hi,
I just wanted to share an example integration with
react-intl-translations-manager
.While it's not rocket science, I think it might be useful for others that are figuring out how to integrate
react-intl
in their TypeScript project. A lot of guides mentionbabel-plugin-react-intl
, but I can't use that as I'm not using Babel, or compiling to ES6. Usingtypescript-react-intl
is a lot easier. I still want to manage my translations withreact-intl-translations-manager
though.In short, the following approach is used:
typescript-react-intl
, and written to a temporaryallMessages.json
file.react-intl-translations-manager
uses thisallMessages.json
file to manage the translations insrc/translations
.Dependencies:
typescript-react-intl
react-intl-translations-manager
rimraf
The script (
scripts/manageTranslations.js
):For convenience, you can add this to your npm scripts:
Hope this helps someone!
The text was updated successfully, but these errors were encountered: