iOS Question [SOLVED] iSpeechRecognition - Supported Languages

aeric

Expert
Licensed User
Longtime User


I only can pass 2 letters code e.g "en" or "zh'.
Instead of "en-US", I want to get "en-UK". Instead of Chinese Traditional ("zh-TW"), I want to get Chinese Simplified ("zh-CN").
B4X:
Speech.SetLanguage("en")

How do I force the system to return a specific locale?
 
Last edited:
Solution
Try this:
B4X:
Sub AddLanguage (Identifier As String)
    Dim locale As NativeObject
    locale = locale.Initialize("NSLocale").RunMethod("alloc", Null).RunMethod("initWithLocaleIdentifier:", Array(Identifier))
    Dim no As NativeObject = speech
    no.GetField("languages").RunMethod("setObject:forKey:", Array(locale, Identifier))
End Sub

lang = "zh-TH"
AddLanguage(lang)
If speech.SetLanguage(lang) = False Then
    TextView1.Text = "Speech Recognition not available."
Else
    TextView1.Text = "Ready!"
    SetState(True)
End If

Erel

B4X founder
Staff member
Licensed User
Longtime User
Try this:
B4X:
Sub AddLanguage (Identifier As String)
    Dim locale As NativeObject
    locale = locale.Initialize("NSLocale").RunMethod("alloc", Null).RunMethod("initWithLocaleIdentifier:", Array(Identifier))
    Dim no As NativeObject = speech
    no.GetField("languages").RunMethod("setObject:forKey:", Array(locale, Identifier))
End Sub

lang = "zh-TH"
AddLanguage(lang)
If speech.SetLanguage(lang) = False Then
    TextView1.Text = "Speech Recognition not available."
Else
    TextView1.Text = "Ready!"
    SetState(True)
End If
 
Upvote 0
Solution

aeric

Expert
Licensed User
Longtime User
Thanks @Erel
Do I need to call AddLanguage(lang) and speech.SetLanguage(lang) every time I need to switch between multiple languages?

B4X:
lang = "en-US"
AddLanguage(lang)
speech.SetLanguage(lang)

lang = "zh-CN"
AddLanguage(lang)
speech.SetLanguage(lang)

lang = "en-US"
AddLanguage(lang)
speech.SetLanguage(lang)

lang = "zh-CN"
AddLanguage(lang)
speech.SetLanguage(lang)
 
Upvote 0

aeric

Expert
Licensed User
Longtime User
I found out I only need to add the languages once.

B4X:
AddLanguage("zh-CN")
AddLanguage("en-US")

speech.SetLanguage("zh-CN")
speech.SetLanguage("en-US")
speech.SetLanguage("zh-CN")
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…