Android: Find the string resource key by the string value - android

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".

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
}

Related

Android what is the formatted flag for string resource means?

Basically question in the title.
I have seen several examples when people apply formatted flag to string resource, but can't find any official explanation, what this flag does.
There isn't any format documentation on the flag in Android docs, but the flag is commonly used to indicate a string that should be formatted before being shown to the user.
The Android docs show an example of how to do this.
Per the example at the link, you may have a string resource named welcome_messages,
<string name="welcome_messages" formatted="true">Hello, %1$s! You have %2$d new messages.</string>
that allows you to format a welcome message with a custom name and number of messages. You would use this string as follows
String text = getString(R.string.welcome_messages, username, mailCount);
The formatted flag helps distinguish strings that need to be formatted in this matter before being shown to the user.

Handling translation strings to languages with inflections on Android

I'm not sure this is the right forum to post this but I'm unsure how to organize my strings in certain scenarios where nouns in certain languages are inflected. It's easier to explain the problem with an example. Consider the following string:
<string name="my_string">Show you more information in context with each
<xliff:g id="item_name">%s</xliff:g></string>
The element <xliff:g id="item_name">%s</xliff:g> in this case can be replaced by, for example, "alarm" or "alert" depending on the context in my app. However, this doesn't work with languages that inflect the noun, such as Finnish.
In Finnish, %s is substituted with the word "hälytys" (=alarm) in the app though grammatically correct form in this case would be "hälytyksen" (an inflected form of singular alarm in Finnish, caused by a postposition and unfortunately no nice way to circumvent the inflection).
So it seems I can't have this sort of generic string element with replaceable nouns. Do you have any suggestions other than hard-coding the string elements for each possible value of %s?
Put your string this way in string.xml
<string name="my_string">Show you more information in context with each <xliff:g id=\"item_name\"> %s <\/xliff:g></string>
From Java
String s = String.format(getString(R.string.my_string,"alarm")); //put your dynamic value for alarm
Output would be :
Show you more information in context with each <xliff:g id="item_name"> alarm </xliff:g>

How to select a stringArray given an input

i'm learning how to do simple apps with Android.
i have a strings.xml with several array-strings. what i want is in an Android Activity to select automatically an array-string given an input string that will have the same name as an array-string in strings.xml.
my input is arraylist1, arraylist2 till arraylist20.
the names of my array-strings in my strings.xml are arraylist1, arraylist2 till arraylist20
each Array will have diferent strings.
for example i have an input my-string ="arraylist11". there is a way to refere "my-string" dinamically??
so if input is arraylist11 will get arraylist11 if the input is arraylist7 then get arraylist7 and so on.
how would be the code bellow??
"res.getStringArray(R.array.my-string)"
thanks
Try using
getResources().getString(getResources().getIdentifier(yourStringName, "string", getPackageName()));
to get your String but this is not recommended.
As per Android Developer
Note: use of this function is discouraged. It is much more efficient to retrieve resources by identifier than by name.

Best way to store multilanguage values on a database using strings.xml?

this is my first question :)
I'm developing an application that stores animal species in a database. The app must be multilanguage, so I tought to take advantage of using strings.xml resource files.
The idea is to store the english name of the species on the db, for example "cat", "dog" etc.. and then display to the user the actual translation, based on an xml like this (for italian):
<string name="dog">Cane</string>
<string name="cat">Gatto</string>
The problem is that R.string contains the name dog and cat, but they are actually int, so I'm searching a way to use the "dog" string to be used to compare the R.string.dog translated value.
I'm almost sure that my design is terribly wrong, but don't know what the correct way to doing this kind of work, since the app is now in a very early stage of development.
Thank you
EDIT with example
This example illustrates the problem:
Database data:
row1: id="1", value="dog"
row2: id="2", value="cat"
String file strings.xml:
<string name="dog">Dog</string>
<string name="cat">Cat</string>
String file strings-it.xml:
<string name="dog">Cane</string>
<string name="cat">Gatto</string>
My problem is: the user want to insert a specie in his native language (eg. "Cane"), and I want to search in the DB for its existence before inserting.
I should loop for every row on the DB (where values are stored in english), get the the translation of each row (eg: I found cat, then I translate to "Gatto") and compare with the user input.
Is it possible to do that?
If you have a string name you want to use, you can use getIdentifier() to get the string id. As an example, to find R.string.cat:
Resources res = getResources();
int stringId = res.getIdentifier("cat", "string", packageName);
In the above example, if there is no R.string.cat found, it will simply return 0. It's an easy test to see if a string exists.
Alternatively, you can get an array of all the string ids in your R.java by using something like:
Field[] fields = R.string.class.getFields();
int[] ids = new int[fields.length];
for(int i=0;i<field.length;i++)
ids[i] = field[i].getInt(null);
Of course, that will also look for any strings that you don't really intend as translations, such as dialog/window titles, label/button captions, etc. I wouldn't advise it in the general case. If I had to do it, I'd prefix the "translation" strings with something so I could easily tell what is what, something like "entry_cat".
Note that we're using reflection, and if you have a lot of strings, it could slow you down. If you are going to loop through R.java, I'd advise only doing it on start-up, and saving the values in some sort of array/list.
First read this.
http://developer.android.com/training/basics/supporting-devices/languages.html
You can create value folder with many language's i.e janapee,dutch etc
you can find out value folder inside the res folder in your project. and create new value folders.
res/
values/
strings.xml
values-es/
strings.xml
values-fr/
strings.xml
JUST TRANSLATE YOUR WORDS BY GOOGLE TRANSLATOR IN ANY LANGUAGE AND PUT INSIDE THE STRING.XML FILE .
Well, first of all, start reading this here:
Suppose that your application's default language is English. Suppose
also that you want to localize all the text in your application to
French, and most of the text in your application (everything except
the application's title) to Japanese. In this case, you could create
three alternative strings.xml files, each stored in a locale-specific
resource directory:
res/values/strings.xml Contains English text for all the strings that
the application uses, including text for a string named title.
res/values-fr/strings.xml Contain French text for all the strings,
including title. res/values-ja/strings.xml Contain Japanese text for
all the strings except title. If your Java code refers to
R.string.title, here is what will happen at runtime:
If the device is set to any language other than French, Android will
load title from the res/values/strings.xml file. If the device is set
to French, Android will load title from the res/values-fr/strings.xml
file. Notice that if the device is set to Japanese, Android will look
for title in the res/values-ja/strings.xml file. But because no such
string is included in that file, Android will fall back to the
default, and will load title in English from the
res/values/strings.xml file.

string formatting issue in android

I am facing an issue with "string.format" in android application. In my application when the user changes his language preferences from default (english) to any other foreign language (japanese,german etc) the variable string positioning is giving a force close error. Please refer the code below:
temp = String.format(locale,getResources().getString(R.string.temp_string), value, name);
where, temp_string = "The parcel number %1$d belongs to %2$s" for default selection (english)
when other languages are selected in some of them %2$s comes before %1$d . Due to which the the application force closes. Is there a way around to dynamically handle the variable strings(value,name).
I'd do something like:
temp = getResources().getString(R.string.temp_string, value, name);
As you see, getString() method can also receive parameters to format. Then, place different strings resources on different folders. For instance:
res/
values/
string.xml <--- here you put "The parcel number %1$d belongs to %2$s"
values-de/
string.xml <--- here you put "The parcel number %2$d belongs to %1$s"
I'm just giving you an example; I actually do not know how germany order is. I just want to explain what you actually have to try.

Categories

Resources