update netease translation lyric line handling (#979)

- lyric should be appended to the original lyric line with a custom splitter
- the custom splitter is now handled in LyricLine
This commit is contained in:
jeffvli 2025-06-29 18:29:59 -07:00
parent b6d902e425
commit df6b6d514d
2 changed files with 19 additions and 14 deletions

View file

@ -201,25 +201,23 @@ function mergeLyrics(original: string | undefined, translated: string | undefine
return original; return original;
} }
// Iterate through each line of the original LRC. If a translation exists for // Iterate through each line of the original LRC. If a translation exists for the same timestamp, append the translated text after the original text.
// the same timestamp, insert it as a new, fully-formatted LRC line. const finalLines = original.split('\n').map((line) => {
const finalLines = original.split('\n').flatMap((line) => {
const match = line.match(lrcLineRegex); const match = line.match(lrcLineRegex);
if (match) { if (match) {
const timestamp = match[1]; const timestamp = match[1];
const originalText = match[2].trim();
const translatedText = translatedMap.get(timestamp); const translatedText = translatedMap.get(timestamp);
if (translatedText) { if (translatedText && originalText) {
// Return an array containing both the original line and the new translated line. // Append and add a break delimiter to separate the original and translated text
// flatMap will flatten this into the final array of lines. return [`[${timestamp}]${originalText}`, translatedText].join('_BREAK_');
const translatedLine = `[${timestamp}]${translatedText}`;
return [line, translatedLine];
} }
} }
// If no match or no translation is found, return only the original line. // If no match or no translation is found, return the original line unchanged.
return [line]; return line;
}); });
return finalLines.join('\n'); return finalLines.join('\n');

View file

@ -3,7 +3,8 @@ import { ComponentPropsWithoutRef } from 'react';
import styles from './lyric-line.module.css'; import styles from './lyric-line.module.css';
import { TextTitle } from '/@/shared/components/text-title/text-title'; import { Box } from '/@/shared/components/box/box';
import { Stack } from '/@/shared/components/stack/stack';
interface LyricLineProps extends ComponentPropsWithoutRef<'div'> { interface LyricLineProps extends ComponentPropsWithoutRef<'div'> {
alignment: 'center' | 'left' | 'right'; alignment: 'center' | 'left' | 'right';
@ -12,8 +13,10 @@ interface LyricLineProps extends ComponentPropsWithoutRef<'div'> {
} }
export const LyricLine = ({ alignment, className, fontSize, text, ...props }: LyricLineProps) => { export const LyricLine = ({ alignment, className, fontSize, text, ...props }: LyricLineProps) => {
const lines = text.split('_BREAK_');
return ( return (
<TextTitle <Box
className={clsx(styles.lyricLine, className)} className={clsx(styles.lyricLine, className)}
style={{ style={{
fontSize, fontSize,
@ -21,7 +24,11 @@ export const LyricLine = ({ alignment, className, fontSize, text, ...props }: Ly
}} }}
{...props} {...props}
> >
{text} <Stack gap={0}>
</TextTitle> {lines.map((line, index) => (
<span key={index}>{line}</span>
))}
</Stack>
</Box>
); );
}; };