Issue
There are a lot of explanations out there in how to retrieve a string resource value by its key. But I actually need the solution for the reverse case - retrieving the key by the value instead.
You may wonder why would I ever need something like that. Well, I got speech recognition in my app and in case there's a non-english speaker input, I need to recover the localized string key from the raw voice-to-text input so I can have multilanguage speech recognition support in my app.
So imagine I had a string resource:
// EN
<string name="not_a_dog">Cat</string>
// DE
<string name="not_a_dog">Katze</string>
So in case user says "Cat" (or "Katze" for German locale), I would get "not_a_dog".
Solution
you don't need to declare mutiple strings for different languages
Just create string.xml for each languages like described here; then you can do : if(yourText.equals(getString(R.string.not_a_dog)){
//do what you want
}
Answered By - Constantin N.
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.