How can I create a multilingual android application?
Asked Answered
B

3

30

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?

Bilander answered 23/11, 2010 at 17:49 Comment(0)
B
32

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

Barrybarrymore answered 23/11, 2010 at 17:58 Comment(3)
thanks! do you know if it is possible to link to these string from a custom xml file?Bilander
I'm not sure, sorry. If you ask that as a new question, someone else might know the answer.Barrybarrymore
I expected a complicated solution. But I got something never simpler. Thanks a lot, Colin and Clamp. :-)Cathern
D
11

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>
Dehaven answered 23/11, 2016 at 8:13 Comment(0)
G
3

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

Gooseberry answered 31/1, 2019 at 11:15 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.