将文本翻译成英语

This commit is contained in:
2025-12-04 18:52:57 +08:00
parent b4a86baaee
commit ffe4bacc82
9 changed files with 43 additions and 39 deletions

View File

@@ -4,17 +4,17 @@
<main> <main>
<!-- 评论头部信息 --> <!-- 评论头部信息 -->
<view class="commenthead"> <view class="commenthead">
<text class="commentcount">{{ comments.length }}件のコメント</text> <text class="commentcount">{{ comments.length }} comments</text>
<view class="headswitch"> <view class="headswitch">
<text class="inact" @tap="handleOpenApp">デフォルト</text> <text class="inact" @tap="handleOpenApp">Default</text>
<view class="act" @tap="handleOpenApp">最新</view> <view class="act" @tap="handleOpenApp">Latest</view>
</view> </view>
</view> </view>
<!-- 评论主体 --> <!-- 评论主体 -->
<!-- 翻译加载中状态 --> <!-- 翻译加载中状态 -->
<view v-if="isTranslating" class="translating"> <view v-if="isTranslating" class="translating">
<text>コメント読み込み中...</text> <text>Loading comments...</text>
</view> </view>
<view v-else-if="comments.list.length > 0"> <view v-else-if="comments.list.length > 0">
@@ -35,8 +35,8 @@
<uni-dateformat v-if="commentItem.createdAt" <uni-dateformat v-if="commentItem.createdAt"
:date="Date.parse(commentItem.createdAt.replace(/-/g, '/'))" :threshold="[0, 0]" format="yyyy-MM-dd" :date="Date.parse(commentItem.createdAt.replace(/-/g, '/'))" :threshold="[0, 0]" format="yyyy-MM-dd"
class="date-text" /> class="date-text" />
<text v-else class="date-text">不明な時間</text> <text v-else class="date-text">Unknown time</text>
<text class="replytext" @tap.stop="handleOpenApp">返信</text> <text class="replytext" @tap.stop="handleOpenApp">Reply</text>
</view> </view>
</view> </view>
<view class="spacerview"></view> <view class="spacerview"></view>
@@ -58,8 +58,8 @@
<view class="date-reply"> <view class="date-reply">
<uni-dateformat v-if="child.date" :date="Date.parse(child.date.replace(/-/g, '/'))" <uni-dateformat v-if="child.date" :date="Date.parse(child.date.replace(/-/g, '/'))"
:threshold="[0, 0]" format="yyyy-MM-dd" class="date-text" /> :threshold="[0, 0]" format="yyyy-MM-dd" class="date-text" />
<text v-else class="date-text">不明な時間</text> <text v-else class="date-text">Unknown time</text>
<text class="replytext" @tap.stop="handleOpenApp">返信</text> <text class="replytext" @tap.stop="handleOpenApp">Reply</text>
</view> </view>
</view> </view>
<view class="commentlike"> <view class="commentlike">
@@ -73,7 +73,7 @@
<view v-if="commentItem.reply.length" class="expandcomment"> <view v-if="commentItem.reply.length" class="expandcomment">
<view style="width:20px;height:1px;background:rgba(65,60,67,.2)"></view> <view style="width:20px;height:1px;background:rgba(65,60,67,.2)"></view>
<text class="expandcommenttext" :data-cid="commentItem.id"> <text class="expandcommenttext" :data-cid="commentItem.id">
{{ commentItem.showChild ? '折りたたむ' : `${commentItem.reply.length}件の返信を表示` }} {{ commentItem.showChild ? 'Collapse' : `Show ${commentItem.reply.length} replies` }}
</text> </text>
</view> </view>
@@ -85,7 +85,7 @@
<view v-else class="nocomments"> <view v-else class="nocomments">
<image src="/static/imgs/empty-img/b-empty-img@3x.webp" mode="aspectFit" alt="暂无评论"></image> <image src="/static/imgs/empty-img/b-empty-img@3x.webp" mode="aspectFit" alt="暂无评论"></image>
<text class="nocommentstext">コメントはまだありません</text> <text class="nocommentstext">No comments yet</text>
</view> </view>
<!-- 占位视图 --> <!-- 占位视图 -->
@@ -108,7 +108,7 @@ import { useCommonStore } from '@/stores/common.js'
import Findmore from '@/pages/findmore/findmore.vue' import Findmore from '@/pages/findmore/findmore.vue'
import Intereact from '@/pages/intereact/intereact.vue' import Intereact from '@/pages/intereact/intereact.vue'
// 引入翻译函数 // 引入翻译函数
import { translateZhToJa } from '@/utils/translate.js'; import { translateZhToEn } from '@/utils/translate.js';
const common = useCommonStore() const common = useCommonStore()
@@ -235,8 +235,8 @@ async function processComment(comment) {
// 翻译用户名和评论内容(并行处理,提升效率) // 翻译用户名和评论内容(并行处理,提升效率)
const [translatedUserName, translatedContent] = await Promise.all([ const [translatedUserName, translatedContent] = await Promise.all([
translateZhToJa(comment.user.nickName || '匿名用户'), translateZhToEn(comment.user.nickName || '匿名用户'),
translateZhToJa(comment.content) translateZhToEn(comment.content)
]); ]);
// 更新为翻译后的日语 // 更新为翻译后的日语
@@ -276,8 +276,8 @@ async function processChildComment(childComment) {
// 翻译子评论的用户名和内容 // 翻译子评论的用户名和内容
const [translatedUserName, translatedContent] = await Promise.all([ const [translatedUserName, translatedContent] = await Promise.all([
translateZhToJa(childComment.user.nickName || '匿名ユーザー'), translateZhToEn(childComment.user.nickName || 'Anonymous user'),
translateZhToJa(childComment.content) translateZhToEn(childComment.content)
]); ]);
// 更新为翻译后的日语 // 更新为翻译后的日语

View File

@@ -1,6 +1,6 @@
<template> <template>
<view class="findmore" @tap="common.openapp"> <view class="findmore" @tap="common.openapp">
<text class="openapptext">APP でさらに表示</text> <text class="openapptext">View more in APP</text>
</view> </view>
</template> </template>

View File

@@ -2,7 +2,7 @@
<view class="head"> <view class="head">
<image src="/static/imgs/h5logo/h5logo@3x.webp" mode="aspectFit" class="applogo" alt="官网logo" /> <image src="/static/imgs/h5logo/h5logo@3x.webp" mode="aspectFit" class="applogo" alt="官网logo" />
<view class="spacerview"></view> <view class="spacerview"></view>
<view class="download" @tap="common.download">アプリをダウンロード</view> <view class="download" @tap="common.download">Download App</view>
</view> </view>
</template> </template>

View File

@@ -1,6 +1,6 @@
<template> <template>
<view class="loading-container"> <view class="loading-container">
<text>页面加载中...</text> <text>Loading...</text>
</view> </view>
</template> </template>
<script setup> <script setup>
@@ -15,7 +15,7 @@ const redirectTo = (url) => {
// 页面加载时自动跳转 // 页面加载时自动跳转
onLoad(() => { onLoad(() => {
// 自动跳转 // 自动跳转
setTimeout(() => { setTimeout(() => {
redirectTo('/pages/post'); redirectTo('/pages/post');
}); // 自动跳转 }); // 自动跳转

View File

@@ -3,7 +3,7 @@
<view class="interaction"> <view class="interaction">
<view class="editarea" @tap="handleOpenApp"> <view class="editarea" @tap="handleOpenApp">
<image src="/static/imgs/editicon/icon@2x.webp" mode="aspectFit" class="editicon" alt="编辑标签"></image> <image src="/static/imgs/editicon/icon@2x.webp" mode="aspectFit" class="editicon" alt="编辑标签"></image>
<text class="edittext">さあ交流しましょう</text> <text class="edittext">Let's interact...</text>
</view> </view>
<view class="spacerview small"></view> <view class="spacerview small"></view>
<view class="collection" @tap="handleOpenApp"> <view class="collection" @tap="handleOpenApp">

View File

@@ -60,7 +60,7 @@
</view> </view>
<view class="spacerview"></view> <view class="spacerview"></view>
<view class="toseeall" @tap="handleInteraction"> <view class="toseeall" @tap="handleInteraction">
<text class="toseealltext">全文を表示</text> <text class="toseealltext">Show Full Text</text>
<image src="@/static/imgs/arrowrightup/arrowrightup@3x.png" class="arrowrightupicon" mode="aspectFit" <image src="@/static/imgs/arrowrightup/arrowrightup@3x.png" class="arrowrightupicon" mode="aspectFit"
alt="查看全文图标" /> alt="查看全文图标" />
</view> </view>
@@ -98,7 +98,7 @@ import Comments from '@/pages/comments/comments.vue'
import Findmore from '@/pages/findmore/findmore.vue' import Findmore from '@/pages/findmore/findmore.vue'
import { getPostList, getPostLImage, getPostVideo } from '../api/api.js' import { getPostList, getPostLImage, getPostVideo } from '../api/api.js'
// 引入与评论组件共享的翻译函数和全局缓存 // 引入与评论组件共享的翻译函数和全局缓存
import { translateZhToJa, translationCache } from '@/utils/translate.js'; import { translateZhToEn, translationCache } from '@/utils/translate.js';
const common = useCommonStore() const common = useCommonStore()
@@ -173,7 +173,7 @@ async function translateNewsContent(newsData) {
if (translateTasks.length > 0) { if (translateTasks.length > 0) {
await Promise.all( await Promise.all(
translateTasks.map(task => translateTasks.map(task =>
translateZhToJa(task.text).then(result => { translateZhToEn(task.text).then(result => {
newsData[task.key] = result; newsData[task.key] = result;
}) })
) )

View File

@@ -15,7 +15,7 @@
<text class="username">{{ post.translatedUserName || post.user.nickName }}</text> <text class="username">{{ post.translatedUserName || post.user.nickName }}</text>
<button class="follow" @tap="common.openapp"> <button class="follow" @tap="common.openapp">
<uni-icons v-if="post.isfollow" type="checkmarkempty" size="20" color="#333"></uni-icons> <uni-icons v-if="post.isfollow" type="checkmarkempty" size="20" color="#333"></uni-icons>
<text v-else>フォロー</text> <text v-else>Follow</text>
</button> </button>
</view> </view>
@@ -62,7 +62,7 @@
</view> </view>
<view v-else class="loading-container"> <view v-else class="loading-container">
<text>ページ読み込み中...</text> <text>Loading page...</text>
</view> </view>
</template> </template>
@@ -76,7 +76,7 @@ import Comments from '@/pages/comments/comments.vue'
import Findmore from '@/pages/findmore/findmore.vue' import Findmore from '@/pages/findmore/findmore.vue'
import { getPostList, getPostLImage, getPostVideo, getUserImg } from '../api/api.js' import { getPostList, getPostLImage, getPostVideo, getUserImg } from '../api/api.js'
// 引入翻译工具 // 引入翻译工具
import { translateZhToJa } from '@/utils/translate.js'; import { translateZhToEn } from '@/utils/translate.js';
const common = useCommonStore() const common = useCommonStore()
@@ -144,12 +144,12 @@ onLoad(() => {
// 仅翻译后端返回的特定内容 // 仅翻译后端返回的特定内容
// 1. 翻译用户名 // 1. 翻译用户名
if (data.user && data.user.nickName) { if (data.user && data.user.nickName) {
data.translatedUserName = await translateZhToJa(data.user.nickName); data.translatedUserName = await translateZhToEn(data.user.nickName);
} }
// 2. 翻译内容 // 2. 翻译内容
if (data.copywriting) { if (data.copywriting) {
data.translatedContent = await translateZhToJa(data.copywriting); data.translatedContent = await translateZhToEn(data.copywriting);
} }
const mediaPromises = [] const mediaPromises = []
@@ -266,10 +266,10 @@ onLoad(() => {
const handleError = (error) => { const handleError = (error) => {
console.error('数据处理错误:', error) console.error('数据处理错误:', error)
post.value = { post.value = {
user: { nickName: '不明ユーザー', userImg: '' }, user: { nickName: 'Unknown user', userImg: '' },
translatedUserName: '不明ユーザー', translatedUserName: 'Unknown user',
copywriting: 'コンテンツを読み込めませんでした', copywriting: 'Could not load content',
translatedContent: 'コンテンツを読み込めませんでした', translatedContent: 'Could not load content',
date: new Date().toISOString(), date: new Date().toISOString(),
imgs: [] imgs: []
} }

View File

@@ -88,7 +88,7 @@
<!-- <Findmore /> --> <!-- <Findmore /> -->
<view v-else class="loading-container"> <view v-else class="loading-container">
<text>ページ読み込み中...</text> <text>Loading...</text>
</view> </view>
</template> </template>
@@ -102,7 +102,7 @@ import Comments from '@/pages/comments/comments.vue'
import Intereact from '@/pages/intereact/intereact.vue' import Intereact from '@/pages/intereact/intereact.vue'
import { getPostList, getPostVideo, getUserImg } from '@/api/api.js' import { getPostList, getPostVideo, getUserImg } from '@/api/api.js'
// 导入翻译工具 // 导入翻译工具
import { translateZhToJa, translationCache } from '@/utils/translate.js' import { translateZhToEn, translationCache } from '@/utils/translate.js'
const common = useCommonStore() const common = useCommonStore()
const formatCount = common.formatCount const formatCount = common.formatCount
@@ -275,8 +275,8 @@ onLoad(() => {
// 并行翻译需要翻译的内容 // 并行翻译需要翻译的内容
Promise.all([ Promise.all([
translateZhToJa(userName), translateZhToEn(userName),
translateZhToJa(copywriting) translateZhToEn(copywriting)
]).then(([translatedUserName, translatedCopywriting]) => { ]).then(([translatedUserName, translatedCopywriting]) => {
// 更新视频数据 - 使用翻译后的内容 // 更新视频数据 - 使用翻译后的内容
data.userName = translatedUserName data.userName = translatedUserName

View File

@@ -11,7 +11,11 @@ let currentConcurrent = 0;
// 全局翻译缓存(跨组件共享,避免重复翻译) // 全局翻译缓存(跨组件共享,避免重复翻译)
export const translationCache = {}; export const translationCache = {};
export const clearTranslationCache = () => {
Object.keys(translationCache).forEach(key => {
delete translationCache[key];
});
};
// 敏感词列表(可根据实际情况扩展) // 敏感词列表(可根据实际情况扩展)
const SENSITIVE_WORDS = [ const SENSITIVE_WORDS = [
'大纪元', // 已确认的敏感词 '大纪元', // 已确认的敏感词
@@ -53,7 +57,7 @@ const filterSensitiveWords = (text) => {
* @param {string} text - 待翻译文本 * @param {string} text - 待翻译文本
* @returns {Promise<string>} 翻译结果(敏感词已被***替换) * @returns {Promise<string>} 翻译结果(敏感词已被***替换)
*/ */
export const translateZhToJa = async (text) => { export const translateZhToEn = async (text) => {
// 空文本直接返回 // 空文本直接返回
if (!text || text.trim() === '') return text.trim(); if (!text || text.trim() === '') return text.trim();
@@ -84,7 +88,7 @@ export const translateZhToJa = async (text) => {
const url = `/baidu-translate/api/trans/vip/translate?` + const url = `/baidu-translate/api/trans/vip/translate?` +
`q=${encodeURIComponent(filteredText)}&` + `q=${encodeURIComponent(filteredText)}&` +
`from=auto&` + `from=auto&` +
`to=jp&` + `to=en&` +
`appid=${APP_ID}&` + `appid=${APP_ID}&` +
`salt=${salt}&` + `salt=${salt}&` +
`sign=${sign}`; `sign=${sign}`;