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: deleting saved translations and Translate any song act at once

My-Name-Is-Jeffa276e5d13b7b
diff --git a/docs/tweaks.md b/docs/tweaks.md
index 16667da2..abdf442f 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 for every song with Translate any song on (SGKeyLyricsTranslateEverySong, off), else 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, or two in another script (a verse in Hangul among English), 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
+ whose prompt and menu item say the lyrics go to Google. The translate items show for every song with Translate any song on (SGKeyLyricsTranslateEverySong, off), else 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, or two in another script (a verse in Hangul among English), 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, synced when the song's lines are shown: each line knows whether its translation was made here (SGKaraokeLine.translationMade), and a made one with nothing saved is taken off, so the Lyrics page's Saved translations row deletes them at once, posting SGLyricsTranslationsDidChangeNotification as Translate any song does for the open lyrics to sync and redraw. 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 4c636804..0da30226 100644
--- a/harness/lyrics/stubs.m
+++ b/harness/lyrics/stubs.m
@@ -81,7 +81,8 @@ void SGLyricsTranslateWithGemini(NSString *trackID, NSArray<SGKaraokeLine *> *li
}
@end
// Nothing is kept between runs of the harness.
-BOOL SGLyricsApplySavedTranslation(NSString *track, NSString *language, NSArray<SGKaraokeLine *> *lines) { return NO; }
+void SGLyricsSyncSavedTranslation(NSString *track, NSString *language, NSArray<SGKaraokeLine *> *lines) {}
+NSNotificationName const SGLyricsTranslationsDidChangeNotification = @"spotifyglass.lyricsTranslationsDidChange";
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
diff --git a/tweak/Sources/Redesigned/Lyrics/SGRKaraokeView.m b/tweak/Sources/Redesigned/Lyrics/SGRKaraokeView.m
index 44fe367a..8ac414fb 100644
--- a/tweak/Sources/Redesigned/Lyrics/SGRKaraokeView.m
+++ b/tweak/Sources/Redesigned/Lyrics/SGRKaraokeView.m
@@ -1359,6 +1359,7 @@ typedef struct {
[NSNotificationCenter.defaultCenter addObserver:self selector:@selector(playerTransitionChanged:) name:SGPlayerTransitionEndedNotification object:nil];
[NSNotificationCenter.defaultCenter addObserver:self selector:@selector(restyle) name:SGRLyricsTextDidChangeNotification object:nil];
[NSNotificationCenter.defaultCenter addObserver:self selector:@selector(restyle) name:SGLyricsRomanisedDidChangeNotification object:nil];
+ [NSNotificationCenter.defaultCenter addObserver:self selector:@selector(translationsChanged) name:SGLyricsTranslationsDidChangeNotification object:nil];
[NSNotificationCenter.defaultCenter addObserver:self selector:@selector(lookChanged) name:SGRLyricsLookDidChangeNotification object:nil];
// A locked phone leaves the card in its window, so the link has to be put down by the app going
// away rather than by the view going: see scheduleLink.
@@ -1772,7 +1773,7 @@ static BOOL anyUntranslated(NSArray<SGKaraokeLine *> *lines) {
_breakCount = 0;
_hasSpoken = _hasTranslation = _untranslated = NO;
_plain = SGKaraokeLinesTiming(_lines) == SGKaraokeTimingNone;
- if (!_sample) SGLyricsApplySavedTranslation(_track, SGLyricsGeminiLanguage(), _lines);
+ if (!_sample) SGLyricsSyncSavedTranslation(_track, SGLyricsGeminiLanguage(), _lines);
_foreign = inAnotherLanguage(_lines, SGLyricsGeminiLanguage());
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++) {
@@ -1980,6 +1981,13 @@ static BOOL anyUntranslated(NSArray<SGKaraokeLine *> *lines) {
});
}
+// The saved translations deleted, or Translate any song switched: the song is synced again and redrawn.
+- (void)translationsChanged {
+ if (!_lines) return;
+ [self timeLines];
+ [self restyle];
+}
+
// 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 {
@@ -1987,6 +1995,7 @@ static BOOL anyUntranslated(NSArray<SGKaraokeLine *> *lines) {
[lines enumerateObjectsUsingBlock:^(SGKaraokeLine *line, NSUInteger i, BOOL *stop) {
if (i < translations.count && translations[i].length && !line.translation.length) {
line.translation = translations[i];
+ line.translationMade = YES;
took = YES;
}
}];
diff --git a/tweak/Sources/Shared/Lyrics/KaraokeTiming.m b/tweak/Sources/Shared/Lyrics/KaraokeTiming.m
index d312c031..34622550 100644
--- a/tweak/Sources/Shared/Lyrics/KaraokeTiming.m
+++ b/tweak/Sources/Shared/Lyrics/KaraokeTiming.m
@@ -16,6 +16,7 @@
copy.backing = _backing;
copy.pronunciation = _pronunciation;
copy.translation = _translation;
+ copy.translationMade = _translationMade;
return copy;
}
@end
diff --git a/tweak/Sources/Shared/Lyrics/Lyrics.h b/tweak/Sources/Shared/Lyrics/Lyrics.h
index f91448af..2184786d 100644
--- a/tweak/Sources/Shared/Lyrics/Lyrics.h
+++ b/tweak/Sources/Shared/Lyrics/Lyrics.h
@@ -82,6 +82,8 @@ typedef NS_ENUM(NSUInteger, SGKaraokeAlign) {
@property (nonatomic, strong) SGKaraokeLine *pronunciation;
// The line in another language, the backing's words with it; nil where the source has none.
@property (nonatomic, copy) NSString *translation;
+// The translation came from a translator of the lyrics' menu (LyricsTranslation), not from the lyrics' source.
+@property (nonatomic) BOOL translationMade;
@end
// The line as one string, a space between the words that are not joined.
diff --git a/tweak/Sources/Shared/LyricsTranslation/LyricsTranslation.h b/tweak/Sources/Shared/LyricsTranslation/LyricsTranslation.h
index 3e39f9e0..e82a8c81 100644
--- a/tweak/Sources/Shared/LyricsTranslation/LyricsTranslation.h
+++ b/tweak/Sources/Shared/LyricsTranslation/LyricsTranslation.h
@@ -7,6 +7,10 @@
BOOL SGGeminiKeySet(void);
+// Posted on the main thread when the saved translations are deleted or Translate any song changes: the lyrics
+// page syncs its song again and redraws.
+extern NSNotificationName const SGLyricsTranslationsDidChangeNotification;
+
// On: the translate menu is offered for every song, not only those found in another language.
#define SGKeyLyricsTranslateEverySong @"spotifyglass.lyrics.translateEverySong"
SGModRow *SGTranslateEverySongRow(void);
@@ -43,8 +47,9 @@ SGModRow *SGGeminiKeyRow(void);
@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);
+// The saved ones are the truth for translations made here: syncing fills the lines with no translation from them,
+// and takes a made translation off a line that has none saved (after Delete). Main thread.
+void SGLyricsSyncSavedTranslation(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/LyricsTranslation.m b/tweak/Sources/Shared/LyricsTranslation/LyricsTranslation.m
index f9939ceb..747701a4 100644
--- a/tweak/Sources/Shared/LyricsTranslation/LyricsTranslation.m
+++ b/tweak/Sources/Shared/LyricsTranslation/LyricsTranslation.m
@@ -196,9 +196,15 @@ static void askForKey(void) {
[SGTopController() presentViewController:alert animated:YES completion:nil];
}
+NSNotificationName const SGLyricsTranslationsDidChangeNotification = @"spotifyglass.lyricsTranslationsDidChange";
+
SGModRow *SGTranslateEverySongRow(void) {
- return SGOptionRow(@"Translate any song", @"The lyrics' translate menu for songs that already read as your language",
- SGKeyLyricsTranslateEverySong);
+ SGModRow *row = SGOptionRow(@"Translate any song", @"The lyrics' translate menu for songs that already read as your language",
+ SGKeyLyricsTranslateEverySong);
+ row.changed = ^(BOOL on) {
+ [NSNotificationCenter.defaultCenter postNotificationName:SGLyricsTranslationsDidChangeNotification object:nil];
+ };
+ return row;
}
SGModRow *SGGeminiKeyRow(void) {
diff --git a/tweak/Sources/Shared/LyricsTranslation/SavedTranslations.m b/tweak/Sources/Shared/LyricsTranslation/SavedTranslations.m
index aea72660..373474ca 100644
--- a/tweak/Sources/Shared/LyricsTranslation/SavedTranslations.m
+++ b/tweak/Sources/Shared/LyricsTranslation/SavedTranslations.m
@@ -26,20 +26,22 @@ static NSArray<NSURL *> *files(void) {
options:NSDirectoryEnumerationSkipsHiddenFiles error:nil] ?: @[];
}
-BOOL SGLyricsApplySavedTranslation(NSString *track, NSString *language, NSArray<SGKaraokeLine *> *lines) {
- if (!track.length || !language.length || !lines.count) return NO;
+void SGLyricsSyncSavedTranslation(NSString *track, NSString *language, NSArray<SGKaraokeLine *> *lines) {
+ if (!track.length || !language.length || !lines.count) return;
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;
+ id saved = data ? [NSJSONSerialization JSONObjectWithData:data options:0 error:nil] : nil;
+ if (![saved isKindOfClass:NSDictionary.class]) saved = nil;
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;
+ BOOL has = [translation isKindOfClass:NSString.class] && [translation length];
+ if (line.translationMade && !has) {
+ line.translation = nil;
+ line.translationMade = NO;
+ } else if (!line.translation.length && has) {
+ line.translation = translation;
+ line.translationMade = YES;
+ }
}
- return applied;
}
void SGLyricsSaveTranslation(NSString *track, NSString *language, NSArray<SGKaraokeLine *> *lines) {
@@ -92,6 +94,7 @@ SGModRow *SGSavedTranslationsRow(void) {
[alert addAction:[UIAlertAction actionWithTitle:@"Delete" style:UIAlertActionStyleDestructive handler:^(UIAlertAction *action) {
[NSFileManager.defaultManager removeItemAtURL:folder() error:nil];
SGLog(@"translation: saved translations deleted");
+ [NSNotificationCenter.defaultCenter postNotificationName:SGLyricsTranslationsDidChangeNotification object:nil];
}]];
[alert addAction:[UIAlertAction actionWithTitle:@"Cancel" style:UIAlertActionStyleCancel handler:nil]];
[SGTopController() presentViewController:alert animated:YES completion:nil];