Command to automatically translate your app JSON files using OpenAI GPT Chat Completions API
npm install gpt-translate-json --save-dev
- Node.js >= 16
- OpenAI Api key
Add the command in package.json
, and provide required parameters:
"scripts": {
"gpt-translate-json": "gpt-translate-json --apiKey=openai_api_key --model=gpt-3.5-turbo --maxTokens=3000 --langs=en-US,it-IT --originalLang=en-US"
}
Available options:
apiKey
OpenAI API key. Requiredmodel
OpenAI Chat Completion model. RequiredmaxTokens
OpenAI model max tokens per request. Requiredlangs
All supported languages. RequiredoriginalLang
Original language. RequiredbasePath
The base path. Default to'./'
assetsPath
Path to translation files:[basePath]/[assetsPath]/[lang]/*.json
. Default to'i18n'
rules
Prompt rules. Defaults:'do not translate proper names'
'do not translate texts enclosed in double braces {{}}'
'do not translate the html tags''
'do not translate URLs'
Depending on the model used, requests can use up to
maxTokens
shared between prompt and completion. Keep the number ofmaxTokens
lower than the maximum allowed by the model: in fact, the command splits the files into multiple requests to respect the maximum number of tokens in each request based on the English language, but depending on the target language, the number of tokens used can vary significantly
Having a file structure like this:
i18n/
│
└───en-US/
app.json
i18n/en-US/app.json
{
"app": {
"title": "<h1>Library to translate JSON using GPT</h1>"
}
}
the command:
npm run gpt-translate-json
will generate:
i18n/
│
└───en-US/
│ app.json
└───it-IT/
│ app.json
└───.metadata/
translated.json
translated-langs.json
i18n/it-IT/app.json
{
"app": {
"title": "<h1>Libreria per tradurre JSON usando GPT</h1>>"
}
}
The file .metadata/translated.json
contains the paths of translated values, so if you add new translations:
i18n/en-US/app.json
{
"app": {
"title": "<h1>Library to translate JSON using GPT</h1>"
},
"about": "About us"
}
the command will request only the new translations, reducing tokens usage, and the files will be updated:
i18n/it-IT/app.json
{
"app": {
"title": "<h1>Libreria per tradurre JSON usando GPT</h1>>"
},
"about": "Chi siamo"
}
The file .metadata/translated-langs.json
contains the langs already translated, so if you add a new lang:
"scripts": {
"gpt-translate-json": "gpt-translate-json --apiKey=openai_api_key --model=gpt-3.5-turbo --maxTokens=3000 --langs=en-US,it-IT,es-ES --originalLang=en-US"
}
will generate:
i18n/
│
└───en-US/
│ app.json
└───it-IT/
│ app.json
└───es-ES/
│ app.json
└───.metadata/
translated.json
translated-langs.json
i18n/es-ES/app.json
{
"app": {
"title": "<h1>Biblioteca para traducir JSON usando GPT</h1>"
},
"about": "Sobre nosotros"
}
Rather than using the command, you can invoke gptTranslateJson
function:
import { gptTranslateJson } from 'gpt-translate-json';
await gptTranslateJson({
apiKey: 'openai_api_key',
model: 'gpt-3.5-turbo',
maxTokens: 3000,
langs: ['en-US', 'it-IT'],
originalLang: 'en-US',
rules: [
// your custom rules
]
});
npm install
npm run build
npm test
MIT