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

feat: translations are saved, and Apple Intelligence shows each batch as it comes

My-Name-Is-Jeffd59b64edd0b8
diff --git a/docs/tweaks.md b/docs/tweaks.md
index 2c96990b..e5e59bdf 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. Gemini's reply read and checked on the Mac
+ whose prompt and menu item say the lyrics go to Google. Apple Intelligence's lines show a batch at a time as they come. 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/harness/lyrics/stubs.m b/harness/lyrics/stubs.m
index f677bfce..0c02f188 100644
--- a/harness/lyrics/stubs.m
+++ b/harness/lyrics/stubs.m
@@ -69,10 +69,20 @@ void SGLyricsTranslateWithGemini(NSString *trackID, NSArray<SGKaraokeLine *> *li
+ (void)translate:(NSArray<NSString *> *)lines to:(NSString *)languageTag done:(void (^)(NSArray<NSString *> *, NSString *))done {
[self answer:lines as:@"iPhone: " done:done];
}
-+ (void)translateWithAppleIntelligence:(NSArray<NSString *> *)lines to:(NSString *)languageTag done:(void (^)(NSArray<NSString *> *, NSString *))done {
- [self answer:lines as:@"Intelligence: " done:done];
+// A batch at a time, as the model works: the first half shows a second before the rest.
++ (void)translateWithAppleIntelligence:(NSArray<NSString *> *)lines to:(NSString *)languageTag progress:(void (^)(NSArray<NSString *> *))progress
+ done:(void (^)(NSArray<NSString *> *, NSString *))done {
+ NSMutableArray<NSString *> *half = [NSMutableArray array];
+ [lines enumerateObjectsUsingBlock:^(NSString *line, NSUInteger i, BOOL *stop) {
+ [half addObject:i < lines.count / 2 && line.length ? [@"Intelligence: " stringByAppendingString:line] : @""];
+ }];
+ dispatch_async(dispatch_get_main_queue(), ^{ progress(half); });
+ dispatch_after(dispatch_time(DISPATCH_TIME_NOW, NSEC_PER_SEC), dispatch_get_main_queue(), ^{ [self answer:lines as:@"Intelligence: " done:done]; });
}
@end
+// Nothing is kept between runs of the harness.
+BOOL SGLyricsApplySavedTranslation(NSString *track, NSString *language, NSArray<SGKaraokeLine *> *lines) { return NO; }
+void SGLyricsSaveTranslation(NSString *track, NSString *language, NSArray<SGKaraokeLine *> *lines) {}
// Line meanings: -title and -artist name the track Genius is searched for, and the setting's key
// (-spotifyglass.lyricsMeanings 3) turns them on.
diff --git a/tweak/Sources/App/Pages.m b/tweak/Sources/App/Pages.m
index 78356625..daba3f68 100644
--- a/tweak/Sources/App/Pages.m
+++ b/tweak/Sources/App/Pages.m
@@ -119,7 +119,7 @@ static UIViewController *lyricsPage(void) {
SGSwitchRow(@"Hide the controls", @"A few seconds after the last touch, the lyrics take the whole player", SGRKeyLyricsAutoHide),
SGSwitchRow(@"Landscape lyrics", @"Turn the phone with the lyrics open", SGRKeyLyricsLandscape)]),
timing,
- SGSection(@"Translation", @[SGLyricsTranslationLanguageRow(), SGGeminiKeyRow()]),
+ SGSection(@"Translation", @[SGLyricsTranslationLanguageRow(), SGGeminiKeyRow(), SGSavedTranslationsRow()]),
SGSection(nil, @[SGLyricsMeaningsRow()]),
]];
} else {
diff --git a/tweak/Sources/Redesigned/Lyrics/SGRKaraokeView.m b/tweak/Sources/Redesigned/Lyrics/SGRKaraokeView.m
index 7840599f..26e94712 100644
--- a/tweak/Sources/Redesigned/Lyrics/SGRKaraokeView.m
+++ b/tweak/Sources/Redesigned/Lyrics/SGRKaraokeView.m
@@ -1704,7 +1704,8 @@ static BOOL hasWords(SGKaraokeLine *line) {
// A way to translate a song: the lines, the track and the language in, one translation per line out, or a
// message to show. Main queue.
typedef void (^SGRTranslated)(NSArray<NSString *> *translations, NSString *error);
-typedef void (^SGRTranslator)(NSArray<SGKaraokeLine *> *lines, NSString *track, NSString *language, SGRTranslated done);
+// `progress` may be called with the lines so far before `done`.
+typedef void (^SGRTranslator)(NSArray<SGKaraokeLine *> *lines, NSString *track, NSString *language, SGRTranslated progress, SGRTranslated done);
// The lines' text for a translator, "" for a line with no words.
static NSArray<NSString *> *textsOf(NSArray<SGKaraokeLine *> *lines) {
@@ -1723,6 +1724,7 @@ static NSArray<NSString *> *textsOf(NSArray<SGKaraokeLine *> *lines) {
_breakCount = 0;
_hasSpoken = _hasTranslation = _untranslated = NO;
_plain = SGKaraokeLinesTiming(_lines) == SGKaraokeTimingNone;
+ if (!_sample) SGLyricsApplySavedTranslation(_track, SGLyricsGeminiLanguage(), _lines);
NSInteger sungTo = 0; // the top of the song counts as where the singing before the first line ends
for (NSUInteger i = 0; i < count; i++) {
SGKaraokeLine *line = _lines[i];
@@ -1875,17 +1877,17 @@ static NSArray<NSString *> *textsOf(NSArray<SGKaraokeLine *> *lines) {
};
if (onDevice) {
offer(@"Translate on iPhone", @"translate", @"Apple's Translate, on this iPhone",
- ^(NSArray<SGKaraokeLine *> *lines, NSString *track, NSString *to, SGRTranslated done) {
+ ^(NSArray<SGKaraokeLine *> *lines, NSString *track, NSString *to, SGRTranslated progress, SGRTranslated done) {
[SGOnDeviceTranslation translate:textsOf(lines) to:to done:done];
});
}
if (intelligence) {
offer(@"Translate with Apple Intelligence", @"apple.intelligence", @"On this iPhone",
- ^(NSArray<SGKaraokeLine *> *lines, NSString *track, NSString *to, SGRTranslated done) {
- [SGOnDeviceTranslation translateWithAppleIntelligence:textsOf(lines) to:to done:done];
+ ^(NSArray<SGKaraokeLine *> *lines, NSString *track, NSString *to, SGRTranslated progress, SGRTranslated done) {
+ [SGOnDeviceTranslation translateWithAppleIntelligence:textsOf(lines) to:to progress:^(NSArray<NSString *> *soFar) { progress(soFar, nil); } done:done];
});
}
- if (gemini) offer(@"Translate with Gemini", @"sparkles", @"Sends the lyrics to Google", ^(NSArray<SGKaraokeLine *> *lines, NSString *track, NSString *to, SGRTranslated done) {
+ if (gemini) offer(@"Translate with Gemini", @"sparkles", @"Sends the lyrics to Google", ^(NSArray<SGKaraokeLine *> *lines, NSString *track, NSString *to, SGRTranslated progress, SGRTranslated done) {
SGLyricsTranslateWithGemini(track, lines, to, done);
});
}
@@ -1902,10 +1904,13 @@ static NSArray<NSString *> *textsOf(NSArray<SGKaraokeLine *> *lines) {
if (!lines.count || _translating == lines) return;
_translating = lines;
[self offerExtras];
- translator(lines, track, SGLyricsGeminiLanguage(), ^(NSArray<NSString *> *translations, NSString *error) {
+ NSString *language = SGLyricsGeminiLanguage();
+ translator(lines, track, language, ^(NSArray<NSString *> *soFar, NSString *error) {
+ if (lines == self->_lines) [self takeTranslations:soFar into:lines];
+ }, ^(NSArray<NSString *> *translations, NSString *error) {
if (self->_translating == lines) self->_translating = nil;
- [self offerExtras];
if (!translations) {
+ [self offerExtras];
UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"No translation" message:error
preferredStyle:UIAlertControllerStyleAlert];
// Over the player, which is dark whatever the system's appearance.
@@ -1914,17 +1919,30 @@ static NSArray<NSString *> *textsOf(NSArray<SGKaraokeLine *> *lines) {
[SGTopController() presentViewController:alert animated:YES completion:nil];
return;
}
- if (lines != self->_lines) return; // the song moved on meanwhile
- [lines enumerateObjectsUsingBlock:^(SGKaraokeLine *line, NSUInteger i, BOOL *stop) {
- if (i < translations.count && translations[i].length && !line.translation.length) line.translation = translations[i];
- }];
- self->_hasTranslation = YES;
- self->_untranslated = NO;
- if (SGFlag(SGRKeyLyricsTranslation, NO)) [self restyle];
- else SGRSetLyricsTextShown(SGRLyricsTextTranslation, YES);
+ [self takeTranslations:translations into:lines];
+ SGLyricsSaveTranslation(track, language, lines);
+ if (lines == self->_lines) self->_untranslated = NO;
+ [self offerExtras];
});
}
+// Each line still without a translation takes the one given for it, and translations are switched on so
+// they show. The lines may be another song's by now: they keep the translation, the page is left alone.
+- (void)takeTranslations:(NSArray<NSString *> *)translations into:(NSArray<SGKaraokeLine *> *)lines {
+ __block BOOL took = NO;
+ [lines enumerateObjectsUsingBlock:^(SGKaraokeLine *line, NSUInteger i, BOOL *stop) {
+ if (i < translations.count && translations[i].length && !line.translation.length) {
+ line.translation = translations[i];
+ took = YES;
+ }
+ }];
+ if (!took || lines != _lines) return;
+ _hasTranslation = YES;
+ [self offerExtras];
+ if (SGFlag(SGRKeyLyricsTranslation, NO)) [self restyle];
+ else SGRSetLyricsTextShown(SGRLyricsTextTranslation, YES);
+}
+
// Where a line starts on the page, for the stack as it is arranged now: an open break holds the room
// of one row at the anchor, and the lines from the one after it on are moved down by it.
//
diff --git a/tweak/Sources/Shared/LyricsTranslation/LyricsTranslation.h b/tweak/Sources/Shared/LyricsTranslation/LyricsTranslation.h
index e449b080..82430539 100644
--- a/tweak/Sources/Shared/LyricsTranslation/LyricsTranslation.h
+++ b/tweak/Sources/Shared/LyricsTranslation/LyricsTranslation.h
@@ -29,6 +29,15 @@ SGModRow *SGGeminiKeyRow(void);
@property (class, readonly) BOOL translationAvailable;
+ (BOOL)appleIntelligenceAvailable:(NSString *)languageTag;
+ (void)translate:(NSArray<NSString *> *)lines to:(NSString *)languageTag done:(void (^)(NSArray<NSString *> *lines, NSString *error))done;
+// Apple Intelligence works through the song a batch at a time: `progress` gets the lines so far, "" for the rest.
+ (void)translateWithAppleIntelligence:(NSArray<NSString *> *)lines to:(NSString *)languageTag
+ progress:(void (^)(NSArray<NSString *> *lines))progress
done:(void (^)(NSArray<NSString *> *lines, NSString *error))done;
@end
+
+// SavedTranslations.m: a song's translations kept on disk by track and language, matched to lines by their text.
+// Applying fills only lines with no translation and says whether any took one. Main thread.
+BOOL SGLyricsApplySavedTranslation(NSString *track, NSString *language, NSArray<SGKaraokeLine *> *lines);
+void SGLyricsSaveTranslation(NSString *track, NSString *language, NSArray<SGKaraokeLine *> *lines);
+// The Lyrics page's row: how many songs are kept, and a tap to delete them.
+SGModRow *SGSavedTranslationsRow(void);
diff --git a/tweak/Sources/Shared/LyricsTranslation/OnDeviceTranslation.swift b/tweak/Sources/Shared/LyricsTranslation/OnDeviceTranslation.swift
index b5b7e5f3..e9ba5c1e 100644
--- a/tweak/Sources/Shared/LyricsTranslation/OnDeviceTranslation.swift
+++ b/tweak/Sources/Shared/LyricsTranslation/OnDeviceTranslation.swift
@@ -97,7 +97,8 @@ public final class SGOnDeviceTranslation: NSObject {
return model.availability == .available && model.supportedLanguages.contains { same($0, target) }
}
- @objc public static func translateWithAppleIntelligence(_ lines: [String], to languageTag: String, done: @escaping ([String]?, String?) -> Void) {
+ @objc public static func translateWithAppleIntelligence(_ lines: [String], to languageTag: String, progress: @escaping ([String]) -> Void,
+ done: @escaping ([String]?, String?) -> Void) {
guard #available(iOS 26.0, *) else { return finish(done, nil, "Apple Intelligence needs iOS 26.") }
let language = name(Locale.Language(identifier: languageTag))
Task {
@@ -109,6 +110,8 @@ public final class SGOnDeviceTranslation: NSObject {
let chunkStarted = Date()
out += try await translateChunk(chunk, into: language)
log.notice("intelligence: lines \(start + 1)-\(start + chunk.count) of \(lines.count) in \(Date().timeIntervalSince(chunkStarted), format: .fixed(precision: 1)) s")
+ let soFar = out + [String](repeating: "", count: lines.count - out.count)
+ if out.count < lines.count { DispatchQueue.main.async { progress(soFar) } }
}
log.notice("intelligence: \(lines.count) lines into \(languageTag, privacy: .public) in \(Date().timeIntervalSince(started), format: .fixed(precision: 1)) s")
finish(done, out, nil)
diff --git a/tweak/Sources/Shared/LyricsTranslation/SavedTranslations.m b/tweak/Sources/Shared/LyricsTranslation/SavedTranslations.m
new file mode 100644
index 00000000..7af2487e
--- /dev/null
+++ b/tweak/Sources/Shared/LyricsTranslation/SavedTranslations.m
@@ -0,0 +1,95 @@
+// Translations kept on disk, whichever way they were made: one small JSON file a song and language in
+// Caches/Vitrine/Translations, each line's text to its translation, so a song comes back translated after
+// a relaunch and lyrics from another source still find their lines. The newest kKept files stay; iOS may
+// clear Caches when it runs low, which costs a translation again.
+#import "Core/SGCore.h"
+#import "Settings/SGModPage.h"
+#import "Settings/SGPageStyle.h"
+#import "Shared/Lyrics/Lyrics.h"
+#import "LyricsTranslation.h"
+
+static const NSUInteger kKept = 200;
+
+static NSURL *folder(void) {
+ NSURL *caches = [NSFileManager.defaultManager URLsForDirectory:NSCachesDirectory inDomains:NSUserDomainMask].firstObject;
+ return [[caches URLByAppendingPathComponent:@"Vitrine" isDirectory:YES] URLByAppendingPathComponent:@"Translations" isDirectory:YES];
+}
+
+static NSURL *fileFor(NSString *track, NSString *language) {
+ NSCharacterSet *unsafe = [NSCharacterSet characterSetWithCharactersInString:@"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-"].invertedSet;
+ NSString *tag = [[language componentsSeparatedByCharactersInSet:unsafe] componentsJoinedByString:@"_"];
+ return [folder() URLByAppendingPathComponent:[NSString stringWithFormat:@"%@.%@.json", track, tag]];
+}
+
+static NSArray<NSURL *> *files(void) {
+ return [NSFileManager.defaultManager contentsOfDirectoryAtURL:folder() includingPropertiesForKeys:@[NSURLContentModificationDateKey, NSURLFileSizeKey]
+ options:NSDirectoryEnumerationSkipsHiddenFiles error:nil] ?: @[];
+}
+
+BOOL SGLyricsApplySavedTranslation(NSString *track, NSString *language, NSArray<SGKaraokeLine *> *lines) {
+ if (!track.length || !language.length || !lines.count) return NO;
+ NSData *data = [NSData dataWithContentsOfURL:fileFor(track, language)];
+ NSDictionary *saved = data ? [NSJSONSerialization JSONObjectWithData:data options:0 error:nil] : nil;
+ if (![saved isKindOfClass:NSDictionary.class]) return NO;
+ BOOL applied = NO;
+ for (SGKaraokeLine *line in lines) {
+ if (line.translation.length) continue;
+ id translation = saved[SGKaraokeLineText(line) ?: @""];
+ if (![translation isKindOfClass:NSString.class] || ![translation length]) continue;
+ line.translation = translation;
+ applied = YES;
+ }
+ return applied;
+}
+
+void SGLyricsSaveTranslation(NSString *track, NSString *language, NSArray<SGKaraokeLine *> *lines) {
+ if (!track.length || !language.length) return;
+ NSMutableDictionary<NSString *, NSString *> *saved = [NSMutableDictionary dictionary];
+ for (SGKaraokeLine *line in lines) {
+ NSString *text = SGKaraokeLineText(line);
+ if (text.length && line.translation.length) saved[text] = line.translation;
+ }
+ if (!saved.count) return;
+ NSData *data = [NSJSONSerialization dataWithJSONObject:saved options:0 error:nil];
+ dispatch_async(dispatch_get_global_queue(QOS_CLASS_UTILITY, 0), ^{
+ [NSFileManager.defaultManager createDirectoryAtURL:folder() withIntermediateDirectories:YES attributes:nil error:nil];
+ [data writeToURL:fileFor(track, language) atomically:YES];
+ NSArray<NSURL *> *all = files();
+ if (all.count <= kKept) return;
+ NSArray<NSURL *> *oldestFirst = [all sortedArrayUsingComparator:^NSComparisonResult(NSURL *a, NSURL *b) {
+ NSDate *da, *db;
+ [a getResourceValue:&da forKey:NSURLContentModificationDateKey error:nil];
+ [b getResourceValue:&db forKey:NSURLContentModificationDateKey error:nil];
+ return [da ?: NSDate.distantPast compare:db ?: NSDate.distantPast];
+ }];
+ for (NSUInteger i = 0; i < all.count - kKept; i++) [NSFileManager.defaultManager removeItemAtURL:oldestFirst[i] error:nil];
+ });
+}
+
+static NSString *summary(void) {
+ NSArray<NSURL *> *all = files();
+ if (!all.count) return @"None";
+ long long bytes = 0;
+ for (NSURL *file in all) {
+ NSNumber *size;
+ [file getResourceValue:&size forKey:NSURLFileSizeKey error:nil];
+ bytes += size.longLongValue;
+ }
+ return [NSString stringWithFormat:@"%lu · %@", (unsigned long)all.count,
+ [NSByteCountFormatter stringFromByteCount:bytes countStyle:NSByteCountFormatterCountStyleFile]];
+}
+
+SGModRow *SGSavedTranslationsRow(void) {
+ return SGStatActionRow(@"Saved translations", @"Songs translated on this iPhone or by Gemini come back translated",
+ ^NSString *{ return summary(); }, ^{
+ if (!files().count) return;
+ UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"Delete saved translations?"
+ message:@"Songs translated before are translated again the next time you ask." preferredStyle:UIAlertControllerStyleAlert];
+ [alert addAction:[UIAlertAction actionWithTitle:@"Delete" style:UIAlertActionStyleDestructive handler:^(UIAlertAction *action) {
+ [NSFileManager.defaultManager removeItemAtURL:folder() error:nil];
+ SGLog(@"translation: saved translations deleted");
+ }]];
+ [alert addAction:[UIAlertAction actionWithTitle:@"Cancel" style:UIAlertActionStyleCancel handler:nil]];
+ [SGTopController() presentViewController:alert animated:YES completion:nil];
+ });
+}