26

I would like to create a multilingual android application.

Is there a way to detect what language the user prefers?

Is there a recommended way to manage multiple languages on Android or should I reinvent the wheel?

John
  • 15,418
  • 12
  • 44
  • 65
clamp
  • 33,000
  • 75
  • 203
  • 299

3 Answers3

28

Yes, there is a recommended way to manage multiple languages

Multilanguage support is easy done for android. Create a new values directory for the language with the suffix of the language code. For german: values-de or french: values-fr than copy your string.xml into that and translate each entry. Thats all you need.

Do android support multiple languages?

Providing you follow the recommendations, detecting which language the user prefers is automatic.

Have a read of this:

http://developer.android.com/guide/topics/resources/localization.html

Community
  • 1
  • 1
Colin Pickard
  • 45,724
  • 13
  • 98
  • 148
  • thanks! do you know if it is possible to link to these string from a custom xml file? – clamp Nov 24 '10 at 14:14
  • I'm not sure, sorry. If you ask that as a new question, someone else might know the answer. – Colin Pickard Nov 24 '10 at 14:24
  • 1
    I expected a complicated solution. But I got something never simpler. Thanks a lot, Colin and Clamp. :-) –  Feb 25 '12 at 11:14
9

In Activity file

public boolean onOptionsItemSelected(MenuItem item)
{
    String languageToLoad="en";

    switch (item.getItemId()) {
        case R.id.eng:
             languageToLoad = "en";
            break;
        case R.id.hn:
            languageToLoad = "hi";
            break;

        case R.id.te:
            languageToLoad = "te";
            break;

        case R.id.ta:
            languageToLoad = "ta";
            break;

        case R.id.ka:
            languageToLoad = "kan";
            break;

        case R.id.ml:
            languageToLoad = "ml";
            break;

        case R.id.mr:
            languageToLoad = "mr";
            break;

        default:
            break;
    }

         Locale locale = new Locale(languageToLoad);
    Locale.setDefault(locale);
    Configuration config = new Configuration();
    config.locale = locale;
    getResources().updateConfiguration(config,getResources().getDisplayMetrics());


}

In res\menu\menus.xml

<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
tools:context="com.example.connect.OrderProcess">
        <item
            android:title="Language"
            app:showAsAction="never">
            <menu>
                <item
                    android:id="@+id/eng"
                    android:title="English"/>
                <item
                    android:id="@+id/hn"
                    android:title="Hindi"/>
                <item
                    android:id="@+id/te"
                    android:title="Telugu"/>
                <item
                    android:id="@+id/ta"
                    android:title="Tamil"/>
                <item
                    android:id="@+id/ka"
                    android:title="Kannada"/>
                <item
                    android:id="@+id/ml"
                    android:title="Malayalam"/>
                <item
                    android:id="@+id/mr"
                    android:title="Marathi"/>
            </menu>
        </item>
   </menu>

AND Create folder and file

res\values\string.xml (English)

res\values-hi\string.xml (Hindi)

res\values-kan\string.xml (Kannada)

res\values-te\string.xml (Telugu)

res\values-ta\string.xml (Tamil)

res\values-ml\string.xml(Malayalam)

res\values-mr\string.xml (Marathi)

In string.xml (Hindi)

 <resources> 
<string name="email">ईमेल</string>
<string name="password">पासवर्ड </string>
 </resources>
Kishor N R
  • 1,521
  • 1
  • 17
  • 23
1

Know that it's a late post, thought of sharing a demo application, so it will be helpful for some people.

Multi-language app using shared preferences

This demo showcase the following two scenario's,

  • Select language from drop down list.
  • Choose language from Bottom Sheet design

The selected language is stored in shared preferences. So next time once the app is open, the prefered language will be choosen automatically.

Source code - https://github.com/anurajr1/Multi-language_App

Anuraj R
  • 524
  • 2
  • 8
  • 24