Skip to content

Commit 17b25bc

Browse files
committed
use openai only when key is set
1 parent bd67b26 commit 17b25bc

File tree

1 file changed

+10
-1
lines changed

1 file changed

+10
-1
lines changed

translate.js

+10-1
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,19 @@
11
import OpenAI from "openai";
22
import { fileURLToPath } from 'url';
33

4-
const openai = new OpenAI();
4+
let openai = null;
5+
6+
if (!process.env.OPENAI_API_KEY) {
7+
console.warn("OPENAI_API_KEY not set in environment variables");
8+
} else {
9+
openai = new OpenAI({apiKey: process.env.OPENAI_API_KEY});
10+
}
511

612
export async function translateStrings(textArray, language) {
713
try {
14+
if (!openai) {
15+
return textArray.map(text => `Server config: translation API key not set`);
16+
}
817
const json = JSON.stringify(textArray);
918
if (language.toLowerCase() === "english")
1019
{

0 commit comments

Comments
 (0)