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