fallback language for captions

This commit is contained in:
Toni Förster 2024-05-20 14:40:25 +02:00
parent c9125644ed
commit 4fa5a15ad4
No known key found for this signature in database
GPG Key ID: 292F3E5086C83FC7
3 changed files with 65 additions and 10 deletions

View File

@ -219,10 +219,17 @@ final class MPVBackend: PlayerBackend {
var captions: Captions? var captions: Captions?
if Defaults[.captionsAutoShow] == true { if Defaults[.captionsAutoShow] == true {
let captionsLanguageCode = Defaults[.captionsDefaultLanguageCode] let captionsDefaultLanguageCode = Defaults[.captionsDefaultLanguageCode],
if !captionsLanguageCode.isEmpty { captionsFallbackLanguageCode = Defaults[.captionsFallbackLanguageCode]
captions = video.captions.first { $0.code == captionsLanguageCode } ??
video.captions.first { $0.code.contains(captionsLanguageCode) } // Try to get captions with the default language code first
captions = video.captions.first { $0.code == captionsDefaultLanguageCode } ??
video.captions.first { $0.code.contains(captionsDefaultLanguageCode) }
// If there are still no captions, try to get captions with the fallback language code
if captions.isNil && !captionsFallbackLanguageCode.isEmpty {
captions = video.captions.first { $0.code == captionsFallbackLanguageCode } ??
video.captions.first { $0.code.contains(captionsFallbackLanguageCode) }
} }
} else { } else {
captions = nil captions = nil

View File

@ -304,6 +304,7 @@ extension Defaults.Keys {
static let captionsAutoShow = Key<Bool>("captionsAutoShow", default: false) static let captionsAutoShow = Key<Bool>("captionsAutoShow", default: false)
static let captionsLanguageCode = Key<String?>("captionsLanguageCode") static let captionsLanguageCode = Key<String?>("captionsLanguageCode")
static let captionsDefaultLanguageCode = Key<String>("captionsDefaultLanguageCode", default: LanguageCodes.English.rawValue) static let captionsDefaultLanguageCode = Key<String>("captionsDefaultLanguageCode", default: LanguageCodes.English.rawValue)
static let captionsFallbackLanguageCode = Key<String>("captionsDefaultFallbackCode", default: LanguageCodes.English.rawValue)
static let lastUsedPlaylistID = Key<Playlist.ID?>("lastPlaylistID") static let lastUsedPlaylistID = Key<Playlist.ID?>("lastPlaylistID")
static let lastAccountIsPublic = Key<Bool>("lastAccountIsPublic", default: false) static let lastAccountIsPublic = Key<Bool>("lastAccountIsPublic", default: false)

View File

@ -40,6 +40,7 @@ struct PlayerSettings: View {
@Default(.captionsAutoShow) private var captionsAutoShow @Default(.captionsAutoShow) private var captionsAutoShow
@Default(.captionsDefaultLanguageCode) private var captionsDefaultLanguageCode @Default(.captionsDefaultLanguageCode) private var captionsDefaultLanguageCode
@Default(.captionsFallbackLanguageCode) private var captionsFallbackLanguageCode
@ObservedObject private var accounts = AccountsModel.shared @ObservedObject private var accounts = AccountsModel.shared
@ -50,7 +51,8 @@ struct PlayerSettings: View {
#endif #endif
#if os(tvOS) #if os(tvOS)
@State private var isShowingLanguagePicker = false @State private var isShowingDefaultLanguagePicker = false
@State private var isShowingFallbackLanguagePicker = false
#endif #endif
var body: some View { var body: some View {
@ -107,18 +109,33 @@ struct PlayerSettings: View {
showCaptionsAutoShowToggle showCaptionsAutoShowToggle
#if !os(tvOS) #if !os(tvOS)
captionDefaultLanguagePicker captionDefaultLanguagePicker
captionFallbackLanguagePicker
#else #else
Button(action: { isShowingLanguagePicker = true }) { Button(action: { isShowingDefaultLanguagePicker = true }) {
HStack { HStack {
Text("Default language") Text("Default language")
Spacer() Spacer()
Text("\(LanguageCodes(rawValue: captionsDefaultLanguageCode)!.description.capitalized) (\(captionsDefaultLanguageCode))").foregroundColor(.secondary) Text("\(LanguageCodes(rawValue: captionsDefaultLanguageCode)!.description.capitalized) (\(captionsDefaultLanguageCode))").foregroundColor(.secondary)
} }
} }
.frame(maxWidth: .infinity).sheet(isPresented: $isShowingLanguagePicker) { .frame(maxWidth: .infinity).sheet(isPresented: $isShowingDefaultLanguagePicker) {
LanguagePickerTVOS( defaultLanguagePickerTVOS(
selectedLanguage: $captionsDefaultLanguageCode, selectedLanguage: $captionsDefaultLanguageCode,
isShowing: $isShowingLanguagePicker isShowing: $isShowingDefaultLanguagePicker
)
}
Button(action: { isShowingFallbackLanguagePicker = true }) {
HStack {
Text("Fallback language")
Spacer()
Text("\(LanguageCodes(rawValue: captionsFallbackLanguageCode)!.description.capitalized) (\(captionsFallbackLanguageCode))").foregroundColor(.secondary)
}
}
.frame(maxWidth: .infinity).sheet(isPresented: $isShowingDefaultLanguagePicker) {
fallbackLanguagePickerTVOS(
selectedLanguage: $captionsFallbackLanguageCode,
isShowing: $isShowingFallbackLanguagePicker
) )
} }
#endif #endif
@ -325,8 +342,19 @@ struct PlayerSettings: View {
.labelsHidden() .labelsHidden()
#endif #endif
} }
private var captionFallbackLanguagePicker: some View {
Picker("Fallback language", selection: $captionsFallbackLanguageCode) {
ForEach(LanguageCodes.allCases, id: \.self) { language in
Text("\(language.description.capitalized) (\(language.rawValue))").tag(language.rawValue)
}
}
#if os(macOS)
.labelsHidden()
#endif
}
#else #else
struct LanguagePickerTVOS: View { struct defaultLanguagePickerTVOS: View {
@Binding var selectedLanguage: String @Binding var selectedLanguage: String
@Binding var isShowing: Bool @Binding var isShowing: Bool
@ -344,6 +372,25 @@ struct PlayerSettings: View {
} }
} }
} }
struct fallbackLanguagePickerTVOS: View {
@Binding var selectedLanguage: String
@Binding var isShowing: Bool
var body: some View {
NavigationView {
List(LanguageCodes.allCases, id: \.self) { language in
Button(action: {
selectedLanguage = language.rawValue
isShowing = false
}) {
Text("\(language.description.capitalized) (\(language.rawValue))")
}
}
.navigationTitle("Select Fallback Language")
}
}
}
#endif #endif
#if !os(tvOS) #if !os(tvOS)