多語言系統核心 - 實現與全域語言切換的完美整合。
包含事件監聽、語言同步、翻譯字典管理和界面文字更新機制。
支援中英文即時切換,無需重新載入頁面。
Multilingual system core - Perfect integration with global language switching.
Includes event listening, language synchronization, translation dictionary management, and UI text update mechanisms.
Supports instant Chinese-English switching without page reload.
// 🌐 全域語言系統整合方法
setupGlobalLanguageListener() {
// 📡 監聽全域語言變更事件
window.addEventListener('languageChanged', (event) => {
this.currentLanguage = event.detail.language;
this.updateLanguage();
console.log(`🌍 語言已切換至: ${this.currentLanguage}`);
});
// 🔍 檢查全域語言管理器可用性
const checkGlobalLanguage = () => {
if (window.globalI18n) {
this.currentLanguage = window.globalI18n.currentLanguage;
this.updateLanguage();
console.log(`🎯 同步到全域語言: ${this.currentLanguage}`);
return true;
}
return false;
};
// ⏰ 如果全域語言管理器尚未載入,延遲檢查
if (!checkGlobalLanguage()) {
setTimeout(() => {
checkGlobalLanguage();
}, 100);
}
}
// 🔄 語言更新主方法
updateLanguage() {
this.updateUIText();
this.updatePreviewPlaceholders();
}
// 📝 完整的多語言翻譯字典
useBuiltinConfig() {
this.translations = {
zh: {
// 🎯 基本界面文字
pageTitle: '條碼/QR碼生成器',
formatLabel: '條碼格式',
contentLabel: '輸入內容',
contentPlaceholder: '請輸入要編碼的內容...',
// 🎨 控制界面
colorLabel: '顏色設定',
foregroundLabel: '前景色',
backgroundLabel: '背景色',
sizeLabel: '尺寸設定',
widthLabel: '寬度',
heightLabel: '高度',
marginLabel: '邊距',
errorCorrectionLabel: '容錯等級',
// 🔘 按鈕文字
generateBtn: '生成條碼',
clearBtn: '清除',
downloadPng: '下載 PNG',
downloadSvg: '下載 SVG',
copyImage: '複製圖片',
generateModeBtn: '生成模式',
decodeModeBtn: '解碼模式',
// 🔍 解碼界面
fileLabel: '選擇QR碼圖片',
fileInfoText: '支援格式: PNG, JPG, JPEG, GIF, WebP',
decodeBtn: '解碼QR碼',
clearDecodeBtn: '清除',
decodeResultLabel: '解碼結果',
copyResult: '複製內容',
visitLink: '前往連結',
// 💬 提示文字
generatePlaceholder: '請設定參數並點擊「生成條碼」',
decodePlaceholder: '請選擇QR碼圖片文件',
decoding: '解碼中...',
// ✅ 成功訊息
barcodeGenerated: '條碼生成成功!',
allCleared: '已清除所有內容',
imageCopied: '圖片已複製到剪貼簿!',
downloadStarted: '文件下載開始',
qrDecodeSuccess: 'QR碼解碼成功!',
decodeCleared: '已清除解碼內容',
contentCopied: '內容已複製到剪貼簿!',
linkOpening: '正在打開連結...',
// ❌ 錯誤訊息
pleaseEnterContent: '請輸入要編碼的內容',
generationFailed: '生成條碼失敗',
copyFailed: '複製失敗,請使用下載功能',
nothingToCopy: '沒有可複製的內容',
copyFailedManual: '複製失敗,請手動選擇複製',
invalidUrl: '無效的網址',
pleaseSelectFile: '請先選擇QR碼圖片',
libNotLoaded: 'QR解碼庫尚未載入完成,請稍後再試',
cannotReadQR: '無法讀取QR碼內容,請確認圖片清晰並包含有效的QR碼',
decodeError: '解碼過程中發生錯誤',
selectValidImage: '請選擇有效的圖片文件',
fileTooLarge: '圖片文件過大,請選擇小於10MB的文件',
generateFirst: '請先生成條碼',
// 🔍 驗證訊息
validContent: '內容格式正確',
invalidContent: '內容格式錯誤'
},
en: {
// 🎯 Basic interface text
pageTitle: 'Barcode / QR Code Generator',
formatLabel: 'Barcode Format',
contentLabel: 'Input Content',
contentPlaceholder: 'Enter content to encode...',
// 🎨 Control interface
colorLabel: 'Color Settings',
foregroundLabel: 'Foreground',
backgroundLabel: 'Background',
sizeLabel: 'Size Settings',
widthLabel: 'Width',
heightLabel: 'Height',
marginLabel: 'Margin',
errorCorrectionLabel: 'Error Correction',
// 🔘 Button text
generateBtn: 'Generate',
clearBtn: 'Clear',
downloadPng: 'Download PNG',
downloadSvg: 'Download SVG',
copyImage: 'Copy Image',
generateModeBtn: 'Generate Mode',
decodeModeBtn: 'Decode Mode',
// 🔍 Decode interface
fileLabel: 'Select QR Code Image',
fileInfoText: 'Supported formats: PNG, JPG, JPEG, GIF, WebP',
decodeBtn: 'Decode QR Code',
clearDecodeBtn: 'Clear',
decodeResultLabel: 'Decode Result',
copyResult: 'Copy Content',
visitLink: 'Visit Link',
// 💬 Hint text
generatePlaceholder: 'Please set parameters and click "Generate"',
decodePlaceholder: 'Please select QR code image file',
decoding: 'Decoding...',
// ✅ Success messages
barcodeGenerated: 'Barcode generated successfully!',
allCleared: 'All content cleared',
imageCopied: 'Image copied to clipboard!',
downloadStarted: 'Download started',
qrDecodeSuccess: 'QR code decoded successfully!',
decodeCleared: 'Decode content cleared',
contentCopied: 'Content copied to clipboard!',
linkOpening: 'Opening link...',
// ❌ Error messages
pleaseEnterContent: 'Please enter content to encode',
generationFailed: 'Failed to generate barcode',
copyFailed: 'Copy failed, please use download',
nothingToCopy: 'Nothing to copy',
copyFailedManual: 'Copy failed, please select manually',
invalidUrl: 'Invalid URL',
pleaseSelectFile: 'Please select QR code image first',
libNotLoaded: 'QR decode library not loaded, please try again',
cannotReadQR: 'Cannot read QR code content, please ensure image is clear and contains valid QR code',
decodeError: 'Error occurred during decoding',
selectValidImage: 'Please select a valid image file',
fileTooLarge: 'Image file too large, please select file smaller than 10MB',
generateFirst: 'Please generate barcode first',
// 🔍 Validation messages
validContent: 'Content format is valid',
invalidContent: 'Content format is invalid'
}
};
}
// 🎨 界面文字更新方法
updateUIText() {
if (!this.translations) return;
const t = this.translations[this.currentLanguage] || this.translations.zh;
// 📝 更新所有界面標籤文字
if (this.elements.pageTitle) this.elements.pageTitle.textContent = t.pageTitle;
if (this.elements.formatLabel) this.elements.formatLabel.textContent = t.formatLabel;
if (this.elements.contentLabel) this.elements.contentLabel.textContent = t.contentLabel;
if (this.elements.colorLabel) this.elements.colorLabel.textContent = t.colorLabel;
if (this.elements.foregroundLabel) this.elements.foregroundLabel.textContent = t.foregroundLabel;
if (this.elements.backgroundLabel) this.elements.backgroundLabel.textContent = t.backgroundLabel;
if (this.elements.sizeLabel) this.elements.sizeLabel.textContent = t.sizeLabel;
if (this.elements.widthLabel) this.elements.widthLabel.textContent = t.widthLabel;
if (this.elements.heightLabel) this.elements.heightLabel.textContent = t.heightLabel;
if (this.elements.marginLabel) this.elements.marginLabel.textContent = t.marginLabel;
if (this.elements.errorCorrectionLabel) this.elements.errorCorrectionLabel.textContent = t.errorCorrectionLabel;
// 🔘 更新按鈕文字
if (this.elements.generateBtn) this.elements.generateBtn.textContent = t.generateBtn;
if (this.elements.clearBtn) this.elements.clearBtn.textContent = t.clearBtn;
if (this.elements.downloadPng) this.elements.downloadPng.textContent = t.downloadPng;
if (this.elements.downloadSvg) this.elements.downloadSvg.textContent = t.downloadSvg;
if (this.elements.copyImage) this.elements.copyImage.textContent = t.copyImage;
if (this.elements.generateModeBtn) this.elements.generateModeBtn.textContent = t.generateModeBtn;
if (this.elements.decodeModeBtn) this.elements.decodeModeBtn.textContent = t.decodeModeBtn;
// 🔍 更新解碼界面文字
if (this.elements.fileLabel) this.elements.fileLabel.textContent = t.fileLabel;
if (this.elements.fileInfoText) this.elements.fileInfoText.textContent = t.fileInfoText;
if (this.elements.decodeBtn) this.elements.decodeBtn.textContent = t.decodeBtn;
if (this.elements.clearDecodeBtn) this.elements.clearDecodeBtn.textContent = t.clearDecodeBtn;
if (this.elements.decodeResultLabel) this.elements.decodeResultLabel.textContent = t.decodeResultLabel;
if (this.elements.copyResult) this.elements.copyResult.textContent = t.copyResult;
if (this.elements.visitLink) this.elements.visitLink.textContent = t.visitLink;
// 💭 更新提示文字
if (this.elements.contentInput) this.elements.contentInput.placeholder = t.contentPlaceholder;
// 🖼️ 更新預覽區域占位符
this.updatePreviewPlaceholders();
}