An npm package that automates the process of updating translation files on every git commit using OpenAI.
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.
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",
...
},
...
},
...
}
npm install translation-hook-assistant
-
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.
- 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
- 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.
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. |
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. |
- Make sure your OpenAI API key remains confidential. Never hard-code it directly in your scripts.
Contributions, issues, and feature requests are welcome. For major changes, please open an issue first to discuss what you'd like to change.
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.