HTML
瓜瓜粤语 GuaGua Cantonese - 普通話/粵語雙向逐字對照 Translate
🍉
瓜瓜粤语 GuaGua Cantonese
普通话 ➔ 粤语(逐字拆解·在线发音)
瓜瓜粤语 GuaGua Cantonese
Powered by viviarter.com
// 接入大模型 API 的口语化翻译函数
async function translateToCantonese() {
const inputArea = document.getElementById('input-text'); // 输入框 ID
const text = inputArea ? inputArea.value.trim() : '';
if (!text) {
alert("请输入需要翻译的文字");
return;
}
// 1. 替换为你的真实 DeepSeek API Key
const API_KEY = "sk-这里填你的真实Key";
try {
const response = await fetch('https://api.deepseek.com/chat/completions', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Authorization': `Bearer ${API_KEY}` // API Key 填入位置
},
body: JSON.stringify({
model: 'deepseek-chat',
messages: [
{
role: 'system',
content: `你是一个精通粤语与普通话对照的语言专家。
请将用户输入的普通话转换为地道的香港/广州日常粤语口语,并提供对应的粤拼(Jyutping)。
你必须且只能输出严格的 JSON 格式,不得包含 markdown 标记或任何解释说明:
{
"spoken": "地道粤语口语汉字",
"pinyin": "对应粤语拼音(带数字声调)"
}`
},
{
role: 'user',
content: text
}
],
temperature: 0.2,
response_format: { type: "json_object" }
})
});
const data = await response.json();
if (data.choices && data.choices[0]) {
const result = JSON.parse(data.choices[0].message.content);
// 2. 将翻译结果渲染到页面对应位置
if (document.getElementById('output-cantonese')) {
document.getElementById('output-cantonese').innerText = result.spoken;
}
if (document.getElementById('output-jyutping')) {
document.getElementById('output-jyutping').innerText = result.pinyin;
}
// 3. 自动触发粤语发音朗读(朗读转换后的地道口语)
playCantoneseSpeech(result.spoken);
} else {
alert("翻译请求失败,请检查 Key 是否有效");
}
} catch (err) {
console.error("请求发生错误:", err);
alert("网络或 API 调用异常");
}
}
// 粤语语音合成朗读函数
function playCantoneseSpeech(text) {
if ('speechSynthesis' in window) {
window.speechSynthesis.cancel(); // 停止先前的播放
const msg = new SpeechSynthesisUtterance(text);
msg.lang = 'zh-HK'; // 设置为香港粤语发音
msg.rate = 0.9; // 自然语速
window.speechSynthesis.speak(msg);
}
}