From 08a96244f3147dec800a86c2497286fe4bd9ed52 Mon Sep 17 00:00:00 2001 From: DrENDzZ Date: Fri, 13 Sep 2024 23:40:18 +0800 Subject: [PATCH 01/17] modified: package.json modified: src/i18n/locales/en.json modified: src/renderer/features/lyrics/lyrics-actions.tsx modified: src/renderer/features/lyrics/lyrics.tsx modified: src/renderer/features/player/components/visualizer.tsx --- package.json | 2 +- src/i18n/locales/en.json | 2 ++ .../features/lyrics/lyrics-actions.tsx | 31 ++++++++++++++++++- src/renderer/features/lyrics/lyrics.tsx | 19 +++++++++++- .../features/player/components/visualizer.tsx | 4 +-- 5 files changed, 53 insertions(+), 5 deletions(-) diff --git a/package.json b/package.json index 17153a39c..c7a1c2984 100644 --- a/package.json +++ b/package.json @@ -309,8 +309,8 @@ "@tanstack/react-query-persist-client": "^4.32.1", "@ts-rest/core": "^3.23.0", "@xhayper/discord-rpc": "^1.0.24", - "auto-text-size": "^0.2.3", "audiomotion-analyzer": "^4.5.0", + "auto-text-size": "^0.2.3", "axios": "^1.6.0", "clsx": "^2.0.0", "cmdk": "^0.2.0", diff --git a/src/i18n/locales/en.json b/src/i18n/locales/en.json index 1e759751d..d316b0ce2 100644 --- a/src/i18n/locales/en.json +++ b/src/i18n/locales/en.json @@ -95,6 +95,7 @@ "resetToDefault": "reset to default", "restartRequired": "restart required", "right": "right", + "romaji": "romaji", "save": "save", "saveAndReplace": "save and replace", "saveAs": "save as", @@ -109,6 +110,7 @@ "trackNumber": "track", "trackGain": "track gain", "trackPeak": "track peak", + "translation": "translation", "unknown": "unknown", "version": "version", "year": "year", diff --git a/src/renderer/features/lyrics/lyrics-actions.tsx b/src/renderer/features/lyrics/lyrics-actions.tsx index b9edc7088..4b7a7ea62 100644 --- a/src/renderer/features/lyrics/lyrics-actions.tsx +++ b/src/renderer/features/lyrics/lyrics-actions.tsx @@ -18,7 +18,9 @@ interface LyricsActionsProps { onRemoveLyric: () => void; onResetLyric: () => void; + onRomajiLyric: () => void; onSearchOverride: (params: LyricsOverride) => void; + onTranslationLyric: () => void; setIndex: (idx: number) => void; } @@ -27,7 +29,9 @@ export const LyricsActions = ({ languages, onRemoveLyric, onResetLyric, + onRomajiLyric, onSearchOverride, + onTranslationLyric, setIndex, }: LyricsActionsProps) => { const { t } = useTranslation(); @@ -120,7 +124,6 @@ export const LyricsActions = ({ {isDesktop && sources.length ? ( + ) : null} + + + + {isDesktop && sources.length ? ( + + ) : null} + ); }; diff --git a/src/renderer/features/lyrics/lyrics.tsx b/src/renderer/features/lyrics/lyrics.tsx index 1cf276a1d..a796e90cb 100644 --- a/src/renderer/features/lyrics/lyrics.tsx +++ b/src/renderer/features/lyrics/lyrics.tsx @@ -96,6 +96,9 @@ export const Lyrics = () => { const [override, setOverride] = useState(undefined); + const [isTranslated, setIsTranslated] = useState(false); + const [translatedLyrics] = useState(null); + const handleOnSearchOverride = useCallback((params: LyricsOverride) => { setOverride(params); }, []); @@ -123,6 +126,12 @@ export const Lyrics = () => { ); }, [currentSong?.id, currentSong?.serverId]); + const handleOnRomajiLyric = useCallback(() => {}, []); + + const handleOnTranslationLyric = useCallback(() => { + setIsTranslated((prev) => !prev); + }, []); + const { isInitialLoading: isOverrideLoading } = useSongLyricsByRemoteId({ options: { enabled: !!override, @@ -203,10 +212,16 @@ export const Lyrics = () => { transition={{ duration: 0.5 }} > {synced ? ( - + ) : ( )} @@ -220,7 +235,9 @@ export const Lyrics = () => { setIndex={setIndex} onRemoveLyric={handleOnRemoveLyric} onResetLyric={handleOnResetLyric} + onRomajiLyric={handleOnRomajiLyric} onSearchOverride={handleOnSearchOverride} + onTranslationLyric={handleOnTranslationLyric} /> diff --git a/src/renderer/features/player/components/visualizer.tsx b/src/renderer/features/player/components/visualizer.tsx index 8709f5a72..3e633eec6 100644 --- a/src/renderer/features/player/components/visualizer.tsx +++ b/src/renderer/features/player/components/visualizer.tsx @@ -5,12 +5,12 @@ import styled from 'styled-components'; import { useSettingsStore } from '/@/renderer/store'; const StyledContainer = styled.div` - margin: auto; max-width: 100%; + margin: auto; canvas { - margin: auto; width: 100%; + margin: auto; } `; From f63dc01d1d15b7080734255cc0d08cfdb625dec8 Mon Sep 17 00:00:00 2001 From: ENDzZ Date: Sun, 15 Sep 2024 23:56:48 +0800 Subject: [PATCH 02/17] Lyrics Translation Settings Init and Typo Revert --- src/renderer/features/lyrics/lyrics.tsx | 15 +----- .../components/playback/lyric-settings.tsx | 53 +++++++++++++++++++ src/renderer/store/settings.store.ts | 8 +++ 3 files changed, 63 insertions(+), 13 deletions(-) diff --git a/src/renderer/features/lyrics/lyrics.tsx b/src/renderer/features/lyrics/lyrics.tsx index a796e90cb..012db1ec9 100644 --- a/src/renderer/features/lyrics/lyrics.tsx +++ b/src/renderer/features/lyrics/lyrics.tsx @@ -96,9 +96,6 @@ export const Lyrics = () => { const [override, setOverride] = useState(undefined); - const [isTranslated, setIsTranslated] = useState(false); - const [translatedLyrics] = useState(null); - const handleOnSearchOverride = useCallback((params: LyricsOverride) => { setOverride(params); }, []); @@ -128,9 +125,7 @@ export const Lyrics = () => { const handleOnRomajiLyric = useCallback(() => {}, []); - const handleOnTranslationLyric = useCallback(() => { - setIsTranslated((prev) => !prev); - }, []); + const handleOnTranslationLyric = useCallback(() => {}, []); const { isInitialLoading: isOverrideLoading } = useSongLyricsByRemoteId({ options: { @@ -212,16 +207,10 @@ export const Lyrics = () => { transition={{ duration: 0.5 }} > {synced ? ( - + ) : ( )} diff --git a/src/renderer/features/settings/components/playback/lyric-settings.tsx b/src/renderer/features/settings/components/playback/lyric-settings.tsx index 157536667..7ff89d854 100644 --- a/src/renderer/features/settings/components/playback/lyric-settings.tsx +++ b/src/renderer/features/settings/components/playback/lyric-settings.tsx @@ -8,6 +8,7 @@ import isElectron from 'is-electron'; import styled from 'styled-components'; import { LyricSource } from '/@/renderer/api/types'; import { useTranslation } from 'react-i18next'; +import { TextInput, Select } from '@mantine/core'; const localSettings = isElectron() ? window.electron.localSettings : null; @@ -116,6 +117,58 @@ export const LyricSettings = () => { isHidden: !isElectron(), title: t('setting.lyricOffset', { postProcess: 'sentenceCase' }), }, + { + control: ( + { + setSettings({ lyrics: { ...settings, targetLanguage: value } }); + }} + /> + ), + description: t('Target language for translation'), + title: t('Target Language'), + }, ]; return ( diff --git a/src/renderer/store/settings.store.ts b/src/renderer/store/settings.store.ts index db64decec..f935e298e 100644 --- a/src/renderer/store/settings.store.ts +++ b/src/renderer/store/settings.store.ts @@ -265,6 +265,8 @@ export interface SettingsState { }; lyrics: { alignment: 'left' | 'center' | 'right'; + apiKey: string | number | readonly string[] | undefined; + apiProvider: string | null | undefined; delayMs: number; fetch: boolean; follow: boolean; @@ -272,9 +274,11 @@ export interface SettingsState { fontSizeUnsync: number; gap: number; gapUnsync: number; + resourceLocation: string | number | readonly string[] | undefined; showMatch: boolean; showProvider: boolean; sources: LyricSource[]; + targetLanguage: string | null | undefined; }; playback: { audioDeviceId?: string | null; @@ -439,6 +443,8 @@ const initialState: SettingsState = { }, lyrics: { alignment: 'center', + apiKey: undefined, + apiProvider: undefined, delayMs: 0, fetch: false, follow: true, @@ -446,9 +452,11 @@ const initialState: SettingsState = { fontSizeUnsync: 20, gap: 5, gapUnsync: 0, + resourceLocation: undefined, showMatch: true, showProvider: true, sources: [], + targetLanguage: undefined, }, playback: { audioDeviceId: undefined, From a0f1d566b0fec762f4bd0552f99d19de3316744c Mon Sep 17 00:00:00 2001 From: ENDzZ Date: Mon, 16 Sep 2024 21:32:28 +0800 Subject: [PATCH 03/17] Lib Type Add and Default Value --- package-lock.json | 115 ++++++++++++++++++ package.json | 3 + .../components/playback/lyric-settings.tsx | 40 ++---- src/renderer/store/settings.store.ts | 2 +- 4 files changed, 132 insertions(+), 28 deletions(-) diff --git a/package-lock.json b/package-lock.json index c94229aee..e550cd843 100644 --- a/package-lock.json +++ b/package-lock.json @@ -23,6 +23,8 @@ "@mantine/modals": "^6.0.17", "@mantine/notifications": "^6.0.17", "@mantine/utils": "^6.0.17", + "@sglkc/kuroshiro": "^1.0.1", + "@sglkc/kuroshiro-analyzer-kuromoji": "^1.0.1", "@tanstack/react-query": "^4.32.1", "@tanstack/react-query-devtools": "^4.32.1", "@tanstack/react-query-persist-client": "^4.32.1", @@ -96,6 +98,7 @@ "@types/react-window-infinite-loader": "^1.0.6", "@types/styled-components": "^5.1.26", "@types/terser-webpack-plugin": "^5.0.4", + "@types/uuid": "^10.0.0", "@types/webpack-bundle-analyzer": "^4.4.1", "@types/webpack-env": "^1.16.3", "@typescript-eslint/eslint-plugin": "^5.47.0", @@ -4656,6 +4659,47 @@ "node": ">=14.0.0" } }, + "node_modules/@sglkc/kuromoji": { + "version": "1.1.0", + "resolved": "https://registry.npmjs.org/@sglkc/kuromoji/-/kuromoji-1.1.0.tgz", + "integrity": "sha512-uKxXYUM5XNjZw0ia0yMYTI3jHSh/c1gEMNvcvS6J49B4mI+PAq8iRYPTxh2ma0RWm45LyWWrILxCJAIXpi/pww==", + "license": "Apache-2.0", + "dependencies": { + "async": "^2.0.1", + "doublearray": "0.0.2", + "fflate": "^0.8.0" + } + }, + "node_modules/@sglkc/kuromoji/node_modules/async": { + "version": "2.6.4", + "resolved": "https://registry.npmjs.org/async/-/async-2.6.4.tgz", + "integrity": "sha512-mzo5dfJYwAn29PeiJ0zvwTo04zj8HDJj0Mn8TD7sno7q12prdbnasKJHhkm2c1LgrhlJ0teaea8860oxi51mGA==", + "license": "MIT", + "dependencies": { + "lodash": "^4.17.14" + } + }, + "node_modules/@sglkc/kuroshiro": { + "version": "1.0.1", + "resolved": "https://registry.npmjs.org/@sglkc/kuroshiro/-/kuroshiro-1.0.1.tgz", + "integrity": "sha512-pDjzQa+KHcoKas5i0nf2uxNh0cFZlgsKzvj5AHMkAF7cvKTayN+Rllb/X+S3Gz8MpMk6pV95Uyu90+iyq3w+/A==", + "license": "MIT", + "dependencies": { + "@babel/runtime": "^7.14.0" + }, + "engines": { + "node": ">=6.5.0" + } + }, + "node_modules/@sglkc/kuroshiro-analyzer-kuromoji": { + "version": "1.0.1", + "resolved": "https://registry.npmjs.org/@sglkc/kuroshiro-analyzer-kuromoji/-/kuroshiro-analyzer-kuromoji-1.0.1.tgz", + "integrity": "sha512-qWEhhmru2T6YKhOx1g6BNxxm8K0GIKyd5NY6ynrGH/lLwxNvfv2osh2PAeoSJhjloVrLe7wn/NEp/5+IoRN88A==", + "license": "MIT", + "dependencies": { + "@sglkc/kuromoji": "^1.0.1" + } + }, "node_modules/@sindresorhus/is": { "version": "4.6.0", "resolved": "https://registry.npmjs.org/@sindresorhus/is/-/is-4.6.0.tgz", @@ -5518,6 +5562,13 @@ "dev": true, "license": "MIT" }, + "node_modules/@types/uuid": { + "version": "10.0.0", + "resolved": "https://registry.npmjs.org/@types/uuid/-/uuid-10.0.0.tgz", + "integrity": "sha512-7gqG38EyHgyP1S+7+xomFtL+ZNHcKv6DwNaCZmJmo1vgMugyF3TCnXVg4t1uk89mLNwnLtnY3TpOpCOyp1/xHQ==", + "dev": true, + "license": "MIT" + }, "node_modules/@types/verror": { "version": "1.10.10", "resolved": "https://registry.npmjs.org/@types/verror/-/verror-1.10.10.tgz", @@ -9741,6 +9792,12 @@ "integrity": "sha512-YXQl1DSa4/PQyRfgrv6aoNjhasp/p4qs9FjJ4q4cQk+8m4r6k4ZSiEyytKG8f8W9gi8WsQtIObNmKd+tMzNTmA==", "dev": true }, + "node_modules/doublearray": { + "version": "0.0.2", + "resolved": "https://registry.npmjs.org/doublearray/-/doublearray-0.0.2.tgz", + "integrity": "sha512-aw55FtZzT6AmiamEj2kvmR6BuFqvYgKZUkfQ7teqVRNqD5UE0rw8IeW/3gieHNKQ5sPuDKlljWEn4bzv5+1bHw==", + "license": "MIT" + }, "node_modules/duplexer": { "version": "0.1.2", "resolved": "https://registry.npmjs.org/duplexer/-/duplexer-0.1.2.tgz", @@ -12218,6 +12275,12 @@ "pend": "~1.2.0" } }, + "node_modules/fflate": { + "version": "0.8.2", + "resolved": "https://registry.npmjs.org/fflate/-/fflate-0.8.2.tgz", + "integrity": "sha512-cPJU47OaAoCbg0pBvzsgpTPhmhqI5eJjh/JIu8tPj5q+T7iLvW/JAYUqmE7KOB4R1ZyEhzBaIQpQpardBF5z8A==", + "license": "MIT" + }, "node_modules/file-entry-cache": { "version": "6.0.1", "resolved": "https://registry.npmjs.org/file-entry-cache/-/file-entry-cache-6.0.1.tgz", @@ -27051,6 +27114,42 @@ "resolved": "https://registry.npmjs.org/@remix-run/router/-/router-1.9.0.tgz", "integrity": "sha512-bV63itrKBC0zdT27qYm6SDZHlkXwFL1xMBuhkn+X7l0+IIhNaH5wuuvZKp6eKhCD4KFhujhfhCT1YxXW6esUIA==" }, + "@sglkc/kuromoji": { + "version": "1.1.0", + "resolved": "https://registry.npmjs.org/@sglkc/kuromoji/-/kuromoji-1.1.0.tgz", + "integrity": "sha512-uKxXYUM5XNjZw0ia0yMYTI3jHSh/c1gEMNvcvS6J49B4mI+PAq8iRYPTxh2ma0RWm45LyWWrILxCJAIXpi/pww==", + "requires": { + "async": "^2.0.1", + "doublearray": "0.0.2", + "fflate": "^0.8.0" + }, + "dependencies": { + "async": { + "version": "2.6.4", + "resolved": "https://registry.npmjs.org/async/-/async-2.6.4.tgz", + "integrity": "sha512-mzo5dfJYwAn29PeiJ0zvwTo04zj8HDJj0Mn8TD7sno7q12prdbnasKJHhkm2c1LgrhlJ0teaea8860oxi51mGA==", + "requires": { + "lodash": "^4.17.14" + } + } + } + }, + "@sglkc/kuroshiro": { + "version": "1.0.1", + "resolved": "https://registry.npmjs.org/@sglkc/kuroshiro/-/kuroshiro-1.0.1.tgz", + "integrity": "sha512-pDjzQa+KHcoKas5i0nf2uxNh0cFZlgsKzvj5AHMkAF7cvKTayN+Rllb/X+S3Gz8MpMk6pV95Uyu90+iyq3w+/A==", + "requires": { + "@babel/runtime": "^7.14.0" + } + }, + "@sglkc/kuroshiro-analyzer-kuromoji": { + "version": "1.0.1", + "resolved": "https://registry.npmjs.org/@sglkc/kuroshiro-analyzer-kuromoji/-/kuroshiro-analyzer-kuromoji-1.0.1.tgz", + "integrity": "sha512-qWEhhmru2T6YKhOx1g6BNxxm8K0GIKyd5NY6ynrGH/lLwxNvfv2osh2PAeoSJhjloVrLe7wn/NEp/5+IoRN88A==", + "requires": { + "@sglkc/kuromoji": "^1.0.1" + } + }, "@sindresorhus/is": { "version": "4.6.0", "resolved": "https://registry.npmjs.org/@sindresorhus/is/-/is-4.6.0.tgz", @@ -27801,6 +27900,12 @@ "integrity": "sha512-ScaPdn1dQczgbl0QFTeTOmVHFULt394XJgOQNoyVhZ6r2vLnMLJfBPd53SB52T/3G36VI1/g2MZaX0cwDuXsfw==", "dev": true }, + "@types/uuid": { + "version": "10.0.0", + "resolved": "https://registry.npmjs.org/@types/uuid/-/uuid-10.0.0.tgz", + "integrity": "sha512-7gqG38EyHgyP1S+7+xomFtL+ZNHcKv6DwNaCZmJmo1vgMugyF3TCnXVg4t1uk89mLNwnLtnY3TpOpCOyp1/xHQ==", + "dev": true + }, "@types/verror": { "version": "1.10.10", "resolved": "https://registry.npmjs.org/@types/verror/-/verror-1.10.10.tgz", @@ -30882,6 +30987,11 @@ "integrity": "sha512-YXQl1DSa4/PQyRfgrv6aoNjhasp/p4qs9FjJ4q4cQk+8m4r6k4ZSiEyytKG8f8W9gi8WsQtIObNmKd+tMzNTmA==", "dev": true }, + "doublearray": { + "version": "0.0.2", + "resolved": "https://registry.npmjs.org/doublearray/-/doublearray-0.0.2.tgz", + "integrity": "sha512-aw55FtZzT6AmiamEj2kvmR6BuFqvYgKZUkfQ7teqVRNqD5UE0rw8IeW/3gieHNKQ5sPuDKlljWEn4bzv5+1bHw==" + }, "duplexer": { "version": "0.1.2", "resolved": "https://registry.npmjs.org/duplexer/-/duplexer-0.1.2.tgz", @@ -32688,6 +32798,11 @@ "pend": "~1.2.0" } }, + "fflate": { + "version": "0.8.2", + "resolved": "https://registry.npmjs.org/fflate/-/fflate-0.8.2.tgz", + "integrity": "sha512-cPJU47OaAoCbg0pBvzsgpTPhmhqI5eJjh/JIu8tPj5q+T7iLvW/JAYUqmE7KOB4R1ZyEhzBaIQpQpardBF5z8A==" + }, "file-entry-cache": { "version": "6.0.1", "resolved": "https://registry.npmjs.org/file-entry-cache/-/file-entry-cache-6.0.1.tgz", diff --git a/package.json b/package.json index c7a1c2984..06659b000 100644 --- a/package.json +++ b/package.json @@ -219,6 +219,7 @@ "@types/react-window-infinite-loader": "^1.0.6", "@types/styled-components": "^5.1.26", "@types/terser-webpack-plugin": "^5.0.4", + "@types/uuid": "^10.0.0", "@types/webpack-bundle-analyzer": "^4.4.1", "@types/webpack-env": "^1.16.3", "@typescript-eslint/eslint-plugin": "^5.47.0", @@ -304,6 +305,8 @@ "@mantine/modals": "^6.0.17", "@mantine/notifications": "^6.0.17", "@mantine/utils": "^6.0.17", + "@sglkc/kuroshiro": "^1.0.1", + "@sglkc/kuroshiro-analyzer-kuromoji": "^1.0.1", "@tanstack/react-query": "^4.32.1", "@tanstack/react-query-devtools": "^4.32.1", "@tanstack/react-query-persist-client": "^4.32.1", diff --git a/src/renderer/features/settings/components/playback/lyric-settings.tsx b/src/renderer/features/settings/components/playback/lyric-settings.tsx index 7ff89d854..d6fce37a8 100644 --- a/src/renderer/features/settings/components/playback/lyric-settings.tsx +++ b/src/renderer/features/settings/components/playback/lyric-settings.tsx @@ -117,6 +117,19 @@ export const LyricSettings = () => { isHidden: !isElectron(), title: t('setting.lyricOffset', { postProcess: 'sentenceCase' }), }, + { + control: ( + { description: t('API Key for translation'), title: t('API Key'), }, - { - control: ( - { - setSettings({ - lyrics: { ...settings, resourceLocation: e.currentTarget.value }, - }); - }} - /> - ), - description: t('Resource location for translation'), - title: t('Resource Location'), - }, - { - control: ( - { setSettings({ lyrics: { ...settings, targetLanguage: value } }); @@ -128,7 +129,7 @@ export const LyricSettings = () => { /> ), description: t('Target language for translation'), - title: t('Target Language'), + title: t('setting.targetLanguage', { postProcess: 'sentenceCase' }), }, { control: ( @@ -141,7 +142,7 @@ export const LyricSettings = () => { /> ), description: t('API provider for translation'), - title: t('API Provider'), + title: t('setting.apiProvider', { postProcess: 'sentenceCase' }), }, { control: ( @@ -153,7 +154,7 @@ export const LyricSettings = () => { /> ), description: t('API Key for translation'), - title: t('API Key'), + title: t('setting.apiKey', { postProcess: 'sentenceCase' }), }, ]; From 0bc8d843a7eb3e8b00e345add718251e179daa5b Mon Sep 17 00:00:00 2001 From: DrENDzZ Date: Thu, 19 Sep 2024 23:04:07 +0800 Subject: [PATCH 09/17] Fallback Fix Dot in Path Error --- .erb/configs/webpack.config.renderer.dev.ts | 1 + src/renderer/features/lyrics/lyrics.tsx | 6 +++--- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/.erb/configs/webpack.config.renderer.dev.ts b/.erb/configs/webpack.config.renderer.dev.ts index 46e51530d..9474b7fd0 100644 --- a/.erb/configs/webpack.config.renderer.dev.ts +++ b/.erb/configs/webpack.config.renderer.dev.ts @@ -163,6 +163,7 @@ const configuration: webpack.Configuration = { publicPath: '/', }, historyApiFallback: { + disableDotRule: true, verbose: true, }, setupMiddlewares(middlewares) { diff --git a/src/renderer/features/lyrics/lyrics.tsx b/src/renderer/features/lyrics/lyrics.tsx index 5c3a6cdef..42f9a2c95 100644 --- a/src/renderer/features/lyrics/lyrics.tsx +++ b/src/renderer/features/lyrics/lyrics.tsx @@ -150,12 +150,12 @@ export const Lyrics = () => { setShowRomaji(!showRomaji); return; } - const kuroshiro = new Kuroshiro(); - await kuroshiro.init(new KuromojiAnalyzer()); if (!lyrics) return; const originalLyrics = Array.isArray(lyrics.lyrics) ? lyrics.lyrics.map(([, line]) => line).join('\n') : lyrics.lyrics; + const kuroshiro = new Kuroshiro(); + await kuroshiro.init(new KuromojiAnalyzer()); const romanizedText = await kuroshiro.convert(originalLyrics, { to: 'romaji' }); setRomanizedLyrics(romanizedText); setShowRomaji(true); @@ -166,11 +166,11 @@ export const Lyrics = () => { setShowTranslation(!showTranslation); return; } - const { apiKey, targetLanguage } = lyricsSettings; if (!lyrics) return; const originalLyrics = Array.isArray(lyrics.lyrics) ? lyrics.lyrics.map(([, line]) => line).join('\n') : lyrics.lyrics; + const { apiKey, targetLanguage } = lyricsSettings; const response = await axios({ baseURL: 'https://api.cognitive.microsofttranslator.com', data: [ From 8d796ae748a6b6f3455b1d983a7ef138f84e916d Mon Sep 17 00:00:00 2001 From: DrENDzZ Date: Fri, 20 Sep 2024 01:35:03 +0800 Subject: [PATCH 10/17] Final Lint for Translation Module --- release/app/package-lock.json | 3 ++- src/renderer/features/lyrics/lyrics.tsx | 11 +---------- 2 files changed, 3 insertions(+), 11 deletions(-) diff --git a/release/app/package-lock.json b/release/app/package-lock.json index 2396932a2..b7bcb4af1 100644 --- a/release/app/package-lock.json +++ b/release/app/package-lock.json @@ -2311,7 +2311,8 @@ "ws": { "version": "8.18.0", "resolved": "https://registry.npmjs.org/ws/-/ws-8.18.0.tgz", - "integrity": "sha512-8VbfWfHLbbwu3+N6OKsOMpBdT4kXPDDB9cJk2bJ6mh9ucxdlnNvH1e+roYkKmN9Nxw2yjz7VzeO9oOz2zJ04Pw==" + "integrity": "sha512-8VbfWfHLbbwu3+N6OKsOMpBdT4kXPDDB9cJk2bJ6mh9ucxdlnNvH1e+roYkKmN9Nxw2yjz7VzeO9oOz2zJ04Pw==", + "requires": {} }, "xml2js": { "version": "0.4.23", diff --git a/src/renderer/features/lyrics/lyrics.tsx b/src/renderer/features/lyrics/lyrics.tsx index 42f9a2c95..3d37b7bd2 100644 --- a/src/renderer/features/lyrics/lyrics.tsx +++ b/src/renderer/features/lyrics/lyrics.tsx @@ -7,7 +7,6 @@ import styled from 'styled-components'; import Kuroshiro from '@sglkc/kuroshiro'; import KuromojiAnalyzer from '@sglkc/kuroshiro-analyzer-kuromoji'; import axios from 'axios'; -import { v4 as uuidv4 } from 'uuid'; import { useSongLyricsByRemoteId, useSongLyricsBySong } from './queries/lyric-query'; import { SynchronizedLyrics, SynchronizedLyricsProps } from './synchronized-lyrics'; import { Spinner, TextTitle } from '/@/renderer/components'; @@ -172,24 +171,16 @@ export const Lyrics = () => { : lyrics.lyrics; const { apiKey, targetLanguage } = lyricsSettings; const response = await axios({ - baseURL: 'https://api.cognitive.microsofttranslator.com', data: [ { text: originalLyrics, }, ], headers: { - 'Content-type': 'application/json', 'Ocp-Apim-Subscription-Key': apiKey as string, - 'X-ClientTraceId': uuidv4().toString(), }, method: 'post', - params: { - 'api-version': '3.0', - to: targetLanguage, - }, - responseType: 'json', - url: '/translate', + url: `https://api.cognitive.microsofttranslator.com/translate?api-version=3.0&to=${targetLanguage}`, }); const TranslatedText = response.data[0].translations[0].text; setTranslatedLyrics(TranslatedText); From 83b981e97b4ab4be786926191b710dd56d12189b Mon Sep 17 00:00:00 2001 From: DrENDzZ Date: Fri, 20 Sep 2024 19:05:07 +0800 Subject: [PATCH 11/17] Add Google Cloud Translation API Support --- src/renderer/features/lyrics/lyrics.tsx | 43 +++++++++++++------ .../components/playback/lyric-settings.tsx | 6 +-- src/renderer/store/settings.store.ts | 12 +++--- 3 files changed, 38 insertions(+), 23 deletions(-) diff --git a/src/renderer/features/lyrics/lyrics.tsx b/src/renderer/features/lyrics/lyrics.tsx index 3d37b7bd2..5f2b73413 100644 --- a/src/renderer/features/lyrics/lyrics.tsx +++ b/src/renderer/features/lyrics/lyrics.tsx @@ -169,20 +169,37 @@ export const Lyrics = () => { const originalLyrics = Array.isArray(lyrics.lyrics) ? lyrics.lyrics.map(([, line]) => line).join('\n') : lyrics.lyrics; - const { apiKey, targetLanguage } = lyricsSettings; - const response = await axios({ - data: [ - { - text: originalLyrics, + const { apiKey, apiProvider, targetLanguage } = lyricsSettings; + let TranslatedText = ''; + if (apiProvider === 'Microsoft Azure') { + const response = await axios({ + data: [ + { + Text: originalLyrics, + }, + ], + headers: { + 'Content-Type': 'application/json', + 'Ocp-Apim-Subscription-Key': apiKey, }, - ], - headers: { - 'Ocp-Apim-Subscription-Key': apiKey as string, - }, - method: 'post', - url: `https://api.cognitive.microsofttranslator.com/translate?api-version=3.0&to=${targetLanguage}`, - }); - const TranslatedText = response.data[0].translations[0].text; + method: 'post', + url: `https://api.cognitive.microsofttranslator.com/translate?api-version=3.0&to=${targetLanguage as string}`, + }); + TranslatedText = response.data[0].translations[0].text; + } else if (apiProvider === 'Google Cloud') { + const response = await axios({ + data: { + format: 'text', + q: originalLyrics, + }, + headers: { + 'Content-Type': 'application/json', + }, + method: 'post', + url: `https://translation.googleapis.com/language/translate/v2?target=${targetLanguage as string}&key=${apiKey}`, + }); + TranslatedText = response.data.data.translations[0].translatedText; + } setTranslatedLyrics(TranslatedText); setShowTranslation(true); }, [lyrics, lyricsSettings, translatedLyrics, showTranslation]); diff --git a/src/renderer/features/settings/components/playback/lyric-settings.tsx b/src/renderer/features/settings/components/playback/lyric-settings.tsx index 153f13793..f7d6a6ca2 100644 --- a/src/renderer/features/settings/components/playback/lyric-settings.tsx +++ b/src/renderer/features/settings/components/playback/lyric-settings.tsx @@ -134,14 +134,14 @@ export const LyricSettings = () => { { control: ( { - setSettings({ lyrics: { ...settings, targetLanguage: value } }); + setSettings({ lyrics: { ...settings, translationTargetLanguage: value } }); }} /> ), - description: t('Target language for translation'), + description: t('setting.translationTargetLanguage', { + context: 'description', + postProcess: 'sentenceCase', + }), isHidden: !isElectron(), - title: t('setting.targetLanguage', { postProcess: 'sentenceCase' }), + title: t('setting.translationTargetLanguage', { postProcess: 'sentenceCase' }), }, { control: (