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 }]);
|
||||
setIsSpeaking(true);
|
||||
|
||||
// Detect language
|
||||
const spanishWords = ["hola", "gracias", "por favor", "quiero", "necesito", "reserva", "precio", "dónde", "cuándo", "cómo", "cuánto", "tengo", "quiero", "busco", "necesito"];
|
||||
const englishWords = ["hello", "thanks", "please", "want", "need", "book", "price", "where", "how", "much", "have", "looking"];
|
||||
|
||||
const isSpanish = spanishWords.some(w => userText.toLowerCase().includes(w));
|
||||
const isEnglish = englishWords.some(w => userText.toLowerCase().includes(w));
|
||||
// Detect language - improved logic
|
||||
// If user writes ANY English word, switch to English
|
||||
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;
|
||||
|
||||
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) {
|
||||
setLanguage(detectedLang);
|
||||
}
|
||||
@@ -231,9 +243,13 @@ export default function MiniMaxVoiceWidget({
|
||||
<span className="text-xl">🤖</span>
|
||||
<span className="font-semibold text-white text-sm">Asistente SiteMente</span>
|
||||
</div>
|
||||
<span className="text-xs text-white/70">
|
||||
{language === "es" ? "ES" : "EN"}
|
||||
</span>
|
||||
<button
|
||||
type="button"
|
||||
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>
|
||||
|
||||
{/* Messages */}
|
||||
|
||||
Reference in New Issue
Block a user