Translation Hook Assistant
An npm package that automates the process of updating translation files on every git commit using OpenAI.
Overview
When working on projects that support multiple languages, keeping track of updated translations can be a challenge. The Translation Hook Assistant
helps you automate this process by leveraging the power of OpenAI's language model, integrating directly into your Git workflow. Every time you make a commit, the updateTranslationFileOnCommit
function ensures that your translation files are up-to-date, making internationalization seamless and efficient.
Enhancing Translations with Context
To achieve more accurate and contextually appropriate translations from OpenAI, you can optionally include appContext
and context
keys in your translation files. The appContext
key can provide general information or context about the application, while the context
key can provide context about a specific section or key within the translation file. This additional context can help OpenAI better understand the intended meaning and usage, leading to improved translation quality.
{
"appContext": "A finance management app",
"screen": {
"dashboard": {
"context": "Main dashboard displaying financial overview",
"balance": "Balance",
...
},
...
},
...
}
Installation
npm install translation-hook-assistant
Prerequisites
-
Obtain an OpenAI API key. If you don’t have one, sign up here.
-
Set up a default translation language and any number of other languages you're targeting for your project. Make sure to have these files already created before setting up the
updateTranslationFileOnCommit
function.
Usage
Update Translation Files
- Set up the function to manually update your translation files.
import { updateTranslationFiles } from 'translation-hook-assistant';
updateTranslationFiles({
openAiApiKey: 'YOUR_OPENAI_API_KEY',
defaultLanguage: 'en',
otherLanguages: ['es', 'fr', 'de'],
});
- Run the script whenever you need to update your translation files manually.
npm run updateTranslationFiles
Update Translation Files On Commit
- Set up the function in a pre-commit script, ensuring you pass the required parameters.
import { updateTranslationFilesOnCommit } from 'translation-hook-assistant';
updateTranslationFilesOnCommit({
openAiApiKey: 'YOUR_OPENAI_API_KEY',
defaultLanguage: 'en',
otherLanguages: ['es', 'fr', 'de'],
});
- Create a script that runs the pre-commit script.
{
"scripts": {
"updateTranslationOnCommit": "ts-node path/to/pre-commit/script.ts"
}
}
You can use a package like husky
to easily set up commit hooks.
- Add the script to your pre-commit hook.
npm run updateTranslationOnCommit
- Whenever you make changes to your translation files and commit them, the
updateTranslationFilesOnCommit
function will automatically detect translation changes or new keys created and update translations for other languages as needed.
Configuration
Update Translation Files
Parameter | Description |
---|---|
openAiApiKey |
Your OpenAI API key. |
defaultLanguage |
The default language of your project (e.g., 'en'). |
otherLanguages |
An array of other languages you want translations for. |
Update Translation Files On Commit
Parameter | Description |
---|---|
openAiApiKey |
Your OpenAI API key. |
defaultLanguage |
The default language of your project (e.g., 'en'). |
otherLanguages |
An array of other languages you want translations for. |
Limitations
- Make sure your OpenAI API key remains confidential. Never hard-code it directly in your scripts.
Contributing
Contributions, issues, and feature requests are welcome. For major changes, please open an issue first to discuss what you'd like to change.
License
MIT
By leveraging the power of OpenAI and Git hooks, the Translation Hook Assistant ensures your project's translations remain consistent and updated, helping you cater to a global audience effortlessly.