简体   繁体   English

Android-如何正确定位内容?

[英]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. 在用户填写表单时,通过用于不同语言的不同strings.xml文件可以以所有不同语言显示“男” /“女”。

I'll be saving the details to a SQLite database in English. 我将用英语将详细信息保存到SQLite数据库中。 So if the user runs the app in Spanish and selects "Masculino", it'll still be saved as "Male" to the database. 因此,如果用户使用西班牙语运行该应用程序并选择“ Masculino”,则该应用程序仍将作为“ Male”保存到数据库中。

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? 现在,当用户想要查看以西班牙语输入的详细信息时,如何读取SQLite中的英语数据并将其转换为西班牙语或普通话或任何其他语言?

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. 我将其存储为整数值,男性为0,女性为1。 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. 然后在显示时,我将检查该值,然后使用资源中相应字符串的id并从xml字符串文件中获取该字符串。

You can also store the name of the string for "Male" instead of "Male". 您也可以将字符串的名称存储为“ Male”而不是“ Male”。 For example if you declare in your strings.xml file 例如,如果您在strings.xml文件中声明

<string name="gender_male">Male</string>

then store "gender_male" as a value in your database instead of "Male" . 然后将"gender_male"作为值而不是"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);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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