File size: 7,007 Bytes
b3b0b53
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
const plugin = requirePlugin("WechatSI");
const manager = plugin.getRecordRecognitionManager(); // Only one manager

Page({
    data: {
        languages: {
            'zh': { name: '中文', flag: 'cn', code: 'zh_CN' },
            'en': { name: 'English', flag: 'us', code: 'en_US' },
            'ja': { name: '日本語', flag: 'jp', code: 'ja_JP' },
            'ko': { name: '한국어', flag: 'kr', code: 'ko_KR' }
        },
        langCodes: ['zh', 'en', 'ja', 'ko'], // Use short codes for internal logic
        sourceLang: 'zh',
        targetLang: 'en',
        transcript: '',
        outputText: '',
        isRecording: false,
        sourceLanguages: [],
        targetLanguages: [],
        hfSpaceUrl: 'https://dazaozi-wechat-translator-app.hf.space'
    },

    onLoad: function () {
        this.initializeLanguages();
        this.initRecordManager();
    },

    // --- Language Selection Logic ---
    initializeLanguages: function () {
        const { langCodes, languages, sourceLang, targetLang } = this.data;
        this.setData({
            sourceLanguages: langCodes.map(c => ({
                langCode: c,
                name: languages[c].name,
                flag: languages[c].flag,
                selected: c === sourceLang
            })),
            targetLanguages: langCodes.map(c => ({
                langCode: c,
                name: languages[c].name,
                flag: languages[c].flag,
                selected: c === targetLang
            }))
        });
    },
    selectSourceLanguage: function (e) {
        const newSourceLang = e.currentTarget.dataset.langCode;
        this.setData({ sourceLang: newSourceLang }, this.initializeLanguages);
    },
    selectTargetLanguage: function (e) {
        const newTargetLang = e.currentTarget.dataset.langCode;
        this.setData({ targetLang: newTargetLang }, this.initializeLanguages);
    },
    swapLanguages: function () {
        const { sourceLang, targetLang } = this.data;
        this.setData({
            sourceLang: targetLang,
            targetLang: sourceLang,
            transcript: this.data.outputText,
            outputText: this.data.transcript
        }, this.initializeLanguages);
    },

    // --- Recorder Manager Initialization (Single Manager Mode) ---
    initRecordManager: function () {
        manager.onStart = () => {
            this.setData({ transcript: '正在聆听...', outputText: '' });
        };

        manager.onRecognize = (res) => {
            // Only update transcript for plugin-handled ASR (CN/EN)
            const { sourceLang } = this.data;
            if (sourceLang === 'zh' || sourceLang === 'en') {
                this.setData({ transcript: res.result });
            }
        };

        manager.onStop = (res) => {
            this.setData({ isRecording: false });
            const { sourceLang, targetLang } = this.data;
            const isChineseEnglish = (sourceLang === 'zh' && targetLang === 'en') || (sourceLang === 'en' && targetLang === 'zh');

            if (isChineseEnglish) {
                // Mode 1: Plugin handles both ASR and Translation
                if (res.result) {
                    this.setData({ transcript: res.result, outputText: res.translateResult || '' });
                } else {
                    this.setData({ transcript: '识别结果为空', outputText: '' });
                }
            } else {
                // Mode 2: Plugin handles ASR (if CN/EN), then HF handles translation
                if (res.result) { // res.result is the ASR result from plugin
                    this.setData({ transcript: res.result });
                    this.fullBackendBridge(res.result, this.data.sourceLang, this.data.targetLang);
                } else {
                    this.setData({ transcript: '识别结果为空' });
                }
            }
        };

        manager.onError = (res) => {
            this.setData({ isRecording: false, transcript: '识别失败', outputText: res.msg });
        };
    },

    startRecording: function () {
        const { sourceLang, targetLang } = this.data;
        const isChineseEnglish = (sourceLang === 'zh' && targetLang === 'en') || (sourceLang === 'en' && targetLang === 'zh');

        this.setData({ isRecording: true });

        // CRITICAL: Check if source language is supported by plugin for ASR
        if (sourceLang !== 'zh' && sourceLang !== 'en') {
            wx.showToast({
                title: '当前源语言不支持语音输入',
                icon: 'none'
            });
            this.setData({ isRecording: false, transcript: '请选择中文或英文作为源语言' });
            return;
        }

        if (isChineseEnglish) {
            // Use plugin for ASR and Translation
            manager.start({
                lang: this.data.languages[sourceLang].code,
                trans_lang: this.data.languages[targetLang].code,
            });
        } else {
            // Use plugin for ASR only, then HF for translation
            manager.start({
                lang: this.data.languages[sourceLang].code,
                // No trans_lang here, as HF will handle translation
            });
        }
    },

    stopRecording: function () {
        manager.stop();
    },

    // --- HF Bridge Translation Flow (for non-CN/EN pairs) ---
    fullBackendBridge: function(text, sourceLang, targetLang) {
        this.setData({ outputText: '翻译中 (1/2)..' }); // Simplified steps
        // Step 1: Translate source (e.g., ZH) to English via HF
        this.translateViaHF(text, sourceLang, 'en', (englishResult) => {
            if (englishResult) {
                // Step 2: Translate English result to final target (e.g., JA) via HF
                this.setData({ outputText: '翻译中 (2/2)..' });
                this.translateViaHF(englishResult, 'en', targetLang, (finalResult) => {
                    if (finalResult) {
                        this.setData({ outputText: finalResult });
                    }
                });
            }
        });
    },

    translateViaHF: function(text, sourceLang, targetLang, callback) {
        wx.request({
            url: `${this.data.hfSpaceUrl}/api/translate`,
            method: 'POST',
            header: { 'Content-Type': 'application/json' },
            data: { "text": text, "source_lang": sourceLang, "target_lang": targetLang },
            timeout: 30000,
            success: (res) => {
                if (res.statusCode === 200 && res.data.translated_text) {
                    callback(res.data.translated_text);
                } else {
                    this.setData({ outputText: `HF翻译失败 (${sourceLang}->${targetLang})` });
                    callback(null);
                }
            },
            fail: () => {
                this.setData({ outputText: `HF翻译请求失败 (${sourceLang}->${targetLang})` });
                callback(null);
            }
        });
    }
});