简体   繁体   中英

Android - How to localize content properly?

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);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM