将文本翻译成英语

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

View File

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

View File

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

View File

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

View File

@@ -3,7 +3,7 @@
<view class="interaction">
<view class="editarea" @tap="handleOpenApp">
<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 class="spacerview small"></view>
<view class="collection" @tap="handleOpenApp">

View File

@@ -60,7 +60,7 @@
</view>
<view class="spacerview"></view>
<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"
alt="查看全文图标" />
</view>
@@ -98,7 +98,7 @@ import Comments from '@/pages/comments/comments.vue'
import Findmore from '@/pages/findmore/findmore.vue'
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()
@@ -173,7 +173,7 @@ async function translateNewsContent(newsData) {
if (translateTasks.length > 0) {
await Promise.all(
translateTasks.map(task =>
translateZhToJa(task.text).then(result => {
translateZhToEn(task.text).then(result => {
newsData[task.key] = result;
})
)

View File

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

View File

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

View File

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