Research
Security News
Malicious npm Packages Inject SSH Backdoors via Typosquatted Libraries
Socket’s threat research team has detected six malicious npm packages typosquatting popular libraries to insert SSH backdoors.
react-intl-translations-manager
Advanced tools
Manage all translations based on the extracted messages of the babel-plugin-react-intl
React-intl-translations-manager will help you in managing your translations. Hereby it will give you the current status of your translation, telling you what duplicate keys you have, what messages aren't translated yet, what messages were added/deleted since the last time you checked.
You'll still need to update the translations manually in your json files, but now you know what messages you still need to update.
npm install --save-dev react-intl-translations-manager
Since you need the babel-plugin-react-intl
to extract the messages, I'll assume you're using babel in your project.
This is an example of the most basic usage of this plugin, in the API documentation below you can find more options.
Create a script in your package.json
{
"scripts": {
"manage:translations": "babel-node ./translationRunner.js"
}
}
Create a file with your config you can run with the npm script
// translationRunner.js
import manageTranslations from 'react-intl-translations-manager';
manageTranslations({
messagesDirectory: 'src/translations/extractedMessages',
translationsDirectory: 'src/translations/locales/',
languages: ['nl'], // any language you need
});
Run the translation manager with your new npm script
npm run manage:translations
Build your own translationManager based on the core of this package, or it's exposed helper methods.
Now you can check the status of your translations by just running the script. Then you can change the missing translations in the translation files.
If you encounter messages that are identical in translation in a certain language as in your default language (example: Dashboard (english) = Dashboard (dutch)), then you can whitelist the translation-key in the language specific whitelist file. This will prevent the message from showing up as untranslated when checking the translations status.
This will maintain all translation files. Based on your config you will get output for duplicate ids, and per specified language you will get the deleted translations, added messages (new messages that need to be translated), and not yet translated messages. It will also maintain a whitelist file per language where you can specify translation keys where the translation is identical to the default message. This way you can avoid untranslated message warnings for these messages.
You can optionally pass a printer object to this method. This way you can override the console logging with your own logging logic. If you want custom file writing logic, it is advised to roll your own translationManager based on the core.
messagesDirectory
(required),
src/locales/extractedMessages
translationsDirectory
(required),
src/locales/lang
whitelistsDirectory
(optional, default: translationsDirectory
)
unmaintained translation
warnings.Dashboard
in english is also accepted as a valid translation for
dutch.languages
(optional, default: []
)
['nl', 'fr']
the translation manager will maintain a nl.json
, fr.json
, whitelist_nl.json
and a whitelist_fr.json
filesingleMessagesFile
(optional, default: false
)
[
{
"path": "src/components/foo.json",
"descriptors": [
{
"id": "bar",
"description": "Text for bar",
"defaultMessage": "Bar",
}
]
}
]
detectDuplicateIds
(optional, default: true
)
sortKeys
(optional, default: true
)
printers
(optional, default: {})
const printers = {
printDuplicateIds: ( duplicateIds ) => { console.log(`You have ${duplicateIds.length } duplicate IDs`) },
printLanguageReport: ( report ) => { console.log('Log report for a language') },
printNoLanguageFile: ( lang ) => { console.log(`No existing ${lang} translation file found. A new one is created.`) },
printNoLanguageWhitelistFile: ( lang ) => { console.log(`No existing ${lang} file found. A new one is created.`) },
};
core(languages, hooks);
This is the core of the translationManager. It just takes a list of languages and an object with all kinds of hooks it will execute when running. Below you can find all hooks.
const hooks = {
provideExtractedMessages,
outputSingleFile,
outputDuplicateKeys,
beforeReporting,
provideLangTemplate,
provideTranslationsFile,
provideWhitelistFile,
reportLanguage,
afterReporting,
};
const extractedMessages = provideExtractedMessages();
Here you should return all extracted messages. This should be an array, with an object per file. Each object should at least contain a descriptors
key which in turn has an array of message objects. Each message object should at least contain the id and message.
Example:
// Minimal expected return value
const extractedMessages = [
{
descriptors: [
{
id: 'foo_ok',
defaultMessage: 'OK',
},
],
},
];
outputSingleFile(extractedMessages);
This gives you the option to output the extractedMessages. This way you can for example shrink all extracted files into a single File containing all messages.
outputDuplicateKeys(duplicateIds);
This gives you the option to warn for duplicate ids.
beforeReporting();
Here you can do the preparation of the reporting, like creating the necessary folders, or printing a start message
const languageResults = provideLangTemplate(lang);
Here you should provide the template for the language results. This is just a basic object ({}
) which can contain pre-filled in data, potentially based on the language.
The following keys are restricted and will be overridden by the core: report
, noTranslationFile
and noWhitelistFile
.
const translationsFile = provideTranslationsFile(lang);
Here you should return the translations for the specified language. This must be an object with the message id and message in a key value format.
const translationsFile = {
messageId: 'message',
};
const whitelistFile = provideWhitelistFile(lang);
Here you should return the whitelisted messsage ids for the specified language. This must be an array of strings.
const whitelistFile = [
'messageId',
];
reportLanguage(languageResults)
Here you can handle the reporting of the results for a language, like logging and creating files based on the results.
afterReporting()
Here you can do actions after all reports are made, like cleanup or printing a finished message.
const extractedMessages = readMessageFiles(messagesDirectory);
This is a babel-plugin-react-intl
specific helper method. It will read all extracted JSON file for the specified directory, filter out all files without any messages, and output an array with all messages.
Example output:
const extractedMessages = [
{
path: 'src/components/Foo.json',
descriptors: [
{
id: 'foo_ok',
description: 'Ok text',
defaultMessage: 'OK',
},
],
},
];
createSingleMessagesFile({ messages, directory });
This helper method will output all messages (potentially read by readMessageFiles
) in a single jsonFile.
defaultMessages.json
) this filename should contain the .json
extension2
) number of spaces used for indentation (0-10)const messages = getDefaultMessages(extractedMessages);
This helper method will flatten all files (as returned from readMessageFiles
) into a single object.
const messages = {
messages: {
messageId: 'message',
},
duplicateIds: [
// potentially double used message keys,
]
};
FAQs
Manage all translations based on the extracted messages of the babel-plugin-react-intl
The npm package react-intl-translations-manager receives a total of 16,860 weekly downloads. As such, react-intl-translations-manager popularity was classified as popular.
We found that react-intl-translations-manager demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Research
Security News
Socket’s threat research team has detected six malicious npm packages typosquatting popular libraries to insert SSH backdoors.
Security News
MITRE's 2024 CWE Top 25 highlights critical software vulnerabilities like XSS, SQL Injection, and CSRF, reflecting shifts due to a refined ranking methodology.
Security News
In this segment of the Risky Business podcast, Feross Aboukhadijeh and Patrick Gray discuss the challenges of tracking malware discovered in open source softare.