Installation
Install the plugin using npm:Configuration & Parameters
The plugin requires a configuration object with the following properties. All options are required and there are no default values:Parameter | Type | Description |
---|---|---|
sourceLanguage | string | The IETF language code of the source text (e.g., ‘en’ for English) |
targetLanguage | string | The IETF language code for the target language (e.g., ‘es’ for Spanish). This is also used to name the new field |
sheetSlug | string | The slug of the sheet that the plugin should listen to. Records from other sheets will be ignored |
fieldsToTranslate | string[] | An array of field keys (names) that should be translated. The plugin will only process these fields |
projectId | string | Your Google Cloud project ID associated with the Google Translate API |
keyFilename | string | The absolute or relative path to your Google Cloud service account key file (JSON) |
Default Behavior
By default, the plugin does not do anything until it is configured and registered with a Flatfile listener. Once configured, it will listen for record processing events on the specifiedsheetSlug
. For each record, it will read the text from the fieldsToTranslate
, call the Google Translate API, and write the results to new fields. If a field to be translated is empty or null, it is skipped.
Usage Examples
Basic Usage
Multiple Language Translation
To translate to multiple languages, use the plugin multiple times with different configurations:Manual Translation with translateRecord
API Reference
convertTranslatePlugin
The main entry point for the plugin. This function registers arecordHook
with the provided Flatfile listener, which will automatically translate specified fields on records belonging to the configured sheet.
Signature:
listener
: FlatfileListener - An instance of a Flatfile listener to attach the hook toconfig
: TranslationConfig - The configuration object for the plugin
void
- This function does not return a value. It modifies the listener instance directly.
translateRecord
A standalone function that applies the translation logic to a singleFlatfileRecord
. It reads values from the fields specified in the config, translates them, and sets the translated values on new fields in the record.
Signature:
record
: FlatfileRecord - The record to processconfig
: TranslationConfig - A configuration object specifying languages, fields, and credentials
record
: FlatfileRecord - The record with new fields containing translated texterror?
: string - An error message if an issue occurred during processing
Troubleshooting
Error Handling
The plugin includes comprehensive error handling:- API Errors: If the Google Translate API call fails, the error is logged to the console and the record won’t be updated with translated values
- Record-level Errors: If an error occurs during processing of a single record, the plugin will attach a general error message to that specific record using
record.addError()
, making it visible in the Flatfile UI - No Text to Translate: If a record has no text in any of the
fieldsToTranslate
, it is skipped without error
Notes
Requirements
- Google Cloud Account: A Google Cloud project with the Translate API enabled is required
- Credentials: You must provide a valid
projectId
and a path to a service accountkeyFilename
with appropriate permissions
Field Naming Convention
The plugin creates new fields for translated content by appending_{targetLanguage}
to the original field name (e.g., name
becomes name_es
). Ensure this does not conflict with existing fields in your sheet configuration.
Limitations
- Field Types: The plugin is designed to work with
string
fields. Behavior with other field types is undefined - Initialization: The Google Translate client is initialized globally within the plugin’s scope when
convertTranslatePlugin
is first called. Subsequent calls with different credentials will re-initialize the client