Add translating system to aboutNewsOrg. (And it didn't work ;(

This commit is contained in:
yuanhau 2025-06-07 14:23:38 +08:00
parent 46197bc4a3
commit 3d9e69d4fc
2 changed files with 76 additions and 7 deletions

View file

@ -6,6 +6,12 @@ import { ScrambleTextPlugin } from "gsap/dist/ScrambleTextPlugin";
gsap.registerPlugin(ScrambleTextPlugin); gsap.registerPlugin(ScrambleTextPlugin);
const loading = ref(true); const loading = ref(true);
const { t, locale } = useI18n(); const { t, locale } = useI18n();
import translate from "translate";
interface translateInterfaceText {
translateText: string;
}
const translateItem: Record<string, translateInterfaceText> = {};
const emit = defineEmits([ const emit = defineEmits([
"windowopener", "windowopener",
@ -19,6 +25,7 @@ const props = defineProps({
type: String, type: String,
required: true, required: true,
}, },
applyForTranslation: Boolean,
}); });
const staticProps = props; const staticProps = props;
@ -59,6 +66,50 @@ watch(
const openNews = (url: string, titleName: string) => { const openNews = (url: string, titleName: string) => {
emit("openArticles", url, titleName); emit("openArticles", url, titleName);
}; };
const startTranslating = async (text: string) => {
try {
console.log(text);
translateItem[text] = {
translateText: await translate(text, { from: "zh", to: "en" }),
};
console.log(translateItem[text]);
} catch (error) {
console.error("Translation failed:", error);
traslateFailed.value = true;
translateItem[text] = { translateText: text }; // fallback to original text
}
};
// Translating logic
const translateText = ref(false);
const translatedBefore = ref(false);
const traslateFailed = ref(false);
watch(
() => props.applyForTranslation,
(value) => {
if (value === true) {
translateText.value = true;
if (!data.value) {
return;
}
if (translatedBefore.value === true) {
return;
}
startTranslating(fetchNewsOrgInfo.value?.title);
startTranslating(fetchNewsOrgInfo.value?.origin);
startTranslating(fetchNewsOrgInfo.value?.author);
for (const articles of fetchNewsOrgInfo.value?.articles) {
startTranslating(articles.title.replaceAll("獨家專欄》", ""));
startTranslating(articles.date);
}
// NOT retranslating AGAIN when disabling the feat
translatedBefore.value = true;
} else {
translateText.value = false;
}
},
); // Translate when requested?
</script> </script>
<template> <template>
<div> <div>
@ -76,7 +127,11 @@ const openNews = (url: string, titleName: string) => {
class="text-4xl font-bold m-2 text-center" class="text-4xl font-bold m-2 text-center"
ref="orgNameAnimation" ref="orgNameAnimation"
> >
{{ fetchNewsOrgInfo?.title }} {{
translateText
? translateItem[fetchNewsOrgInfo?.title]
: fetchNewsOrgInfo?.title
}}
</h1> </h1>
<div v-if="pending" class="flex flex-col gap-2 m-1 mt-5"> <div v-if="pending" class="flex flex-col gap-2 m-1 mt-5">
@ -85,7 +140,11 @@ const openNews = (url: string, titleName: string) => {
<div class="h-4 bg-gray-200 animate-pulse rounded w-4/6"></div> <div class="h-4 bg-gray-200 animate-pulse rounded w-4/6"></div>
</div> </div>
<span v-else class="text-ms m-1 mt-5 text-left text-wrap"> <span v-else class="text-ms m-1 mt-5 text-left text-wrap">
{{ fetchNewsOrgInfo?.description }} {{
translateText
? translateItem[fetchNewsOrgInfo?.description]
: fetchNewsOrgInfo?.description
}}
</span> </span>
</div> </div>
</div> </div>
@ -109,9 +168,13 @@ const openNews = (url: string, titleName: string) => {
<div> <div>
<div class="flex flex-col"> <div class="flex flex-col">
<span class="title text-bold texxt-sm">{{ <span class="title text-bold texxt-sm">{{
item.title.replaceAll("獨家專欄》", "") translateText
? translateItem[item.title.replaceAll("獨家專欄》", "")]
: item.title.replaceAll("獨家專欄》", "")
}}</span>
<span class="date text-xs">{{
translateText ? translateItem[item.date] : item.date
}}</span> }}</span>
<span class="date text-xs">{{ item.date }}</span>
</div> </div>
</div> </div>
</button> </button>

View file

@ -216,6 +216,12 @@ const openPublisher = (slug: string, title: string) => {
}; };
const isLoading = computed(() => contentArray.value.length === 0); const isLoading = computed(() => contentArray.value.length === 0);
const testmessage = await translate("嗨", { from: "zh", to: "en" }); const testmessage = await translate("嗨", { from: "zh", to: "en" });
const shouldHideItem = (item) => {
return (
item.contentType !== "GENERAL" ||
item.publisher?.toLowerCase().includes("line")
);
};
</script> </script>
<template> <template>
<div class="justify-center align-center text-center"> <div class="justify-center align-center text-center">
@ -257,7 +263,7 @@ const testmessage = await translate("嗨", { from: "zh", to: "en" });
<div> <div>
<!-- Loading State --> <!-- Loading State -->
<template v-if="isLoading"> <template v-if="isLoading">
<div v-for="n in 5" :key="n" class="p-2 bg-gray-200 rounded m-1"> <div v-for="n in 7" :key="n" class="p-2 bg-gray-200 rounded m-1">
<!-- Title Skeleton --> <!-- Title Skeleton -->
<div <div
class="h-8 bg-gray-300 animate-pulse rounded-lg w-3/4 mx-auto mb-2" class="h-8 bg-gray-300 animate-pulse rounded-lg w-3/4 mx-auto mb-2"
@ -303,9 +309,9 @@ const testmessage = await translate("嗨", { from: "zh", to: "en" });
<div <div
v-for="item in contentArray" v-for="item in contentArray"
:key="item.id" :key="item.id"
:class="item.contentType !== 'GENERAL' && 'hidden'" :class="shouldHideItem(item) && 'hidden'"
> >
<div class="p-2 bg-gray-200 rounded m-1 p-1"> <div class="p-2 bg-gray-200 rounded m-1">
<h1 <h1
class="text-2xl text-bold" class="text-2xl text-bold"
:class="getCheckResult(item.title) ? 'text-red-600' : ''" :class="getCheckResult(item.title) ? 'text-red-600' : ''"