From ccd558090c0f9158a7a630a599093c6a66209427 Mon Sep 17 00:00:00 2001 From: anghunk Date: Tue, 10 Feb 2026 14:39:00 +0800 Subject: [PATCH] =?UTF-8?q?fix(widget):=20=E4=BF=AE=E5=A4=8D=E8=AF=84?= =?UTF-8?q?=E8=AE=BA=E5=88=97=E8=A1=A8=E6=97=A0=E8=AF=84=E8=AE=BA=E6=97=B6?= =?UTF-8?q?=E7=9A=84=E6=96=87=E6=A1=88=E6=98=BE=E7=A4=BA=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 将硬编码的“暂无评论,快来抢沙发吧!”文案移至国际化文件,便于统一管理 - 在 CommentList 构造函数中注入翻译函数,确保组件能正确使用国际化文案 - 调整语言选择逻辑,优先使用用户配置的语言,其次使用服务器配置,最后自动检测 - 在开发工具中增加语言选择配置项,便于测试不同语言环境 --- docs/widget/src/components/CommentList.js | 3 ++- docs/widget/src/core/CWDComments.js | 4 ++-- docs/widget/src/dev.js | 6 +++++- docs/widget/src/locales/index.js | 2 +- 4 files changed, 10 insertions(+), 5 deletions(-) diff --git a/docs/widget/src/components/CommentList.js b/docs/widget/src/components/CommentList.js index 88d43fb..912ec62 100644 --- a/docs/widget/src/components/CommentList.js +++ b/docs/widget/src/components/CommentList.js @@ -35,6 +35,7 @@ export class CommentList extends Component { */ constructor(container, props = {}) { super(container, props); + this.t = props.t || ((k) => k); this.loadingComponent = null; this.paginationComponent = null; this.commentItems = new Map(); // 缓存 CommentItem 实例,key 为 comment.id @@ -120,7 +121,7 @@ export class CommentList extends Component { const emptyEl = this.createElement('div', { className: 'cwd-empty', children: [ - this.createTextElement('p', '暂无评论,快来抢沙发吧!', 'cwd-empty-text') + this.createTextElement('p', this.t('noComments'), 'cwd-empty-text') ] }); root.appendChild(emptyEl); diff --git a/docs/widget/src/core/CWDComments.js b/docs/widget/src/core/CWDComments.js index 539a672..e7332bf 100644 --- a/docs/widget/src/core/CWDComments.js +++ b/docs/widget/src/core/CWDComments.js @@ -169,7 +169,7 @@ export class CWDComments { }); // 设置语言 - let lang = serverConfig.widgetLanguage || 'auto'; + let lang = this.config.lang || serverConfig.widgetLanguage || 'auto'; if (lang === 'auto' && typeof navigator !== 'undefined') { const browserLang = navigator.language || navigator.userLanguage; if (browserLang.toLowerCase().startsWith('en')) { @@ -194,7 +194,7 @@ export class CWDComments { } } else { // 即使没有域名限制,也需要设置语言 - let lang = serverConfig.widgetLanguage || 'auto'; + let lang = this.config.lang || serverConfig.widgetLanguage || 'auto'; if (lang === 'auto' && typeof navigator !== 'undefined') { const browserLang = navigator.language || navigator.userLanguage; if (browserLang.toLowerCase().startsWith('en')) { diff --git a/docs/widget/src/dev.js b/docs/widget/src/dev.js index 8b4844b..427ed26 100644 --- a/docs/widget/src/dev.js +++ b/docs/widget/src/dev.js @@ -51,12 +51,14 @@ function populateInputs(config) { const avatarPrefixInput = document.getElementById('avatarPrefix'); const siteIdInput = document.getElementById('siteId'); const postSlugInput = document.getElementById('postSlug'); + const langSelect = document.getElementById('lang'); if (apiBaseUrlInput) apiBaseUrlInput.value = config.apiBaseUrl || DEFAULT_CONFIG.apiBaseUrl; if (themeSelect) themeSelect.value = config.theme || DEFAULT_CONFIG.theme; if (avatarPrefixInput) avatarPrefixInput.value = config.avatarPrefix || DEFAULT_CONFIG.avatarPrefix; if (siteIdInput) siteIdInput.value = config.siteId || DEFAULT_CONFIG.siteId || ''; if (postSlugInput) postSlugInput.value = config.postSlug || DEFAULT_CONFIG.postSlug || ''; + if (langSelect) langSelect.value = config.lang || DEFAULT_CONFIG.lang; } /** @@ -67,7 +69,8 @@ function getConfigFromInputs() { const theme = document.getElementById('theme')?.value || DEFAULT_CONFIG.theme; const siteId = document.getElementById('siteId')?.value || DEFAULT_CONFIG.siteId || ''; const postSlug = document.getElementById('postSlug')?.value || DEFAULT_CONFIG.postSlug || ''; - return { apiBaseUrl, theme, siteId, postSlug }; + const lang = document.getElementById('lang')?.value || DEFAULT_CONFIG.lang; + return { apiBaseUrl, theme, siteId, postSlug, lang }; } /** @@ -98,6 +101,7 @@ async function initWidget() { apiBaseUrl: config.apiBaseUrl, siteId: config.siteId, postSlug: config.postSlug, + lang: config.lang, }); widgetInstance.mount(); } catch (error) {} diff --git a/docs/widget/src/locales/index.js b/docs/widget/src/locales/index.js index 78b4b14..d0cca5d 100644 --- a/docs/widget/src/locales/index.js +++ b/docs/widget/src/locales/index.js @@ -64,7 +64,7 @@ export const locales = { verifyAdmin: '退出验证', formTitle: '发表评论', loadMore: '加载更多', - noComments: '暂无评论', + noComments: '暂无评论,快来抢沙发吧!', delete: '删除', confirmDelete: '确定删除这条评论吗?', deleteSuccess: '删除成功',