Fix language detection and add manual toggle
This commit is contained in:
@@ -131,18 +131,30 @@ export default function MiniMaxVoiceWidget({
|
|||||||
setMessages(prev => [...prev, { role: "user", content: userText }]);
|
setMessages(prev => [...prev, { role: "user", content: userText }]);
|
||||||
setIsSpeaking(true);
|
setIsSpeaking(true);
|
||||||
|
|
||||||
// Detect language
|
// Detect language - improved logic
|
||||||
const spanishWords = ["hola", "gracias", "por favor", "quiero", "necesito", "reserva", "precio", "dónde", "cuándo", "cómo", "cuánto", "tengo", "quiero", "busco", "necesito"];
|
// If user writes ANY English word, switch to English
|
||||||
const englishWords = ["hello", "thanks", "please", "want", "need", "book", "price", "where", "how", "much", "have", "looking"];
|
const spanishPatterns = /^(hola|gracias|por| favor|quiero|necesito|reserva|precio|dónde|cuándo|cómo|cuánto|tengo|busco|qué|es|son|hay|me|puedo)\s/i;
|
||||||
|
const englishPatterns = /^(hello|thanks|please|want|need|book|price|where|how|much|have|can|i|my|your|the|a|an|is|are|do|does|what|who|when|where|why|how)\s/i;
|
||||||
const isSpanish = spanishWords.some(w => userText.toLowerCase().includes(w));
|
|
||||||
const isEnglish = englishWords.some(w => userText.toLowerCase().includes(w));
|
|
||||||
|
|
||||||
let detectedLang: Lang = language;
|
let detectedLang: Lang = language;
|
||||||
if (isSpanish && !isEnglish) detectedLang = "es";
|
|
||||||
else if (isEnglish && !isSpanish) detectedLang = "en";
|
|
||||||
else if (isSpanish && isEnglish && language === "es") detectedLang = "es";
|
|
||||||
|
|
||||||
|
// Check if it's clearly English (more inclusive)
|
||||||
|
const isEnglish = englishPatterns.test(userText.toLowerCase());
|
||||||
|
const isSpanish = spanishPatterns.test(userText.toLowerCase());
|
||||||
|
|
||||||
|
// If user writes in English (more than 50% English letters or explicit English words)
|
||||||
|
if (!isSpanish && (isEnglish || /[a-z]{3,}/i.test(userText))) {
|
||||||
|
// More English characters or explicit English = English
|
||||||
|
const englishChars = (userText.match(/[a-zA-Z]/g) || []).length;
|
||||||
|
const spanishChars = (userText.match(/[áéíóúñü]/gi) || []).length;
|
||||||
|
if (englishChars > spanishChars * 2 || isEnglish) {
|
||||||
|
detectedLang = "en";
|
||||||
|
}
|
||||||
|
} else if (isSpanish) {
|
||||||
|
detectedLang = "es";
|
||||||
|
}
|
||||||
|
|
||||||
|
// Update language if detected differently
|
||||||
if (detectedLang !== language) {
|
if (detectedLang !== language) {
|
||||||
setLanguage(detectedLang);
|
setLanguage(detectedLang);
|
||||||
}
|
}
|
||||||
@@ -231,9 +243,13 @@ export default function MiniMaxVoiceWidget({
|
|||||||
<span className="text-xl">🤖</span>
|
<span className="text-xl">🤖</span>
|
||||||
<span className="font-semibold text-white text-sm">Asistente SiteMente</span>
|
<span className="font-semibold text-white text-sm">Asistente SiteMente</span>
|
||||||
</div>
|
</div>
|
||||||
<span className="text-xs text-white/70">
|
<button
|
||||||
{language === "es" ? "ES" : "EN"}
|
type="button"
|
||||||
</span>
|
onClick={() => setLanguage(language === "es" ? "en" : "es")}
|
||||||
|
className="px-2 py-1 bg-white/20 rounded text-xs text-white hover:bg-white/30 transition"
|
||||||
|
>
|
||||||
|
{language === "es" ? "🇪🇸 ES" : "🇺🇸 EN"}
|
||||||
|
</button>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
{/* Messages */}
|
{/* Messages */}
|
||||||
|
|||||||
Reference in New Issue
Block a user