-
Notifications
You must be signed in to change notification settings - Fork 881
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: ai tranlation for entry content
- Loading branch information
Showing
3 changed files
with
80 additions
and
58 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
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 |
---|---|---|
@@ -1,28 +1,78 @@ | ||
import { apiClient } from "@renderer/lib/api-fetch" | ||
import { views } from "@renderer/lib/constants" | ||
import { defineQuery } from "@renderer/lib/defineQuery" | ||
import type { SupportedLanguages } from "@renderer/models" | ||
import type { EntryModel, SupportedLanguages } from "@renderer/models" | ||
import { franc } from "franc-min" | ||
|
||
const LanguageMap: Record< | ||
SupportedLanguages, | ||
{ | ||
code: string | ||
} | ||
> = { | ||
"en": { | ||
code: "eng", | ||
}, | ||
"ja": { | ||
code: "jpn", | ||
}, | ||
"zh-CN": { | ||
code: "cmn", | ||
}, | ||
"zh-TW": { | ||
code: "cmn", | ||
}, | ||
} | ||
|
||
export const ai = { | ||
translation: ({ | ||
id, | ||
entry, | ||
view, | ||
language, | ||
fields, | ||
extraFields, | ||
}: { | ||
id: string | ||
entry: EntryModel | ||
view?: number | ||
language?: SupportedLanguages | ||
fields: string | ||
extraFields?: string[] | ||
}) => | ||
defineQuery(["translation", id, language], async () => { | ||
defineQuery(["translation", entry.entries.id, language], async () => { | ||
if (!language) { | ||
return null | ||
} | ||
const res = await apiClient.ai.translation.$get({ | ||
query: { | ||
id, | ||
language: language as SupportedLanguages, | ||
fields, | ||
}, | ||
let fields = | ||
entry.settings?.translation && view !== undefined ? | ||
views[view!].translation.split(",") : | ||
[] | ||
if (extraFields) { | ||
fields = [...fields, ...extraFields] | ||
} | ||
|
||
fields = fields.filter((field) => { | ||
if (entry.settings?.translation && entry.entries[field]) { | ||
const sourceLanguage = franc(entry.entries[field]) | ||
|
||
if (sourceLanguage === LanguageMap[entry.settings?.translation].code) { | ||
return false | ||
} else { | ||
return true | ||
} | ||
} else { | ||
return false | ||
} | ||
}) | ||
return res.data | ||
|
||
if (fields.length > 0) { | ||
const res = await apiClient.ai.translation.$get({ | ||
query: { | ||
id: entry.entries.id, | ||
language: language as SupportedLanguages, | ||
fields: fields?.join(",") || "title", | ||
}, | ||
}) | ||
return res.data | ||
} else { | ||
return null | ||
} | ||
}), | ||
} |