Adding multilingual support ensures your app can cater to users across different regions and languages. On Day 8, we’ll explore how to recognize and process voice commands in multiple languages using React Native Voice and Expo Speech API.
1. Why Multilingual Support Matters
- Global Reach: Attract users from diverse linguistic backgrounds.
- Accessibility: Enhance usability for non-native speakers.
- Localization: Provide region-specific responses and functionality.
2. Setting Up Multilingual Recognition
Step 1: Define Supported Languages
Create an array of supported languages and their codes:
const supportedLanguages = [
{ name: "English", code: "en-US" },
{ name: "Spanish", code: "es-ES" },
{ name: "French", code: "fr-FR" },
];
Step 2: Capture Selected Language
Store the user’s selected language in the app state:
import React, { useState } from 'react';
const [language, setLanguage] = useState("en-US");
Add a dropdown or picker for users to choose their language:
import { Picker } from '@react-native-picker/picker';
<Picker
selectedValue={language}
onValueChange={(itemValue) => setLanguage(itemValue)}
>
{supportedLanguages.map((lang) => (
<Picker.Item key={lang.code} label={lang.name} value={lang.code} />
))}
</Picker>;
3. Recognizing Voice Input in Multiple Languages
Step 1: Start Voice Recognition with Selected Language
Pass the selected language code to Voice.start()
:
const startListening = async () => {
try {
await Voice.start(language); // Use the selected language
} catch (error) {
console.error("Error starting voice recognition:", error);
}
};
Step 2: Handle Language-Specific Commands
Modify the command handler to support multilingual phrases:
const commands = {
"en-US": {
"open settings": () => console.log("Opening settings..."),
"show weather": () => console.log("Fetching weather data..."),
},
"es-ES": {
"abrir configuración": () => console.log("Abriendo configuración..."),
"mostrar clima": () => console.log("Obteniendo datos del clima..."),
},
};
const handleCommand = (text) => {
const languageCommands = commands[language];
const command = Object.keys(languageCommands).find((cmd) =>
text.toLowerCase().includes(cmd)
);
if (command) {
languageCommands[command]();
} else {
console.log("Command not recognized.");
}
};
4. Adding Multilingual Text-to-Speech
Step 1: Adjust Speech Settings
Pass the selected language to the Speech.speak()
function:
import * as Speech from 'expo-speech';
const speak = (text) => {
Speech.speak(text, { language });
};
Step 2: Provide Language-Specific Responses
Use localized responses for commands:
const responses = {
"en-US": {
"settings": "Opening settings.",
"weather": "Here is the weather data.",
},
"es-ES": {
"settings": "Abriendo configuración.",
"weather": "Aquí están los datos del clima.",
},
};
const handleCommandWithResponse = (text) => {
const languageResponses = responses[language];
const command = Object.keys(languageResponses).find((cmd) =>
text.toLowerCase().includes(cmd)
);
if (command) {
Speech.speak(languageResponses[command], { language });
} else {
Speech.speak("Command not recognized.", { language });
}
};
5. Testing Multilingual Support
Step 1: Start the Development Server
Run your app:
expo start
Step 2: Test Different Languages
- Select a language from the picker.
- Speak commands in the selected language and verify their accuracy.
- Ensure TTS provides feedback in the corresponding language.
6. Key Considerations
- Language Detection: Automatically detect the language using APIs like Google Translate or NLP libraries.
- Command Variations: Support synonyms and colloquial phrases for better accuracy.
- Fallback Language: Set a default language for unrecognized commands or unsupported languages.
7. Key Concepts Covered
- Capturing and recognizing voice input in multiple languages.
- Localizing commands and text-to-speech feedback.
- Providing dynamic language switching for global audiences.
Next Steps
On Day 9, we’ll focus on error handling for speech misinterpretation to create a more robust and user-friendly experience.
References and Links:
SEO Keywords: multilingual voice recognition, React Native voice commands in multiple languages, Expo Speech API multilingual support, localized TTS in React Native, handling multilingual commands in apps.