Using different string files in android

I am transferring the iPhone app to android, and now I have a problem with string files.

The application is a translation tool, and users can switch between languages, so all localized strings are in both languages, and they do not depend on which OS language works.

For the iOS version, I have different files like de.strings, en.strings and fr.strings, etc. For each target with a specified language pair, I read rows from string tables, for example. for de-fr, I will include de.strings and fr.strings in the project and set the name of the string tables in the info-list file and read the rows from them. In the end, I have one project containing different goals (with different info list files), and they are all well tuned.

I intend to do the same on the Android platform, but

  • Is only one strings.xml allowed for each project?

  • How to set another build target? for example, my de-fr and de-en translation applications are actually two applications in which the only difference is pairs of languages. How can I install something so that I can use one project to create two applications? In Xcode, this is simple, but I cannot find a solution with eclipse.

  • How to specify for each purpose that strings.xml should be read?

Thank you for the answers, but Please note that I need the language settings of the OS locale, that is, if the user changes the OS locale from en to de, my application still displays localized strings in English. Actually I ask how can I install something like prebuild and download various string files for different purposes.

+4
source share
4 answers

Auto locale selection according to user preferences

strings.xml contains the source text, suggesting the presence of English. 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 to these folders and translate it.

When the application starts, the OS automatically selects the language in accordance with the user settings.

Manual locale selection, overriding user settings

To force the Greek language, 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 must, of course, provide the Greek translation for this to work.

More details

You can check the documentation here: Support for different languages ​​- Android

+5
source

you need to put your localized strings in different folders such as values-es, values-de, values-fr, etc.

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

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

in the values-es folder:

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

etc.

+2
source

You need to create one values ​​folder for each language by adding the ISO code of the language of the language you want to translate into using this format: values-es, values-de, ... In each folder you must add strings.xml with lines of it language. The values ​​folder (with country code) will be the default language.

To select 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); } } 
+1
source

ad 1 . No, you can have as much as you want. See ad 3 more details.

ad 2 ????

ad 3 . To make a choice of language in our application, you must update the context . Then the correct xml will be selected automatically. Read about it to learn how to do it.

0
source

All Articles