forked from cgsvv/AISubtitle
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
cgsv
authored and
cgsv
committed
Mar 8, 2023
1 parent
f4b7742
commit 9f1145a
Showing
6 changed files
with
177 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
import translate from 'google-translate-api-x'; | ||
import { NextFetchEvent, NextRequest, NextResponse } from "next/server"; | ||
import { getLocale } from '@/lib/lang'; | ||
|
||
// to like "en", "zh-CN" | ||
async function trans_texts(texts: string[], to: string) { | ||
const res = await translate(texts, {to: to}); | ||
return res.map((item) => item.text); | ||
} | ||
|
||
export const config = { | ||
runtime: "edge", | ||
} | ||
|
||
export default async function handler( | ||
req: NextRequest, | ||
context: NextFetchEvent | ||
) { | ||
const {sentences, targetLang, srcLang} = (await req.json()) as { | ||
sentences: string[]; | ||
targetLang: string; | ||
srcLang?: string; | ||
} | ||
if (!sentences || sentences.length === 0) { | ||
return new Response("no subtitles", { status: 500 }); | ||
} | ||
|
||
try { | ||
const resp = await trans_texts(sentences, getLocale(targetLang)!); | ||
return NextResponse.json(resp); | ||
} catch (error: any) { | ||
console.log("API error", error, error.message); | ||
return NextResponse.json({ | ||
errorMessage: error.message, | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters