[feat] add translate replace regex
This commit is contained in:
@@ -483,4 +483,5 @@ export const languageEnglish = {
|
|||||||
template: "Template",
|
template: "Template",
|
||||||
chatAsOriginalOnSystem: "Send as original role",
|
chatAsOriginalOnSystem: "Send as original role",
|
||||||
exportAsDataset: "Export Save as Dataset",
|
exportAsDataset: "Export Save as Dataset",
|
||||||
|
editTranslationDisplay: "Edit Translation Display",
|
||||||
}
|
}
|
||||||
@@ -92,7 +92,7 @@
|
|||||||
if(translateText){
|
if(translateText){
|
||||||
const marked = await ParseMarkdown(data, charArg, mode, chatID)
|
const marked = await ParseMarkdown(data, charArg, mode, chatID)
|
||||||
translating = true
|
translating = true
|
||||||
const translated = await translateHTML(marked, false)
|
const translated = await translateHTML(marked, false, charArg)
|
||||||
translating = false
|
translating = false
|
||||||
lastParsed = translated
|
lastParsed = translated
|
||||||
lastCharArg = charArg
|
lastCharArg = charArg
|
||||||
|
|||||||
@@ -52,6 +52,7 @@
|
|||||||
<OptionInput value="editoutput">{language.editOutput}</OptionInput>
|
<OptionInput value="editoutput">{language.editOutput}</OptionInput>
|
||||||
<OptionInput value="editprocess">{language.editProcess}</OptionInput>
|
<OptionInput value="editprocess">{language.editProcess}</OptionInput>
|
||||||
<OptionInput value="editdisplay">{language.editDisplay}</OptionInput>
|
<OptionInput value="editdisplay">{language.editDisplay}</OptionInput>
|
||||||
|
<OptionInput value="edittrans">{language.editTranslationDisplay}</OptionInput>
|
||||||
</SelectInput>
|
</SelectInput>
|
||||||
<span class="text-textcolor mt-6">IN:</span>
|
<span class="text-textcolor mt-6">IN:</span>
|
||||||
<TextInput size="sm" bind:value={value.in} />
|
<TextInput size="sm" bind:value={value.in} />
|
||||||
|
|||||||
@@ -1,10 +1,12 @@
|
|||||||
import { get } from "svelte/store"
|
import { get } from "svelte/store"
|
||||||
import { translatorPlugin } from "../plugins/plugins"
|
import { translatorPlugin } from "../plugins/plugins"
|
||||||
import { DataBase } from "../storage/database"
|
import { DataBase, type customscript } from "../storage/database"
|
||||||
import { globalFetch } from "../storage/globalApi"
|
import { globalFetch } from "../storage/globalApi"
|
||||||
import { alertError } from "../alert"
|
import { alertError } from "../alert"
|
||||||
import { requestChatData } from "../process/request"
|
import { requestChatData } from "../process/request"
|
||||||
import { doingChat } from "../process"
|
import { doingChat } from "../process"
|
||||||
|
import type { simpleCharacterArgument } from "../parser"
|
||||||
|
import { selectedCharID } from "../stores"
|
||||||
|
|
||||||
let cache={
|
let cache={
|
||||||
origin: [''],
|
origin: [''],
|
||||||
@@ -171,7 +173,7 @@ export function isExpTranslator(){
|
|||||||
return db.translatorType === 'llm' || db.translatorType === 'deepl'
|
return db.translatorType === 'llm' || db.translatorType === 'deepl'
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function translateHTML(html: string, reverse:boolean): Promise<string> {
|
export async function translateHTML(html: string, reverse:boolean, charArg:simpleCharacterArgument|string = ''): Promise<string> {
|
||||||
let db = get(DataBase)
|
let db = get(DataBase)
|
||||||
let DoingChat = get(doingChat)
|
let DoingChat = get(doingChat)
|
||||||
if(DoingChat){
|
if(DoingChat){
|
||||||
@@ -233,6 +235,28 @@ export async function translateHTML(html: string, reverse:boolean): Promise<stri
|
|||||||
// Remove the outer <body> tags
|
// Remove the outer <body> tags
|
||||||
translatedHTML = translatedHTML.replace(/^<body[^>]*>|<\/body>$/g, '');
|
translatedHTML = translatedHTML.replace(/^<body[^>]*>|<\/body>$/g, '');
|
||||||
|
|
||||||
|
if(charArg !== ''){
|
||||||
|
let scripts:customscript[] = []
|
||||||
|
if(typeof(charArg) === 'string'){
|
||||||
|
const db = get(DataBase)
|
||||||
|
const charId = get(selectedCharID)
|
||||||
|
const char = db.characters[charId]
|
||||||
|
scripts = (db.globalscript ?? []).concat(char.customscript)
|
||||||
|
}
|
||||||
|
else{
|
||||||
|
scripts = (db.globalscript ?? []).concat(charArg.customscript)
|
||||||
|
|
||||||
|
}
|
||||||
|
for(const script of scripts){
|
||||||
|
if(script.type === 'edittrans'){
|
||||||
|
const reg = new RegExp(script.in, script.ableFlag ? script.flag : 'g')
|
||||||
|
let outScript = script.out.replaceAll("$n", "\n")
|
||||||
|
translatedHTML = translatedHTML.replace(reg, outScript)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
// console.log(html)
|
// console.log(html)
|
||||||
// console.log(translatedHTML)
|
// console.log(translatedHTML)
|
||||||
// Return the translated HTML, excluding the outer <body> tags if needed
|
// Return the translated HTML, excluding the outer <body> tags if needed
|
||||||
|
|||||||
Reference in New Issue
Block a user