global-message-converter v1.7.4
Global Message Converter
global-message-converter
is a command-line tool that detects hardcoded text within React applications and converts it to the format required by global messaging systems (react-intl). This library enables easy conversion of text.
CHANGELOG: Check the latest update history here.
Conversion Examples (Class and Functional Component Support)
- Text Conversion in JSX
Before:
<div>Hello.</div>
After:
<div><FormattedMessage id="greeting" defaultMessage="Hello." /></div>
- Plain Text Conversion
- In this case, if the text is english converts only that starts with uppercase.
Before:
const hi = 'Hello.';
After:
const hi = intl.formatMessage({ id: 'new.message', defaultMessage: 'Hello.' });
- Template Literal Conversion
Before:
const error = `${error} occurred`;
After:
const error = intl.formatMessage({ id: 'error.message' }, { error });
Getting Started
To use this library, Node.js must be installed. Then you can install the library using npm.
npm install -g global-message-converter
Usage
After installation, run the library using the following command:
gmc [command]
Commands
convert
Detects hardcoded text and converts it to a format readable by the global messaging system.
Usage Example
gmc convert -t <file or folder path> -m <message file path>
Options
-t, --target <target>
: Enter the file or folder to be converted.-m, --message <message>
: Enter the message file path.
Example
To convert files containing hardcoded text:
gmc convert --target ./src/components --message ./messages/messages.json
Message File Format
The message file is the baseline used to convert hardcoded text.
It detects message keys matching the text. If no matching message is found, a new message is created, and newly created messages are provided in file format (newMessages).
Must be written in JSON or JS format, with each message having a unique key and corresponding text value. Here's an example of a message file:
{
"welcome_message": "Welcome, {username}!",
"loading": "Loading...",
"error": "Error: Unable to load data.",
"success": "Congratulations! All tasks completed."
}
For JS files, nested message structures are not recognized:
export default {
ko: {
"welcome_message": "Welcome, {username}!",
"loading": "Loading...",
"error": "Error: Unable to load data.",
"success": "Congratulations! All tasks completed."
},
en: {
...
}
}
Must be converted to a non-nested format for proper message conversion:
export default {
"welcome_message": "Welcome, {username}!",
"loading": "Loading...",
"error": "Error: Unable to load data.",
"success": "Congratulations! All tasks completed."
}
How to Contribute
- Fork the repository.
- Create a branch: git checkout -b feature/feature-name
- Commit and push your changes.
- Bug Reports: If a bug is discovered, create a new issue in the Issues section. Clearly describe the problem and include reproduction steps.
- Feature Requests: New feature suggestions are always welcome! Provide detailed explanations to aid review.
License
This project is distributed under the MIT License.