Reputation: 687
I have textView and ListView. i made a list of supporting langugage in listview
when i click any language Textview text needs to change
Code
String lang[] = new String[]{"English","French"};
ListView listView = (ListView) findViewById(android.R.id.list);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1,lang);
listView.setAdapter(adapter);
TextView text = (TextView)findViewById(R.id.textbox);
}
@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
TextView tv = (TextView)v;
String selected_lang = tv.getText().toString();
Toast.makeText(this, selected_lang, Toast.LENGTH_LONG).show();
}
Note: Need to Translate From one language to another language without make strings.xml and GoogleApi
Anybody Know How the Google Translate one language into other language ?
Upvotes: 3
Views: 1197
Reputation: 454
Create near your directory values new directory values-fr with file strings.xml and content like
<resources>
<string name="some_string_vith_localization">French translation</string>
</resources>
and in your code
String lang[] = new String[]{"en","fr"};
ListView listView = (ListView) findViewById(R.id.my_list_id);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1,lang);
listView.setAdapter(adapter);
TextView text = (TextView)findViewById(R.id.textbox);
}
@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
TextView tv = (TextView)view.findViewById(R.id.text_view_to_change);
String selected_lang = tv.getText().toString();
Locale locale2 = new Locale(selected_lang);
Locale.setDefault(locale2);
Configuration config2 = new Configuration();
config2.locale = locale2;
getBaseContext().getResources().updateConfiguration(config2,
getBaseContext().getResources().getDisplayMetrics());
tv.setText(getResources().getString(R.string.some_string_vith_localization));
}
Upvotes: 3
Reputation: 34301
Here is one complete example for Multiple Language Support in Android as like you needed.
Now,
For that few things are required to understand,
res/values
folder you can create as many folder and keep the respective language string values in Strings.xml
file under the folder.
now when the user select any language of it's choice then change the configuration
I also suggest that you should also go through once this topic : Localization
Upvotes: 1
Reputation: 1371
ok... try using the android-translate-api-1.1.jar
link:http://code.google.com/p/android-translate-api/
Locale curLocale = this.getResources().getConfiguration().locale;
I18nTranslator i18nTranslator = new I18nTranslator(curLocale .getLanguage());
String text = i18nTranslator.translateString("YOUR-TEXT-HERE");
Upvotes: 0