using different string files in android

2019-04-29 18:37发布

问题:

I'm porting my iPhone app to android and I'm having a problem with the string files now.

The app is a translation tool and users can switch the languages, so all the localized strings are in both languages and they are independent from what locale the OS is running.

For iOS version I have different files like de.strings, en.strings and fr.strings and so on. For every target with specified language pair I read the strings from the string tables, e.g. for de-fr I will include de.strings and fr.strings in project and set the name of the string tables in the info-list file and read strings from them. In the end I have one project containing different targets (with different info-list files) and all are well configured.

I'm intending to do the same on android platform, but

  1. Is only one strings.xml allowed per project?

  2. How do I set different build target? e.g. my de-fr and de-en translation app are actually two apps where the only difference is the language pairs. How can I set something so that I can use one project to generate the two apps? In XCode it's simple but I can't find a solution with eclipse.

  3. How do I specify per target which strings.xml it should read?

Thank you for your answers but Please Note that I need OS locale independent language settings, i.e. if the user changes OS locale from en to de, my app still shows localized strings in English. What I'm asking is actually how I can set something like prebuild and load different string files for different targets.

回答1:

Automatic locale selection, according to user settings

The strings.xml contains the original text, assuming for the English language. To create translations into different languages you can create folders, for example: values-gr, values.it, for the Greek end Italian. Just copy strings.xml into those folders and translate it.

On application launch, OS automatically picks a language according to the user's preferences.

Manually locale selection, overriding user settings

To force Greek for example you can use:

Locale locale = new Locale("gr"); 
            Locale.setDefault(locale);
            Configuration config = new Configuration();
            config.locale = locale;
            getBaseContext().getResources().updateConfiguration(config, getBaseContext().getResources().getDisplayMetrics());

You should, of course, provide a Greek translation for this to work.

Read more

You can check the documentation here: Support Different Languages - Android



回答2:

you have to put your localized strings in different folders like values-es, values-de, values-fr, etc.

The file must contain the same keys, for example in values folder:

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string name="hello">Hello</string>
</resources>

in values-es folder:

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string name="hello">Hola</string>
</resources>

and so on.



回答3:

You have to create one values folder for each language adding the language ISO code of the language you want to have a translation using this format: values-es, values-de, ... In each folder you have to add a strings.xml with strings of its language. The values folder (withoud country code) will be the default language.

For choose the string language you want to use:

import android.app.Activity;
import android.content.res.Configuration;
import android.os.Bundle;

public class Main extends Activity {
  /** Called when the activity is first created. */
  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    String languageToLoad  = "fa"; // your language
    Locale locale = new Locale(languageToLoad); 
    Locale.setDefault(locale);
    Configuration config = new Configuration();
    config.locale = locale;
    getBaseContext().getResources().updateConfiguration(config, 
    getBaseContext().getResources().getDisplayMetrics());
    this.setContentView(R.layout.main);
  }
}


回答4:

ad 1. No you can have as many as you want. See ad 3 for more information.

ad 2. ????

ad 3. To make language selection in our app you should update context. Then proper xml will be selected automatically. Read about this to see how to do it.