Translate bio cleanup

This commit is contained in:
Natsumi
2025-10-19 14:46:44 +11:00
parent d7e08d6f7d
commit 8c1b6d3e51
6 changed files with 74 additions and 50 deletions

View File

@@ -249,7 +249,7 @@
style="margin-left: 5px" style="margin-left: 5px"
@click="showBioDialog"></el-button> @click="showBioDialog"></el-button>
</div> </div>
<div v-if="userDialog.id !== currentUser.id" style="float: right"> <div v-if="translationApi" style="float: right">
<el-button type="text" size="small" style="margin-left: 5px" @click="translateBio" <el-button type="text" size="small" style="margin-left: 5px" @click="translateBio"
><i class="ri-translate-2"></i ><i class="ri-translate-2"></i
></el-button> ></el-button>
@@ -1310,10 +1310,9 @@
const { t } = useI18n(); const { t } = useI18n();
const advancedSettingsStore = useAdvancedSettingsStore();
const { hideUserNotes, hideUserMemos } = storeToRefs(useAppearanceSettingsStore()); const { hideUserNotes, hideUserMemos } = storeToRefs(useAppearanceSettingsStore());
const { bioLanguage, avatarRemoteDatabase } = storeToRefs(useAdvancedSettingsStore()); const { bioLanguage, avatarRemoteDatabase, translationApi } = storeToRefs(useAdvancedSettingsStore());
const { translateText } = useAdvancedSettingsStore();
const { userDialog, languageDialog, currentUser, isLocalUserVrcPlusSupporter } = storeToRefs(useUserStore()); const { userDialog, languageDialog, currentUser, isLocalUserVrcPlusSupporter } = storeToRefs(useUserStore());
const { const {
cachedUsers, cachedUsers,
@@ -1427,6 +1426,13 @@
pronouns: '' pronouns: ''
}); });
const bioCache = ref({
userId: null,
original: null,
translated: null,
showingTranslated: false
});
const userDialogAvatars = computed(() => { const userDialogAvatars = computed(() => {
const { avatars, avatarReleaseStatus } = userDialog.value; const { avatars, avatarReleaseStatus } = userDialog.value;
if (avatarReleaseStatus === 'public' || avatarReleaseStatus === 'private') { if (avatarReleaseStatus === 'public' || avatarReleaseStatus === 'private') {
@@ -2277,26 +2283,24 @@
D.visible = true; D.visible = true;
} }
const bioCache = ref({
userID: null,
original: null,
translated: null,
showingTranslated: false
});
async function translateBio() { async function translateBio() {
const bio = userDialog.value.ref.bio; const bio = userDialog.value.ref.bio;
if (!bio || bio === '-' || !advancedSettingsStore.translationApi) return; if (!bio) {
return;
}
const targetLang = bioLanguage.value; const targetLang = bioLanguage.value;
if (bioCache.value.userID !== userDialog.value.id) { if (bioCache.value.userId !== userDialog.value.id) {
bioCache.value.userID = userDialog.value.id; bioCache.value.userId = userDialog.value.id;
bioCache.value.original = null; bioCache.value.original = null;
bioCache.value.translated = null; bioCache.value.translated = null;
bioCache.value.showingTranslated = false; bioCache.value.showingTranslated = false;
} }
if (!bioCache.value.original) bioCache.value.original = bio; if (!bioCache.value.original) {
bioCache.value.original = bio;
}
if (bioCache.value.showingTranslated) { if (bioCache.value.showingTranslated) {
userDialog.value.ref.bio = bioCache.value.original; userDialog.value.ref.bio = bioCache.value.original;
@@ -2311,9 +2315,10 @@
} }
try { try {
const translated = await advancedSettingsStore.translateText(bio + '\n\nTranslated by Google', targetLang); const translated = await translateText(bio + '\n\nTranslated by Google', targetLang);
if (!translated) {
if (!translated) throw new Error('No translation returned'); throw new Error('No translation returned');
}
bioCache.value.translated = translated; bioCache.value.translated = translated;
bioCache.value.showingTranslated = true; bioCache.value.showingTranslated = true;

View File

@@ -640,10 +640,10 @@
"enable_tooltip": "Fetches video titles for use with gameLog and duration for overlay progress bar" "enable_tooltip": "Fetches video titles for use with gameLog and duration for overlay progress bar"
}, },
"translation_api": { "translation_api": {
"header": "Translation API", "header": "Google Translate API",
"enable": "Enable", "enable": "Enable",
"translation_api_key": "Translation API Key", "translation_api_key": "Google Translate API Key",
"enable_tooltip": "Translates user bios using a button on the bottom right" "enable_tooltip": "Translate user bios"
}, },
"video_progress_pie": { "video_progress_pie": {
"header": "Progress pie overlay for videos", "header": "Progress pie overlay for videos",
@@ -1305,10 +1305,10 @@
"save": "Save" "save": "Save"
}, },
"translation_api": { "translation_api": {
"header": "Translation API", "header": "Google Translate API",
"description": "Enter your Translation API Key (optional)", "description": "Enter your Google translate API Key",
"placeholder": "Translation API Key", "placeholder": "Google Translate API Key",
"guide": "Guide (skip the restrict usage part)", "guide": "Guide",
"save": "Save" "save": "Save"
}, },
"set_world_tags": { "set_world_tags": {

View File

@@ -41,8 +41,8 @@ export const useAdvancedSettingsStore = defineStore('AdvancedSettings', () => {
const screenshotHelperModifyFilename = ref(false); const screenshotHelperModifyFilename = ref(false);
const screenshotHelperCopyToClipboard = ref(false); const screenshotHelperCopyToClipboard = ref(false);
const youTubeApi = ref(false); const youTubeApi = ref(false);
const translationApi = ref(false);
const youTubeApiKey = ref(''); const youTubeApiKey = ref('');
const translationApi = ref(false);
const translationApiKey = ref(''); const translationApiKey = ref('');
const progressPie = ref(false); const progressPie = ref(false);
const progressPieFilter = ref(true); const progressPieFilter = ref(true);
@@ -87,8 +87,8 @@ export const useAdvancedSettingsStore = defineStore('AdvancedSettings', () => {
screenshotHelperModifyFilenameConfig, screenshotHelperModifyFilenameConfig,
screenshotHelperCopyToClipboardConfig, screenshotHelperCopyToClipboardConfig,
youTubeApiConfig, youTubeApiConfig,
translationApiConfig,
youTubeApiKeyConfig, youTubeApiKeyConfig,
translationApiConfig,
translationApiKeyConfig, translationApiKeyConfig,
progressPieConfig, progressPieConfig,
progressPieFilterConfig, progressPieFilterConfig,
@@ -128,8 +128,8 @@ export const useAdvancedSettingsStore = defineStore('AdvancedSettings', () => {
false false
), ),
configRepository.getBool('VRCX_youtubeAPI', false), configRepository.getBool('VRCX_youtubeAPI', false),
configRepository.getBool('VRCX_translationAPI', false),
configRepository.getString('VRCX_youtubeAPIKey', ''), configRepository.getString('VRCX_youtubeAPIKey', ''),
configRepository.getBool('VRCX_translationAPI', false),
configRepository.getString('VRCX_translationAPIKey', ''), configRepository.getString('VRCX_translationAPIKey', ''),
configRepository.getBool('VRCX_progressPie', false), configRepository.getBool('VRCX_progressPie', false),
configRepository.getBool('VRCX_progressPieFilter', true), configRepository.getBool('VRCX_progressPieFilter', true),
@@ -175,8 +175,8 @@ export const useAdvancedSettingsStore = defineStore('AdvancedSettings', () => {
screenshotHelperCopyToClipboard.value = screenshotHelperCopyToClipboard.value =
screenshotHelperCopyToClipboardConfig; screenshotHelperCopyToClipboardConfig;
youTubeApi.value = youTubeApiConfig; youTubeApi.value = youTubeApiConfig;
translationApi.value = translationApiConfig;
youTubeApiKey.value = youTubeApiKeyConfig; youTubeApiKey.value = youTubeApiKeyConfig;
translationApi.value = translationApiConfig;
translationApiKey.value = translationApiKeyConfig; translationApiKey.value = translationApiKeyConfig;
progressPie.value = progressPieConfig; progressPie.value = progressPieConfig;
progressPieFilter.value = progressPieFilterConfig; progressPieFilter.value = progressPieFilterConfig;
@@ -584,26 +584,43 @@ export const useAdvancedSettingsStore = defineStore('AdvancedSettings', () => {
} }
async function translateText(text, targetLang) { async function translateText(text, targetLang) {
if (!translationApiKey.value) return null; if (!translationApiKey.value) {
ElMessage({
message: 'No Translation API key configured',
type: 'warning'
});
return null;
}
try { try {
const res = await fetch( const response = await webApiService.execute({
`https://translation.googleapis.com/language/translate/v2?key=${translationApiKey.value}`, url: `https://translation.googleapis.com/language/translate/v2?key=${translationApiKey.value}`,
{
method: 'POST', method: 'POST',
headers: { 'Content-Type': 'application/json' }, headers: {
'Content-Type': 'application/json',
Referer: 'https://vrcx.app'
},
body: JSON.stringify({ body: JSON.stringify({
q: text, q: text,
target: targetLang, target: targetLang,
format: 'text' format: 'text'
}) })
} });
if (response.status !== 200) {
throw new Error(
`Translation API error: ${response.status} - ${response.data}`
); );
const data = await res.json(); }
const data = JSON.parse(response.data);
if (data.error) return null; if (AppDebug.debugWebRequests) {
console.log(data, response);
}
return data.data.translations[0].translatedText; return data.data.translations[0].translatedText;
} catch (err) { } catch (err) {
ElMessage({
message: `Translation failed: ${err.message}`,
type: 'error'
});
return null; return null;
} }
} }

View File

@@ -146,9 +146,10 @@
:long-label="true" :long-label="true"
@change="changeTranslationAPI('VRCX_translationAPI')" /> @change="changeTranslationAPI('VRCX_translationAPI')" />
<div class="options-container-item"> <div class="options-container-item">
<el-button size="small" :icon="CaretRight" @click="showTranslationApiDialog">{{ <el-button size="small" @click="showTranslationApiDialog"
t('view.settings.advanced.advanced.translation_api.translation_api_key') ><i class="ri-translate-2" style="margin-right: 5px"></i
}}</el-button> >{{ t('view.settings.advanced.advanced.translation_api.translation_api_key') }}</el-button
>
</div> </div>
</div> </div>
<div class="options-container"> <div class="options-container">

View File

@@ -3,11 +3,11 @@
class="x-dialog" class="x-dialog"
:model-value="isTranslationApiDialogVisible" :model-value="isTranslationApiDialogVisible"
:title="t('dialog.translation_api.header')" :title="t('dialog.translation_api.header')"
width="400px" width="450px"
@close="closeDialog"> @close="closeDialog">
<div class="options-container-item"> <div class="options-container-item">
<span class="name">{{ t('view.settings.appearance.appearance.bio_language') }}</span> <span class="name">{{ t('view.settings.appearance.appearance.bio_language') }}</span>
<el-dropdown trigger="click" size="small" @click.stop> <el-dropdown trigger="click" size="small" style="float: right" @click.stop>
<el-button size="small"> <el-button size="small">
<span> <span>
{{ messages[bioLanguage]?.language || bioLanguage }} {{ messages[bioLanguage]?.language || bioLanguage }}
@@ -25,6 +25,7 @@
</template> </template>
</el-dropdown> </el-dropdown>
</div> </div>
<br />
<div style="font-size: 12px">{{ t('dialog.translation_api.description') }} <br /></div> <div style="font-size: 12px">{{ t('dialog.translation_api.description') }} <br /></div>
<el-input <el-input

View File

@@ -3,7 +3,7 @@
class="x-dialog" class="x-dialog"
:model-value="isYouTubeApiDialogVisible" :model-value="isYouTubeApiDialogVisible"
:title="t('dialog.youtube_api.header')" :title="t('dialog.youtube_api.header')"
width="400px" width="450px"
@close="closeDialog"> @close="closeDialog">
<div style="font-size: 12px">{{ t('dialog.youtube_api.description') }} <br /></div> <div style="font-size: 12px">{{ t('dialog.youtube_api.description') }} <br /></div>