From d0b43f058f7a3d5db7ff83d0ed5c2a8f3793cdf1 Mon Sep 17 00:00:00 2001 From: Martin Donadieu Date: Wed, 16 Oct 2024 14:08:44 +0900 Subject: [PATCH] fix: better doc and auto choose api --- README.md | 2 +- scripts/translate.tsx | 7 ++++++- 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index eeb38a26..6083abd7 100644 --- a/README.md +++ b/README.md @@ -78,7 +78,7 @@ All commands are run from the root of the project, from a terminal: The website aims at having an automatic i18n done via various scripts in the `scripts` directory. -The [translations.tsx](./scripts/translations.tsx) script is used to translate the website content into the desired language. It has two methods to create translations, via `OpenAI API`, and the other via `api.datpmt.com`. To use the OpenAI API method, make sure you have an `OPENAI_API_KEY` as the environment variable set. To use the other API, just un-comment the `translateText` function call using it. +The [translations.tsx](./scripts/translations.tsx) script is used to translate the website content into the desired language. It has two methods to create translations, via `OpenAI API` or `Anthropic API`, and the other via `api.datpmt.com`. To use the OpenAI API method, make sure you have an `OPENAI_API_KEY` or `ANTHROPIC_API_KEY` as the environment variable set. To use the other API, just un-comment the `translateText` function call using it. Now, let's say that you want to update translations or add a new locale, `fr`. diff --git a/scripts/translate.tsx b/scripts/translate.tsx index 9e3a3dee..b0c8e49c 100644 --- a/scripts/translate.tsx +++ b/scripts/translate.tsx @@ -131,5 +131,10 @@ export const translateTextDatpmt = async (text: string, lang: string) => { return await response.json() } -export const translateText = translateTextAnthropic +export const translateText = (text: string, lang: string) => { + if (process.env.ANTHROPIC_API_KEY) return translateTextAnthropic(text, lang) + if (process.env.OPENAI_API_KEY) return translateTextOpenAI(text, lang) + return translateTextDatpmt(text, lang) +} + export const translateTexts = translateTextsAnthropic