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: lyrics take Musixmatch's community translations, whichever source the lines came from

My-Name-Is-Jeff88e885bd1b20
diff --git a/harness/lyrics/main.m b/harness/lyrics/main.m
index d30e300b..9a78659c 100644
--- a/harness/lyrics/main.m
+++ b/harness/lyrics/main.m
@@ -16,6 +16,9 @@
// -dump 1 prints the lines as read, with their pronunciations and translations, and quits
// -openMenu S opens the pronunciation and translation menu S seconds in, as a tap on its button would
// -toggleAt S switches the pronunciation and the translation over S seconds in, as the menu would
+// -translateIn S keeps copies of the lines S seconds in with every other one translated, as
+// Musixmatch's community translations come in after the lines on the phone
+// -gemini 1 a Gemini key is set, so the menu offers Translate with Gemini (stubs.m answers it)
// and the lyrics' own settings by their keys: -spotifyglass.lyricsSimulateWords 1 (sweep line timed
// lines on the estimate), -spotifyglass.redesign.lyricsPronunciation 1,
// -spotifyglass.redesign.lyricsTranslation 1, -spotifyglass.redesign.lyricsTextOrder '(translation, lyrics, pronunciation)'.
@@ -238,6 +241,17 @@ static void timedTick(id self, SEL _cmd) {
}
});
}
+ if ([args objectForKey:@"translateIn"]) {
+ dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)([args doubleForKey:@"translateIn"] * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
+ NSMutableArray<SGKaraokeLine *> *translated = [SGKaraokeLinesForTrack(@"harness") mutableCopy];
+ for (NSUInteger i = 0; i < translated.count; i += 2) {
+ SGKaraokeLine *line = [translated[i] copy];
+ line.translation = [@"Translated: " stringByAppendingString:SGKaraokeLineText(line)];
+ translated[i] = line;
+ }
+ SGKaraokeKeepLines(@"harness", translated);
+ });
+ }
#ifdef SGRKeyLyricsTextOrder
if ([args objectForKey:@"toggleAt"]) {
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)([args doubleForKey:@"toggleAt"] * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
diff --git a/tweak/Sources/Redesigned/Lyrics/SGRKaraokeView.m b/tweak/Sources/Redesigned/Lyrics/SGRKaraokeView.m
index b65aef3f..026fd7c5 100644
--- a/tweak/Sources/Redesigned/Lyrics/SGRKaraokeView.m
+++ b/tweak/Sources/Redesigned/Lyrics/SGRKaraokeView.m
@@ -1096,6 +1096,7 @@ typedef struct {
CFTimeInterval _stillSince; // when the position stopped moving, 0 while it moves
SGRKaraokeStyle *_style; // how the lines were laid out
BOOL _hasSpoken, _hasTranslation; // whether the song has any line with either
+ NSArray<SGKaraokeLine *> *_passedOver; // lines kept for the song that changed nothing here, not looked at again
UIButton *_extras;
CGFloat _builtWidth;
BOOL _showing;
@@ -1342,6 +1343,13 @@ typedef struct {
_dots = nil;
}
+static BOOL hasTranslations(NSArray<SGKaraokeLine *> *lines) {
+ for (SGKaraokeLine *line in lines) {
+ if (line.translation.length) return YES;
+ }
+ return NO;
+}
+
// When each line is sung and where the breaks are, worked out once per song for the frames to read.
- (void)timeLines {
free(_spans);
@@ -1719,19 +1727,40 @@ typedef struct {
SGLog(@"karaoke: page shows track %@, lyrics %@", track, SGKaraokeLinesForTrack(track) ? @"captured" : @"not captured yet");
_track = track;
_lines = nil;
+ _passedOver = nil;
_builtWidth = 0;
[self creditTo:nil];
[self dropLineViews];
[self offerExtras];
}
- // Plain text is shown while Spotify is asked whether it has the song timed; its answer replaces it.
- NSArray<SGKaraokeLine *> *kept = _plain && _lines && track ? SGKaraokeLinesForTrack(track) : nil;
- if (kept && kept != _lines) {
- SGLog(@"karaoke: timed lines of %@ came in over the plain text", track);
- _lines = nil;
- _builtWidth = 0;
- [self creditTo:nil];
- [self dropLineViews];
+ NSArray<SGKaraokeLine *> *kept = _lines && track ? SGKaraokeLinesForTrack(track) : nil;
+ if (kept && kept != _lines && kept != _passedOver) {
+ if (!_hasTranslation && kept.count == _lines.count && SGKaraokeLinesTiming(kept) == SGKaraokeLinesTiming(_lines)
+ && hasTranslations(kept)) {
+ // Musixmatch's translations come in a moment after the lines, as copies of them: the song is
+ // restyled with them where it stands.
+ SGLog(@"karaoke: translations of %@ came in", track);
+ if (_tops) {
+ _lines = kept;
+ [self timeLines];
+ [self restyle];
+ } else {
+ // Nothing placed yet, and a measure may be in flight in the old style: it is dropped, and
+ // the translated lines are built below as new ones.
+ _lines = nil;
+ _builtWidth = 0;
+ [self dropLineViews];
+ }
+ } else if (_plain) {
+ // Plain text is shown while Spotify is asked whether it has the song timed; its answer replaces it.
+ SGLog(@"karaoke: timed lines of %@ came in over the plain text", track);
+ _lines = nil;
+ _builtWidth = 0;
+ [self creditTo:nil];
+ [self dropLineViews];
+ } else {
+ _passedOver = kept;
+ }
}
if (!_lines && track && (_lines = SGKaraokeLinesForTrack(track))) {
SGLog(@"karaoke: showing %lu lines of %@", (unsigned long)_lines.count, track);
diff --git a/tweak/Sources/Shared/Lyrics/KaraokeSource.x b/tweak/Sources/Shared/Lyrics/KaraokeSource.x
index 21c3db9d..7beba1e1 100644
--- a/tweak/Sources/Shared/Lyrics/KaraokeSource.x
+++ b/tweak/Sources/Shared/Lyrics/KaraokeSource.x
@@ -72,6 +72,30 @@ static SPTPlayerTrack *upNextIn(SPTPlayerState *state) {
return [next isKindOfClass:objc_getClass("SPTPlayerTrack")] ? next : nil;
}
+static BOOL hasTranslations(NSArray<SGKaraokeLine *> *lines) {
+ for (SGKaraokeLine *line in lines) {
+ if (line.translation.length) return YES;
+ }
+ return NO;
+}
+
+// Lines with no translation of their own take Musixmatch's community ones in the Lyrics page's language,
+// whichever source they came from, Spotify's own included, matched by their text. Only the redesign shows
+// translations, so the native look asks for none. The translated lines are copies kept in place of the
+// lines: the lyrics view measures lines off the main thread, and a new array is what tells it to look.
+// Musixmatch answers at once once it has answered, so lines kept again take the translations straight away.
+static void translate(NSString *track) {
+ NSString *language = SGRedesignedUI() ? SGLyricsTranslationLanguage() : nil;
+ if (!language || hasTranslations(sg_lyrics[track])) return;
+ SGMusixmatchTranslations(track, language, ^(NSDictionary<NSString *, NSString *> *byLine) {
+ // Whatever is kept by now, which may be Spotify's timed lines in place of the plain ones asked for.
+ NSArray<SGKaraokeLine *> *kept = sg_lyrics[track];
+ if (!byLine || hasTranslations(kept)) return;
+ NSArray<SGKaraokeLine *> *translated = SGMusixmatchTranslatedLines(kept, byLine);
+ if (translated) sg_lyrics[track] = translated;
+ });
+}
+
// Main queue only. A full cache is emptied but for the track playing, the one up next and those still
// being asked for; what goes can be asked for again.
static void keep(NSString *track, NSArray<SGKaraokeLine *> *lines) {
@@ -88,6 +112,7 @@ static void keep(NSString *track, NSArray<SGKaraokeLine *> *lines) {
}
}
sg_lyrics[track] = lines;
+ if (lines.count) translate(track);
}
void SGKaraokeKeepLines(NSString *track, NSArray<SGKaraokeLine *> *lines) {
diff --git a/tweak/Sources/Shared/Lyrics/KaraokeTiming.m b/tweak/Sources/Shared/Lyrics/KaraokeTiming.m
index 781b3cda..00c7481d 100644
--- a/tweak/Sources/Shared/Lyrics/KaraokeTiming.m
+++ b/tweak/Sources/Shared/Lyrics/KaraokeTiming.m
@@ -5,6 +5,19 @@
@end
@implementation SGKaraokeLine
+- (id)copyWithZone:(NSZone *)zone {
+ SGKaraokeLine *copy = [SGKaraokeLine new];
+ copy.words = _words;
+ copy.start = _start;
+ copy.end = _end;
+ copy.voice = _voice;
+ copy.align = _align;
+ copy.timing = _timing;
+ copy.backing = _backing;
+ copy.pronunciation = _pronunciation;
+ copy.translation = _translation;
+ return copy;
+}
@end
// A line is sung at about this pace and never takes longer than the gap to the next one. A slow
diff --git a/tweak/Sources/Shared/Lyrics/Lyrics.h b/tweak/Sources/Shared/Lyrics/Lyrics.h
index bc0eee00..04014cdb 100644
--- a/tweak/Sources/Shared/Lyrics/Lyrics.h
+++ b/tweak/Sources/Shared/Lyrics/Lyrics.h
@@ -10,8 +10,8 @@
@class SGModRow, SGModSection;
// LyricsSettings.m: the Lyrics page's parts (App/Pages.m puts the page together): where lyrics come
// from, naming the source (read by the redesign's lyrics view only), the lock screen, which
-// language a line's translation is taken in, of those the lyrics come with (the redesign's lyrics
-// being where translations show), and whether lines timed only by the line are swept word by word.
+// language a line's translation is taken in, of those the lyrics come with, and the one Musixmatch's
+// community translations are asked in (the redesign's lyrics being where translations show), and whether lines timed only by the line are swept word by word.
SGModSection *SGLyricsSourcesSection(BOOL namingSource);
SGModRow *SGLockScreenLyricsRow(void);
SGModRow *SGLyricsTranslationLanguageRow(void);
@@ -47,7 +47,9 @@ typedef NS_ENUM(NSUInteger, SGKaraokeAlign) {
@property (nonatomic) BOOL joined;
@end
-@interface SGKaraokeLine : NSObject
+// A copy shares the words, the backing and the pronunciation, which nothing changes once a line is
+// built; a translation set on the copy leaves the original as it was.
+@interface SGKaraokeLine : NSObject <NSCopying>
@property (nonatomic, copy) NSArray<SGKaraokeWord *> *words;
@property (nonatomic) NSInteger start, end;
// The voice singing the line, named as the source names it ("v1", "v2"), nil when it names none.
diff --git a/tweak/Sources/Shared/Lyrics/LyricsSettings.m b/tweak/Sources/Shared/Lyrics/LyricsSettings.m
index cd7b66e9..fd9d5acb 100644
--- a/tweak/Sources/Shared/Lyrics/LyricsSettings.m
+++ b/tweak/Sources/Shared/Lyrics/LyricsSettings.m
@@ -24,7 +24,8 @@ SGModRow *SGLockScreenLyricsRow(void) {
SGModRow *SGLyricsTranslationLanguageRow(void) {
SGModRow *row = SGChoiceRow(@"Translation language", nil, SGKeyLyricsTranslationLanguage, SGLyricsTranslationLanguageNames(), 0);
- row.choiceFooter = @"Used when the lyrics come with translations. Any shows the first.";
+ row.choiceFooter = @"A language brings Musixmatch's community translations to lyrics that have none, "
+ @"which sends Musixmatch each song's ID. Any shows the first translation the lyrics come with.";
return row;
}
diff --git a/tweak/Sources/Shared/LyricsSources/LyricsSources.h b/tweak/Sources/Shared/LyricsSources/LyricsSources.h
index 5cb0ac28..8da7d898 100644
--- a/tweak/Sources/Shared/LyricsSources/LyricsSources.h
+++ b/tweak/Sources/Shared/LyricsSources/LyricsSources.h
@@ -125,6 +125,13 @@ NSString *SGLyricsTranslationLanguage(void);
// The sources themselves, each in its own file.
extern SGLyricsAsk SGBiniLyricsAsk;
extern SGLyricsAsk SGMusixmatchAsk;
+// Musixmatch's community translations of a track into `language`, by SGMusixmatchLineKey of each original
+// line, or nil when there are none. Main queue, and at once when the answer is in. Asked once per
+// track and language a launch, again only after an answer cut short.
+void SGMusixmatchTranslations(NSString *trackID, NSString *language, void (^done)(NSDictionary<NSString *, NSString *> *byLine));
+NSString *SGMusixmatchLineKey(NSString *text);
+// Copies of the lines with the translations matched in, in a new array; nil when none matched.
+NSArray<SGKaraokeLine *> *SGMusixmatchTranslatedLines(NSArray<SGKaraokeLine *> *lines, NSDictionary<NSString *, NSString *> *byLine);
extern SGLyricsAsk SGUnisonAsk;
extern SGLyricsAsk SGNetEaseAsk;
extern SGLyricsAsk SGLrcLibAsk;
diff --git a/tweak/Sources/Shared/LyricsSources/LyricsSources.m b/tweak/Sources/Shared/LyricsSources/LyricsSources.m
index 9eb8690c..5e4b7d68 100644
--- a/tweak/Sources/Shared/LyricsSources/LyricsSources.m
+++ b/tweak/Sources/Shared/LyricsSources/LyricsSources.m
@@ -296,16 +296,18 @@ static BOOL named(SGLyricsQuery *query) {
@implementation SGLyricsWalk
@end
-// A walk that ends with nothing is only an answer when every source got to search. One that passed a
-// source over for want of a name asked it nothing, and keeping that as "no lyrics" would stick to the
-// track: every later request would get the kept nil, and the lyrics card would be taken off the track
-// for the rest of the session. The same goes for a walk during which a request failed: a busy
-// server's 503 read as "no lyrics" hid a track's lyrics until Spotify was restarted.
+// Musixmatch's community translations are not waited for here: the lines take them as they are kept
+// for the lyrics view (Shared/Lyrics/KaraokeSource.x), whichever source won, Spotify included.
static void finish(SGLyricsWalk *walk) {
SGLyricsQuery *query = walk.query;
SGLyricsResult *merged = walk.merged;
NSString *trackID = query.trackID;
SGLyricsResult *lyrics = merged.karaokeLines.count || merged.texts.count ? merged : nil;
+ // A walk that ends with nothing is only an answer when every source got to search. One that passed a
+ // source over for want of a name asked it nothing, and keeping that as "no lyrics" would stick to the
+ // track: every later request would get the kept nil, and the lyrics card would be taken off the track
+ // for the rest of the session. The same goes for a walk during which a request failed: a busy
+ // server's 503 read as "no lyrics" hid a track's lyrics until Spotify was restarted.
BOOL everyoneAsked = !walk.passedOver.count;
BOOL failed = atomic_load(&sg_failures) != walk.failuresAtStart;
if (lyrics || (everyoneAsked && !failed) || merged.instrumental) {
diff --git a/tweak/Sources/Shared/LyricsSources/Musixmatch.m b/tweak/Sources/Shared/LyricsSources/Musixmatch.m
index 54c88ecb..14bfa42a 100644
--- a/tweak/Sources/Shared/LyricsSources/Musixmatch.m
+++ b/tweak/Sources/Shared/LyricsSources/Musixmatch.m
@@ -21,6 +21,11 @@ static NSMutableDictionary<NSString *, id> *sg_kept;
static NSMutableDictionary<NSString *, NSMutableArray *> *sg_waiting;
static NSMutableArray<void (^)(NSString *)> *sg_tokenWaiting;
static NSDate *sg_tokenRefused;
+// Musixmatch's own id of a track, by Spotify's, as the lyrics call matched it; translations go by it.
+static NSMutableDictionary<NSString *, NSString *> *sg_commontracks;
+// The community translations, by track and language: those answered, and those still being asked for.
+static NSMutableDictionary<NSString *, NSDictionary *> *sg_translations;
+static NSMutableDictionary<NSString *, NSMutableArray *> *sg_translationsWaiting;
static void setUp(void) {
static dispatch_once_t once;
@@ -28,6 +33,9 @@ static void setUp(void) {
sg_kept = [NSMutableDictionary dictionary];
sg_waiting = [NSMutableDictionary dictionary];
sg_tokenWaiting = [NSMutableArray array];
+ sg_commontracks = [NSMutableDictionary dictionary];
+ sg_translations = [NSMutableDictionary dictionary];
+ sg_translationsWaiting = [NSMutableDictionary dictionary];
});
}
@@ -233,6 +241,13 @@ static SGLyricsResult *fromCalls(NSDictionary *calls) {
#pragma mark - asking
+static void noteCommontrack(NSString *trackID, id commontrack) {
+ if ([commontrack respondsToSelector:@selector(stringValue)]) commontrack = [commontrack stringValue];
+ if (![commontrack isKindOfClass:NSString.class] || ![commontrack length] || [commontrack isEqualToString:@"0"]) return;
+ if (sg_commontracks.count >= kKeptTracks) [sg_commontracks removeAllObjects];
+ sg_commontracks[trackID] = commontrack;
+}
+
static void finish(NSString *trackID, SGLyricsResult *lyrics, BOOL answered) {
if (answered) {
if (sg_kept.count >= kKeptTracks) [sg_kept removeAllObjects];
@@ -283,6 +298,7 @@ static void ask(NSString *trackID, BOOL renewToken) {
finish(trackID, nil, NO);
return;
}
+ noteCommontrack(trackID, dig(calls, @"matcher.track.get/message/body/track/commontrack_id"));
SGLyricsResult *lyrics = fromCalls(calls);
SGLog(@"musixmatch: %@ has %@", trackID, !lyrics ? @"no lyrics it may show"
: lyrics.wordTimed ? [NSString stringWithFormat:@"%lu word timed lines", (unsigned long)lyrics.karaokeLines.count]
@@ -313,3 +329,157 @@ SGLyricsAsk SGMusixmatchAsk = ^(SGLyricsQuery *query, void (^done)(SGLyricsResul
sg_waiting[trackID] = [NSMutableArray arrayWithObject:[done copy]];
ask(trackID, YES);
};
+
+#pragma mark - translations
+
+// The community's translations of a track's lines, by the line's text as SGMusixmatchLineKey makes it.
+// Musixmatch pages them; a page that brings no line not seen yet ends the paging, whatever the API
+// does with the page number.
+static const NSInteger kTranslationPage = 100, kTranslationPages = 5;
+
+// Musixmatch's codes for the Lyrics page's languages where they differ, tried in turn until one has
+// lines. Asked on 5 October 2026: zh-Hans and zh-Hant had no lines where zh (Simplified) and z1
+// (Traditional) had, and Norwegian is kept under nb on some tracks and no on others.
+static NSArray<NSString *> *codesFor(NSString *language) {
+ NSDictionary<NSString *, NSArray<NSString *> *> *codes = @{@"zh-Hans": @[@"zh"], @"zh-Hant": @[@"z1"], @"nb": @[@"nb", @"no"]};
+ return codes[language] ?: @[language];
+}
+
+// What a page's status says about the answer: lost on the way or to a busy server is asked again later.
+static BOOL lostStatus(NSInteger status) {
+ return status == 0 || status == 429 || status >= 500;
+}
+
+NSString *SGMusixmatchLineKey(NSString *text) {
+ if (![text isKindOfClass:NSString.class]) return @"";
+ NSString *folded = [text stringByFoldingWithOptions:NSCaseInsensitiveSearch | NSDiacriticInsensitiveSearch locale:nil];
+ NSMutableArray<NSString *> *words = [NSMutableArray array];
+ for (NSString *word in [folded componentsSeparatedByCharactersInSet:NSCharacterSet.alphanumericCharacterSet.invertedSet]) {
+ if (word.length) [words addObject:word];
+ }
+ return [words componentsJoinedByString:@" "];
+}
+
+// done gets Musixmatch's status for the paging: 200 once it is over, the first other one where it stopped,
+// 0 for a page that never came.
+static void translationPage(NSString *token, NSString *commontrack, NSString *code, NSInteger page,
+ NSMutableDictionary<NSString *, NSString *> *found, void (^done)(NSInteger status)) {
+ call(@"crowd.track.translations.get", @{
+ @"usertoken": token, @"commontrack_id": commontrack, @"selected_language": code,
+ @"comment_format": @"text", @"part": @"user", @"translation_fields_set": @"minimal",
+ @"page": @(page).stringValue, @"page_size": @(kTranslationPage).stringValue,
+ }, ^(NSDictionary *message) {
+ NSInteger status = [dig(message, @"header/status_code") integerValue];
+ if (status != 200) {
+ done(status);
+ return;
+ }
+ id list = dig(message, @"body/translations_list");
+ NSUInteger before = found.count;
+ for (id entry in [list isKindOfClass:NSArray.class] ? list : @[]) {
+ id snippet = dig(entry, @"translation/snippet"), text = dig(entry, @"translation/description");
+ if (![snippet isKindOfClass:NSString.class] || ![text isKindOfClass:NSString.class] || ![text length]) continue;
+ NSString *key = SGMusixmatchLineKey(snippet);
+ if (key.length && ![text isEqualToString:snippet]) found[key] = text;
+ }
+ BOOL more = found.count > before && [list count] >= 20 && page < kTranslationPages;
+ if (more) translationPage(token, commontrack, code, page + 1, found, done);
+ else done(200);
+ });
+}
+
+// Each of the language's codes in turn, until one has lines.
+static void translationsIn(NSString *token, NSString *commontrack, NSArray<NSString *> *codes,
+ NSMutableDictionary<NSString *, NSString *> *found, void (^done)(NSInteger status)) {
+ translationPage(token, commontrack, codes.firstObject, 1, found, ^(NSInteger status) {
+ if (status == 200 && !found.count && codes.count > 1) {
+ translationsIn(token, commontrack, [codes subarrayWithRange:NSMakeRange(1, codes.count - 1)], found, done);
+ return;
+ }
+ done(status);
+ });
+}
+
+// done gets what was found, and whether it is Musixmatch's whole answer rather than one cut short.
+static void askTranslations(NSString *trackID, NSString *language, BOOL renewToken,
+ void (^done)(NSDictionary<NSString *, NSString *> *found, BOOL answered)) {
+ withToken(^(NSString *token) {
+ if (!token) {
+ done(nil, NO);
+ return;
+ }
+ // As ask() does: a token Musixmatch no longer takes is dropped and a new one asked for, once.
+ void (^ended)(NSInteger, NSDictionary *) = ^(NSInteger status, NSDictionary *found) {
+ if (status == 401 && renewToken) {
+ SGLog(@"musixmatch: token no longer accepted for translations, asking for a new one");
+ [NSUserDefaults.standardUserDefaults removeObjectForKey:kTokenKey];
+ askTranslations(trackID, language, NO, done);
+ return;
+ }
+ done(found, !lostStatus(status) && status != 401);
+ };
+ void (^translate)(NSString *) = ^(NSString *commontrack) {
+ NSMutableDictionary<NSString *, NSString *> *found = [NSMutableDictionary dictionary];
+ translationsIn(token, commontrack, codesFor(language), found, ^(NSInteger status) { ended(status, found); });
+ };
+ // The lyrics call has usually matched the track already; the matcher is asked only when it has not.
+ NSString *known = sg_commontracks[trackID];
+ if (known) {
+ translate(known);
+ return;
+ }
+ call(@"matcher.track.get", @{@"usertoken": token, @"track_spotify_id": trackID}, ^(NSDictionary *message) {
+ NSInteger status = [dig(message, @"header/status_code") integerValue];
+ noteCommontrack(trackID, dig(message, @"body/track/commontrack_id"));
+ NSString *commontrack = status == 200 ? sg_commontracks[trackID] : nil;
+ if (commontrack) translate(commontrack);
+ else ended(status, nil);
+ });
+ });
+}
+
+void SGMusixmatchTranslations(NSString *trackID, NSString *language, void (^done)(NSDictionary<NSString *, NSString *> *byLine)) {
+ setUp();
+ if (!trackID.length || !language.length) {
+ done(nil);
+ return;
+ }
+ NSString *memo = [NSString stringWithFormat:@"%@|%@", trackID, language];
+ NSDictionary *known = sg_translations[memo];
+ if (known) {
+ done(known.count ? known : nil);
+ return;
+ }
+ NSMutableArray *waiting = sg_translationsWaiting[memo];
+ if (waiting) {
+ [waiting addObject:[done copy]];
+ return;
+ }
+ sg_translationsWaiting[memo] = [NSMutableArray arrayWithObject:[done copy]];
+ askTranslations(trackID, language, YES, ^(NSDictionary<NSString *, NSString *> *found, BOOL answered) {
+ // Only a whole answer is kept: a page lost on the way leaves the track to be asked again.
+ if (answered) {
+ if (sg_translations.count >= kKeptTracks) [sg_translations removeAllObjects];
+ sg_translations[memo] = [found copy] ?: @{};
+ }
+ SGLog(@"musixmatch: %lu translated lines in %@ for %@%@", (unsigned long)found.count, language, trackID,
+ answered ? @"" : @", cut short; not kept, so the next lines ask again");
+ NSArray *waiters = sg_translationsWaiting[memo];
+ [sg_translationsWaiting removeObjectForKey:memo];
+ for (void (^waiter)(NSDictionary *) in waiters) waiter(found.count ? found : nil);
+ });
+}
+
+NSArray<SGKaraokeLine *> *SGMusixmatchTranslatedLines(NSArray<SGKaraokeLine *> *lines, NSDictionary<NSString *, NSString *> *byLine) {
+ NSMutableArray<SGKaraokeLine *> *translated = nil;
+ for (NSUInteger i = 0; i < lines.count; i++) {
+ SGKaraokeLine *line = lines[i];
+ NSString *text = byLine[SGMusixmatchLineKey(SGKaraokeLineText(line))];
+ if (!text.length || line.translation.length) continue;
+ if (!translated) translated = [lines mutableCopy];
+ SGKaraokeLine *copy = [line copy];
+ copy.translation = text;
+ translated[i] = copy;
+ }
+ return translated;
+}