@saykit/babel-plugin automatically extracts and transforms internationalized messages from your code at build time.
Installation
Configuration
Using .babelrc
Add the plugin to your.babelrc file:
.babelrc
Using babel.config.js
babel.config.js
Using babel.config.json
babel.config.json
What It Transforms
The Babel plugin transforms two types of expressions:- JSX Elements:
<Say>components and their variants (Say.Plural,Say.Ordinal,Say.Select) - Template Literals: Tagged template literals using
say.tort
Supported File Extensions
The plugin processes files with the following extensions:.js,.cjs,.mjs.jsx.ts,.cts,.mts.tsx
Transformation Examples
JSX Element Transformation
- Extracts the message content
- Generates a unique hash ID (
a8f3c2d1) based on the message - Converts child elements into props
- Replaces the original JSX with a descriptor
Pluralization Transformation
Ordinal Transformation
Select Transformation
Template Literal Transformation
How It Works
Parsing
The plugin parses your source code using Babel’s parser with TypeScript and JSX support enabled.
Traversal
It traverses the Abstract Syntax Tree (AST) looking for:
- JSX elements named
Say,Say.Plural,Say.Ordinal, orSay.Select - Tagged template literals
Message Extraction
For each match, it:
- Extracts the message content and structure
- Identifies variables and components used within the message
- Generates a unique hash ID based on the message content
Message Hash Generation
The plugin generates stable, deterministic hash IDs for messages using SHA-256:- Same message content → Same hash ID
- Different content → Different hash ID
- Ensures consistency across builds
- Allows safe refactoring without breaking translations
Programmatic Usage
You can use the plugin programmatically for advanced use cases:transformCode
Transforms source code and returns the modified code:File path or identifier (used for extension checking)
Source code to transform
collectMessages
Extracts messages without transforming the code:File path or identifier
Source code to analyze
Integration Examples
Next.js with Babel
Next.js uses Babel by default. Simply add the plugin to.babelrc:
.babelrc
Next.js 13+ uses SWC by default, which doesn’t support Babel plugins. To use this plugin with Next.js 13+, you need to disable SWC or use the unplugin instead.
Create React App
CRA doesn’t expose Babel configuration by default. You’ll need to either:- Eject the configuration
- Use a tool like
react-app-rewired - Use the unplugin with a custom webpack config
Gatsby
Add to yourgatsby-config.js:
gatsby-config.js
Standalone Babel
Use with the Babel CLI:Limitations
Troubleshooting
Plugin not running
Ensure your build tool is using Babel. Some tools (like Vite) don’t use Babel by default.Transformations not applied
Check that:- The plugin is in your Babel configuration
- Your file extension is supported
- You’re importing
Sayfrom@saykit/react
Hash IDs changing unexpectedly
Hash IDs are based on message content. Even small changes (whitespace, formatting) will generate new IDs.See Also
- React Integration - Learn about the Say component
- Unplugin - Alternative for non-Babel build tools
- Message Extraction - Understanding the extraction process