Consider an app that takes user details - Name, Gender, Marital status etc.
And say, I need the app to support English, Spanish and Mandarin.
Displaying "Male" / "Female" in all the different languages while the user is filling out the form is achieved through different strings.xml files for different languages.
I'll be saving the details to a SQLite database in English. So if the user runs the app in Spanish and selects "Masculino", it'll still be saved as "Male" to the database.
Now, when the user wants to view the details that he has entered in Spanish, how do I read the English data in the SQLite and translate it to Spanish or Mandarin or any other language?
I wouldn't store english strings like that in the db. I'd store it as integer values, with 0 for male and 1 for female. Then when displaying I would check the value, then use the id for the corresponding string in resources and get the string from the xml string files.
You can also store the name of the string for "Male" instead of "Male". For example if you declare in your strings.xml file
<string name="gender_male">Male</string>
then store "gender_male" as a value in your database instead of "Male". When retrieving from database you then call
String value = get from database;
int valueResId = getResources().getIdentifier(value, "string", your package name);
String gender = getString(valueResId);
Related
So I have string.xml files for two languages, english and swahili. I let my users choose their default languages. And everything goes well until when I save the string resource ID for later use when they decide to change the language from the application's settings,but whenever I edit or add another string resource,and update my application, the original string ID's change and the string resource ID's that I saved in the database end up fetching values from different string resource from what my users had selected earlier. How do I overcome this? or stop my string resource ID's from changing every time I add new strings.
And everything goes well until when I save the string resource ID for later use
That is not a good plan.
but whenever I edit or add another string resource,and update my application, the original string ID's change and the string resource ID's that I saved in the database end up fetching values from different string resource from what my users had selected earlier
That is why this is not a good plan. Resource IDs are not designed to be constant from build to build.
How do I overcome this?
Do not store string resource IDs in a file, database, Web service, or similar container. Instead, store some identifier that you control that allows you to determine what string resource ID(s) that you should use.
I have two languages in my application, English and Arabic. and in the string resource files I have an array called "employment_array". I want to be able to grab this array from the language that is not selected. for example: if the user is using English language I want to grab the array from the Arabic String file.
I'm using the code below, but it is grabbing the array from the file of the selected language.
val employmentPositions: Array<String> = resources.getStringArray(R.array.employment_array)
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.
Hi I am trying to localize my android application and I read up on how by reading this tutorial http://developer.android.com/guide/topics/resources/localization.html but after looking through it, I couldn't figure out how to change the language. I have the default values in res/values/strings.xml and I have my other strings in res/values-latin/strings.xml. but I cant figure out how to make my application use the strings in res/values-latin/strings.xml instead of the default. Could someone explain this to me?
How about if someone clicks a change language button how could I inform the application to change all the strings?
Thank you!
Android will do it for you automatically.
When a user runs your application:
The Android system selects which resources to load, based on the
device's locale.
Example:
Suppose that your application's default language is English. Suppose also that you want to localize all the text in your application to French. In this case, you could create two 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.
res/values-fr/strings.xml
Contain French text for all the strings.
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 English title from the res/values/strings.xml file.
If the device is set to French, Android will load French title from the res/values-fr/strings.xml file.
With latin it will not work. You should search alternative method.
My solution in java:
public final class English {
String moon = "Moon";
String earth = "Earth";
String mars = "Mars";
}
public final class Latin {
String moon = "Luna";
String earth = "Terrae";
String mars = "Mars in Latin";
}
Then in your code you can aceess them:
TextView textView = (TextView) findViewById(R.id.my_text);
textView.setText( Latin.moon );
Hello friends I have made an application in which I have fetched data from database.All data in my application is coming from database.I have made this application in English language but now I want to convert my application in Hindi and other languages too.Actually if I used XML for string then I make different string.xml for every language and then set locale it was not the issue but converting string in database and then fetch.so please suggest me how can I adopt this idea.Any help would be appreciable.
You can either:
Add columns to your tables with the translations:
TABLE messages : ID, CONTENT, CONTENT_FR, CONTENT_ES, ...
Then you select the column according to the locale set in the device (if locale is FR, then use the column CONTENT_FR, if the locale is something not yet provided by the app, fallback to the CONTENT column)
Use several DB files (if your DB is static and won't be updated):
Load the DB file that corresponds to the user locale. Then your queries will remain unchanged.
You can also have db like
id, type, lang_id, str
id - string id
type - where this is used (1-login message, 2-error message, 3-character names, 4-attack names, etc.)
lang_id - language id, you can use 2-digit code (EN-English, FR-French, DE-German, etc.) or your own id definition (1-English, 2-French, 3-German, etc.)
str - the display string