user5817470
user5817470

Reputation:

Multiple language support on android?

How can I in my android apps allow for several languages in the easiest way possible? I have seen bigger projects use xml files, and the coding of those isn't hard, but I can't seem to integrate it into the code. The other way which is actually very tricky is using booleans but that would require several languages from the start, and I can only supply 2 at this point.

The languages have to be easy to integrate at a late state, so it doesn't require changing several thousand lines, but the initial work has to be done in an early state(the late state integration is for the languages, the early is for support of it)

I'm using android studio

Upvotes: 2

Views: 7214

Answers (4)

Ahmed Hegazy
Ahmed Hegazy

Reputation: 12605

You should read the documentation. It's surprisingly very easy to incorporate multiple languages in your app as you just need to move all your strings into values/strings.xml and then provide other languages with the same string ids but different values at values-[LANGUAGE_ISO_CODE]/strings.xml.

Example

values/strings.xml

<resources>
    <string name="app_name">Something</string>
</resources>

For Arabic language

values-ar/strings.xml

<resources>
    <string name="app_name">شئ</string>
</resources>

Upvotes: 7

Mohamed Haridy
Mohamed Haridy

Reputation: 39

there are two ways to do so: using systme's language: to detect the system language and then the app will launch with this system's language.To do so, 1. first , you need to have all strings of the app not hard coded, meaning you have to have them all defined in the strings file under values folder 2. go to the strings file and on the top toolbar choose the globe icon and choose the second language you want to add. 3. the system will provide you with a translator where to translate each word of your defined strings. 4. once completed , the system will automatically add new strings file under value folder with the second language. 5. now if you change the phone system's language to the second language , the app will open with the second language automatically.

second way to support language without changing the system's language: 1. develop your code with the main language completely 2. you need ,preferably, to avoid hard coding the strings as well. so define all stings in the string file

  1. for each language you want to add, repeat the activities you want to translate with the new language
  2. add button for each language you need to add to the app
  3. code onclicklistener for each button that will direct the app to the activities of its language

Upvotes: 0

parvez rafi
parvez rafi

Reputation: 482

To add support for more locales, create additional directories inside res/. Each directory's name should adhere to the following format:

-b+[+] For example, values-b+es/ contains string resources for locales with the language code es. Similarly, mipmap-b+es+ES/ contains icons for locales with the es language code and the ES country code.

Android loads the appropriate resources according to the locale settings of the device at runtime. For example, the following are some different resource files for different languages: English strings (default locale), /values/strings.xml:

 <resources>
        <string name="hello">Hello</string>
    </resources> 

    French strings (fr locale), /values-fr/strings.xml:

<resources>
    <string name="hello">Bonjour</string>
</resources> 

    Use the Resources in your App:

   <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/hello" />

Upvotes: 1

liangdrew
liangdrew

Reputation: 16

Define all your text in your strings.xml file and avoid hard-coding your text into your app. You can use a third-party translation service to perform all your translations for you.

Upvotes: 0

Related Questions