-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: display translations that are not the same as i18n locale (#73)
* feat: display translations that are not the same as i18n locale * feat: add separate prop `translationLocale` * feat: display translations that are not the same as i18n locale * chore: lint --------- Co-authored-by: Anthony Fu <[email protected]>
- Loading branch information
1 parent
fea0f73
commit dba34fc
Showing
14 changed files
with
124 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
<script setup lang="ts"> | ||
import type { MaruSongDataParsed } from '@marure/schema' | ||
import { Menu } from 'floating-vue' | ||
const props = defineProps<{ | ||
type: 'toggle-button' | 'toggle' | ||
song?: MaruSongDataParsed | ||
}>() | ||
const settings = useSettings() | ||
const { locales } = useI18n() | ||
const { | ||
translations, | ||
translationsEnabled, | ||
setTranslationLocale, | ||
toggleTranslation, | ||
} = useTranslationSettings(() => props.song) | ||
</script> | ||
|
||
<template> | ||
<Menu placement="bottom"> | ||
<ActionButton | ||
icon="i-uil-english-to-chinese" | ||
:type="type" | ||
:active="translationsEnabled" | ||
:title="type === 'toggle-button' ? $t('settings.displayTranslation') : undefined" | ||
@click="toggleTranslation" | ||
/> | ||
<template #popper> | ||
<div px4 py3 flex="~ col gap-2"> | ||
<div v-if="!translations.length" op75> | ||
{{ $t('messages.noTranslations') }} | ||
</div> | ||
<template v-else> | ||
<div> | ||
{{ $t('settings.displayTranslation') }} | ||
</div> | ||
<ToggleButton | ||
v-for="trans in translations" :key="trans" | ||
:model-value="settings.translationLocale === trans && settings.translation" | ||
:title="locales.find(l => l.code === trans)?.name || trans" | ||
@click="setTranslationLocale(trans)" | ||
/> | ||
</template> | ||
</div> | ||
</template> | ||
</Menu> | ||
</template> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,49 @@ | ||
import type { MaruSongDataParsed } from '@marure/schema' | ||
import { _settings } from '~/state/local-storage' | ||
|
||
export function useSettings() { | ||
return _settings | ||
} | ||
|
||
export function useTranslationSettings(song: MaybeRefOrGetter<MaruSongDataParsed | undefined>) { | ||
const settings = useSettings() | ||
|
||
const translations = computed(() => { | ||
const _song = toValue(song) | ||
if (!_song) | ||
return [] | ||
|
||
const locales = new Set<string>() | ||
for (const line of _song.lyrics) { | ||
for (const [key, value] of Object.entries(line.trans ?? {})) { | ||
if (value?.trim()) | ||
locales.add(key) | ||
} | ||
} | ||
|
||
return Array.from(locales) | ||
}) | ||
|
||
const translationsEnabled = computed(() => settings.value.translation && translations.value.includes(settings.value.translationLocale)) | ||
|
||
function toggleTranslation() { | ||
settings.value.translation = !settings.value.translation | ||
} | ||
|
||
function setTranslationLocale(localeCode: string) { | ||
if (settings.value.translationLocale === localeCode && settings.value.translation) { | ||
settings.value.translation = false | ||
} | ||
else { | ||
settings.value.translationLocale = localeCode | ||
settings.value.translation = true | ||
} | ||
} | ||
|
||
return { | ||
translations, | ||
translationsEnabled, | ||
toggleTranslation, | ||
setTranslationLocale, | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters