我决定使用bing api
翻译我的系统。代码下方会显示spinner
。我想让用户从spinner
中选择语言。那么bing api会根据所选择的语言来翻译系统,是不是可以让bing api来翻译值文件夹(strings.xml)?我如何在java
中调用它。以下是我的代码
public class LanguageActivity extends Activity {
Spinner spinnerctrl;
String translatedText;
Locale[] locales;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_language);
setTitle(R.string.title_activity_language)
spinnerctrl = (Spinner) findViewById(R.id.spinner1);
spinnerctrl.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> parent, View view,
int pos, long id) {
if (pos == 1) {
Toast.makeText(parent.getContext(),
getResources().getString(R.string.YouselectEn), Toast.LENGTH_SHORT)
.show();
} else if (pos == 2) {
Toast.makeText(parent.getContext(),
getResources().getString(R.string.YouselectZH), Toast.LENGTH_SHORT)
.show();
} else if (pos == 3) {
Toast.makeText(parent.getContext(),
getResources().getString(R.string.YouselectHI), Toast.LENGTH_SHORT)
.show();
}
else if (pos == 4) {
Toast.makeText(parent.getContext(),
getResources().getString(R.string.YouselectMs), Toast.LENGTH_SHORT)
.show();
}
}
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
Toast.makeText(getApplicationContext(), "Nothing to select", Toast.LENGTH_LONG).show();
}
class MyAsyncTask extends AsyncTask<Void, Integer, Boolean> {
@Override
protected Boolean doInBackground(Void... arg0) {
Translate.setClientId("plantidentificatio");
Translate.setClientSecret("lMFkUM+gG96NFrhhZV649Ix8NxR2SvqXJWpOTOgNq54");
try {
translatedText = Translate.execute("I should probably set this to something a little less profane", Language.ENGLISH, Language.CHINESE_SIMPLIFIED);
} catch(Exception e) {
translatedText = e.toString();
}
return true;
}
}
});
}
}
答案 0 :(得分:2)
不知道bing api但是这里是从string.xml
Field[] fields = R.string.class.getFields();
for (final Field field : fields) {
String name = field.getName(); //name of string
try {
int id = field.getInt(R.string.class); //id of string
Log.e("name",name);
Log.e("value", getString(id));
} catch (Exception ex) {
ex.printStackTrace();
}
}
此处Field
应为java.lang.reflect.Field