2

Possible Duplicate:
Change language programatically in Android

I would like to develop an Android app which will offer some specific information based on the user selection of a country. So the user will choose a country and the app will update the fields for that country. The information is text. For each country the information would be about 2-3 pages, with different main fields.

My first thought was to add text files to the application in the "res/raw" folder and load them after getting the user preference. Is that the best way to do it? Another idea would be to add xml files in res/xml ( it would be easier to parse? ).

And if I would go with prepackaged text files, which would be the best way to parse the text file for adding information in the specific fields of the application?

Community
  • 1
  • 1
Berry
  • 59
  • 1
  • 10

3 Answers3

2

I think it is good to put them in "res/raw". You may consider using xml files instead of plain text files. It could be easier to parse and better organized.

Caner
  • 57,267
  • 35
  • 174
  • 180
1

It's recommend to use the method from Jave, but your way is also possible. Create for every language element in /res/values/strings.xml. You can set and select the chosen language with shared preferences. The file to select the chosen language is something like this:

TextView tv = (TextView) getViewById(R.id.textviewid);

SharedPreferences getPrefs = PreferenceManager.getDefaultSharedPreferences(getBaseContext());
boolean language = getPrefs.getString("language", "en");
if(language == "en") tv.setText(this.getString(R.string.en));
if(language == "nl") tv.setText(this.getString(R.string.nl));
if(language == "de") tv.setText(this.getString(R.string.de));

You have to make a layout for the page to choose a language by yourself. The java to set the language preference is something like this. I've used a textview to keep it simple, but it's recommend to change it to a dropdown menu orso. And you have to put this on a place where it checks it after clicking a button orso.

EditText lang = (EditText) findViewById(R.id.etId);

SharedPreferences getPrefs = PreferenceManager.getDefaultSharedPreferences(getBaseContext());
Editor editor = someData.edit();
editor.putString("language", lang.getText().toString());
editor.commit();

I haven't tested anything, so it can contain errors. If it does show errors and you don't know how to fix it I would like to help you.

I'm sorry for my bad English

//EDIT: The strings.xml should look like this:

<?xml version="1.0" encoding="utf-8" standalone="no"?>
<resources>
    <string name="en">Hello! This is the text!</string>
    <string name="nl">Hallo! Dit is de tekst!</string>
    ....
</resources>
Wietse de Vries
  • 695
  • 1
  • 5
  • 18
0

Not really what you are looking for, but you might be interested in reading the developer document about Localization as well.

Jave
  • 31,598
  • 14
  • 77
  • 90
  • I read about that but that is to support different languages. I want to give information which is specific to different countries, not necessarily the country the user is in. – Berry Mar 09 '12 at 10:42
  • That's what I thought. But I figured it could be related anyway, especially for the people coming here after reading the title of the question :) – Jave Mar 09 '12 at 10:45