Skip to content

Instantly share code, notes, and snippets.

@talhatahir
Created January 27, 2024 14:58
Show Gist options
  • Save talhatahir/8fcc591f5a0ae4c0c418bba87c09813f to your computer and use it in GitHub Desktop.
Save talhatahir/8fcc591f5a0ae4c0c418bba87c09813f to your computer and use it in GitHub Desktop.
Auto Translate your App using ChatGPT OpenAI and i18n
require('dotenv').config()
const fs = require('fs')
const OpenAI = require('openai')
//make sure you have the OPENAI_API_KEY in your .env file
const OPENAI_API_KEY = process.env.OPENAI_API_KEY
const openai = new OpenAI({
apiKey: OPENAI_API_KEY,
})
async function updateLanguageFile(english, fileName) {
//get the .json file for the language using fileName
let langJson = require(`./lang/${fileName}.json`)
console.warn(`${fileName}.json:`)
//get all empty keys for a language which need to be translated
const emptyKeys = Object.keys(langJson).filter((key) => langJson[key] === '')
if (emptyKeys.length > 0) {
console.warn(`Translating using ChatGPT 3.5 Turbo...\n`)
const englishEntries = {}
//fetch the english sentences for all the missing empty keys
//which would be sent to ChatGPT OpenAI api
emptyKeys.forEach((ele) => {
englishEntries[ele] = english[ele]
})
try {
//Calls ChatGPT OpenAI api to translate the strings
const translatedEntries = await completeTranslation(englishEntries, fileName)
console.log(`en => ${JSON.stringify(englishEntries)}`)
console.log(`${fileName} => ${translatedEntries}`)
//append the freshly translated strings to the original language file
langJson = { ...langJson, ...JSON.parse(translatedEntries) }
} catch (translationError) {
console.error(`Error translating: ${translationError.message}`)
}
//sort the translations according to key name,
//so that order is preserved in the language file
const sortedLangJson = Object.keys(langJson)
.sort()
.reduce((acc, key) => {
acc[key] = langJson[key]
return acc
}, {})
//write it to the language file
fs.writeFileSync(`./lang/${fileName}.json`, JSON.stringify(sortedLangJson, null, 2))
} else {
console.warn(`No translations missing`)
}
console.warn(`\n`)
}
const completeTranslation = async (messageObject, lang) => {
try {
/*This functions sends all the entries of a single language as a json object
to ChatGPT OpenAI api and it returns you the translated object in json format.
To save cost, I send the whole object in one request rather than
sending each string individually which would cost more tokens.*/
const completion = await openai.chat.completions.create({
model: 'gpt-3.5-turbo', //You can use other models too
messages: [
//These are the prompts which I use for translations
{
role: 'system',
content:
'You will be provided with a json object in English language
and your task is to only translate the values into the
requested language, and return the result as valid json',
},
{
role: 'user',
content: `Translate the following json Object
to '${lang}' language: ${JSON.stringify(messageObject)}`,
},
],
//better to not mess with these,
//you can tweak token size if you want to send huge translation objects
temperature: 1,
max_tokens: 256,
top_p: 1,
frequency_penalty: 0,
presence_penalty: 0,
})
return completion.choices[0].message.content
} catch (error) {
throw new Error(`Error completing translation: ${error.message}`)
}
}
try {
console.info(`Checking for missing translations\n`)
const english = require('./lang/en.json') //path to your english .json file
const languages = ['de', 'es', 'fr'] //names of you other language files.
languages.forEach((lang) => {
updateLanguageFile(english, lang)
})
} catch (error) {
console.error('Error reading JSON files:', error.message)
}
@talhatahir
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment