Skip to content

Liquid Glass and Apple Music touches for the Spotify iOS app. A GPL-3.0 fork of spoti.pw 0.21.1.

C

fix: the translate menu tells songs in another language by their lines

My-Name-Is-Jeff46cf87ea83cd
diff --git a/docs/tweaks.md b/docs/tweaks.md
index 98216d25..09aa3724 100644
--- a/docs/tweaks.md
+++ b/docs/tweaks.md
@@ -134,7 +134,7 @@ Shared:
Intelligence's model (iOS 26 where it is on and speaks the language; permissive guardrails for changing
the user's own text, 12 lines a session, greedy and capped at 60 tokens a line, exactly one string a line by a generation schema; Translate retried once, as its first ask can fail while it loads), both in
OnDeviceTranslation.swift, the frameworks weak linked; and by Gemini on the user's own key (Keychain),
- whose prompt and menu item say the lyrics go to Google. The translate items show only for a song NaturalLanguage finds in another language than the Lyrics page's (or the iPhone's). Apple Intelligence's lines show and are saved a batch at a time, only lines without a translation are sent, and a batch it turns down is skipped (asking again sends only those lines). Every finished translation is kept by SavedTranslations.m in Caches/Vitrine/Translations, one JSON file a song and language mapping each line's text to its translation, the newest 200, applied when the song's lines are next shown; the Lyrics page's Saved translations row counts them and deletes them. Gemini's reply read and checked on the Mac
+ whose prompt and menu item say the lyrics go to Google. The translate items show only for a song NaturalLanguage finds in another language than the Lyrics page's (or the iPhone's): one distinct line in five it is at least 80% sure of, not the whole song's guess (short lines sway that: Havana reads as Dutch), so songs half in English count and a repeated chorus weighs as one line; Translate on iPhone translates from the language most of those lines are in and leaves lines already in the target as they are. Apple Intelligence's lines show and are saved a batch at a time, only lines without a translation are sent, and a batch it turns down is skipped (asking again sends only those lines). Every finished translation is kept by SavedTranslations.m in Caches/Vitrine/Translations, one JSON file a song and language mapping each line's text to its translation, the newest 200, applied when the song's lines are next shown; the Lyrics page's Saved translations row counts them and deletes them. Gemini's reply read and checked on the Mac
against harness/lyrics-translation/
LockScreenLyrics/ the line being sung in the system's now playing, and on iOS 26 the lyrics as the lock screen's
full-screen artwork (LyricsArtwork.x): a new artwork ID per line, its 3:4 H.264 clip (the line and the
diff --git a/tweak/Sources/Redesigned/Lyrics/SGRKaraokeView.m b/tweak/Sources/Redesigned/Lyrics/SGRKaraokeView.m
index 849990d9..354fc806 100644
--- a/tweak/Sources/Redesigned/Lyrics/SGRKaraokeView.m
+++ b/tweak/Sources/Redesigned/Lyrics/SGRKaraokeView.m
@@ -1724,14 +1724,32 @@ static NSArray<NSString *> *textsOf(NSArray<SGKaraokeLine *> *lines) {
return texts;
}
-// Whether the song's words are mostly in a language other than `language`. A song too short to tell counts as one.
+// Whether one distinct line in five is, with the recognizer at least 80% sure, in a language other than `language`:
+// K-pop and other songs half in English count, and a chorus sung eight times weighs as one line. The whole song's
+// guess is not asked: short lines sway it (it reads Havana, all "ooh na-na", as Dutch), and an unsure line does not
+// count either way. A song with no line it is sure of counts as foreign, too little to tell.
static BOOL inAnotherLanguage(NSArray<SGKaraokeLine *> *lines, NSString *language) {
- NSMutableString *words = [NSMutableString string];
- for (SGKaraokeLine *line in lines) if (hasWords(line)) [words appendFormat:@"%@\n", SGKaraokeLineText(line)];
- NSString *found = [NLLanguageRecognizer dominantLanguageForString:words];
- if (!found.length || [found isEqualToString:NLLanguageUndetermined]) return YES;
- NSString *(^code)(NSString *) = ^NSString *(NSString *tag) { return [tag componentsSeparatedByCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:@"-_"]].firstObject.lowercaseString; };
- return ![code(found) isEqualToString:code(language)];
+ NSString *(^code)(NSString *) = ^NSString *(NSString *tag) {
+ return [tag componentsSeparatedByCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:@"-_"]].firstObject.lowercaseString;
+ };
+ NSString *target = code(language);
+ NSUInteger distinct = 0, sure = 0, foreign = 0;
+ NSMutableSet<NSString *> *seen = [NSMutableSet set];
+ NLLanguageRecognizer *recognizer = [NLLanguageRecognizer new];
+ for (SGKaraokeLine *line in lines) {
+ NSString *text = hasWords(line) ? SGKaraokeLineText(line).lowercaseString : nil;
+ if (!text.length || [seen containsObject:text]) continue;
+ [seen addObject:text];
+ distinct++;
+ [recognizer reset];
+ [recognizer processString:text];
+ NSDictionary<NLLanguage, NSNumber *> *guess = [recognizer languageHypothesesWithMaximum:1];
+ NLLanguage found = guess.allKeys.firstObject;
+ if (!found || guess[found].doubleValue < 0.8) continue;
+ sure++;
+ if (![code(found) isEqualToString:target]) foreign++;
+ }
+ return !sure || foreign * 5 >= MAX(distinct, 5);
}
// Whether a line with words is still without a translation.
diff --git a/tweak/Sources/Shared/LyricsTranslation/OnDeviceTranslation.swift b/tweak/Sources/Shared/LyricsTranslation/OnDeviceTranslation.swift
index 1666121d..ff1e480d 100644
--- a/tweak/Sources/Shared/LyricsTranslation/OnDeviceTranslation.swift
+++ b/tweak/Sources/Shared/LyricsTranslation/OnDeviceTranslation.swift
@@ -23,10 +23,26 @@ public final class SGOnDeviceTranslation: NSObject {
Locale.current.localizedString(forIdentifier: language.minimalIdentifier) ?? language.minimalIdentifier
}
- // The song's language, from its words.
- private static func sourceLanguage(_ lines: [String]) -> Locale.Language? {
+ // A line's language when the recognizer is sure of it; short lines ("yeah") guess wildly otherwise.
+ private static func languageOf(_ line: String) -> Locale.Language? {
let recognizer = NLLanguageRecognizer()
- recognizer.processString(lines.joined(separator: "\n"))
+ recognizer.processString(line)
+ guard let (found, chance) = recognizer.languageHypotheses(withMaximum: 1).first, chance >= 0.8 else { return nil }
+ return Locale.Language(identifier: found.rawValue)
+ }
+
+ // The language to translate from: the one most of the song's other-language lines are in, so a K-pop song
+ // half in English is Korean; else the whole song's.
+ private static func sourceLanguage(_ lines: [String], target: Locale.Language) -> Locale.Language? {
+ var counts: [String: (Locale.Language, Int)] = [:]
+ // Each line once, so a repeated chorus does not outvote the verses.
+ for line in Set(lines.map { $0.lowercased() }) where !line.isEmpty {
+ guard let language = languageOf(line), !same(language, target), let code = language.languageCode?.identifier else { continue }
+ counts[code] = (counts[code]?.0 ?? language, (counts[code]?.1 ?? 0) + 1)
+ }
+ if let most = counts.values.max(by: { $0.1 < $1.1 }) { return most.0 }
+ let recognizer = NLLanguageRecognizer()
+ recognizer.processString(Set(lines).joined(separator: "\n"))
guard let found = recognizer.dominantLanguage, found != .undetermined else { return nil }
return Locale.Language(identifier: found.rawValue)
}
@@ -46,7 +62,7 @@ public final class SGOnDeviceTranslation: NSObject {
@objc public static func translate(_ lines: [String], to languageTag: String, done: @escaping ([String]?, String?) -> Void) {
guard #available(iOS 26.0, *) else { return finish(done, nil, "Translating on this iPhone needs iOS 26.") }
let target = Locale.Language(identifier: languageTag)
- guard let source = sourceLanguage(lines) else { return finish(done, nil, "The song's language could not be told from its words.") }
+ guard let source = sourceLanguage(lines, target: target) else { return finish(done, nil, "The song's language could not be told from its words.") }
if same(source, target) { return finish(done, nil, "The song is already in \(name(target)).") }
Task {
switch await LanguageAvailability().status(from: source, to: target) {
@@ -59,8 +75,9 @@ public final class SGOnDeviceTranslation: NSObject {
@unknown default:
break
}
+ // Lines already in the target language are left as they are, not run through the other language.
let requests = lines.enumerated().compactMap { index, line in
- line.trimmingCharacters(in: .whitespaces).isEmpty || line == "♪" ? nil
+ line.trimmingCharacters(in: .whitespaces).isEmpty || line == "♪" || languageOf(line).map({ same($0, target) }) == true ? nil
: TranslationSession.Request(sourceText: line, clientIdentifier: String(index))
}
let started = Date()