feat(translate): keep translated subtitles next to the video so they load on reopen (#14)

Co-authored-by: Tri Vo Khac <git@trivk.dev>
This commit was merged in pull request #14.
This commit is contained in:
tri authored and tri committed 2026-11-13 22:49:56 +07:00
1 parent b34a01a92f
commit 76b12d6632
13 files changed
+716 -65

No files matched your search

+10 -9
View File
@@ -24,7 +24,6 @@ ApplicationWindow {
// subtitle even while that translated track is selected in mpv.
property var lastTranslationTrack: null
property url lastTranslationVideo: ""
property var translatedSubtitleUrls: []
function openFile(url: url) {
playlistModel.openFile(url, QtPotatoSettings.playlistLoadSiblings)
@@ -46,10 +45,15 @@ ApplicationWindow {
}
}
function translatedTrackTitle(): string {
return KI18n.i18nc("@item:inlistbox subtitle track, %1 is a language", "%1 (translated)", QtPotatoSettings.targetLanguage)
}
function translateCurrentSubtitle() {
let track = video.subtitleTracks.find(track => track.id === video.subtitleId)
if (track && translatedSubtitleUrls.includes(track.externalFilename) && lastTranslationTrack) {
track = video.subtitleTracks.find(track => track.id === lastTranslationTrack.id)
// Translate the original again, not the translation on screen.
if (track && video.isTranslatedSubtitle(track.id)) {
track = lastTranslationTrack ? video.subtitleTracks.find(track => track.id === lastTranslationTrack.id) : null
}
// No subtitle shown, or a picture-based one: translate the best text subtitle.
if (!track || track.bitmap) {
@@ -141,7 +145,8 @@ ApplicationWindow {
PlaylistModel {
id: playlistModel
onPlayRequested: url => {
video.loadFile(url)
// A translation made earlier is loaded and turned on with the file.
video.loadFile(url, translationService.savedTranslation(url), appWindow.translatedTrackTitle())
RecentFilesModel.add(url)
}
}
@@ -169,10 +174,7 @@ ApplicationWindow {
TranslationService {
id: translationService
onTranslated: (subtitleUrl, fromCache) => {
// mpv exposes local external subtitle filenames as filesystem paths.
appWindow.translatedSubtitleUrls.push(decodeURIComponent(subtitleUrl.toString().replace(/^file:\/\//, "")))
video.showTranslatedSubtitleFile(subtitleUrl,
KI18n.i18nc("@item:inlistbox subtitle track, %1 is a language", "%1 (translated)", QtPotatoSettings.targetLanguage))
video.showTranslatedSubtitleFile(subtitleUrl, appWindow.translatedTrackTitle())
osdItem.message(fromCache
? KI18n.i18nc("@info:osd", "Loaded the saved translation")
: KI18n.i18nc("@info:osd", "Translation ready"))
@@ -187,7 +189,6 @@ ApplicationWindow {
translationService.forgetTranslation()
appWindow.lastTranslationTrack = null
appWindow.lastTranslationVideo = ""
appWindow.translatedSubtitleUrls = []
glossaryDialog.close()
}