1

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

  • Smaller projects use xml files as well, just do it that way, it's pretty straightforward. All you have to do to add a new language is then add an extra xml file, no new code required – Tim Mar 22 '16 at 15:00

4 Answers4

6

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>
Ahmed Hegazy
  • 12,395
  • 5
  • 41
  • 64
  • A couple of questions: First of all, what is a language iso code? Do I make a new folder with that name? How do I enter it into the code(e.g: c.drawText(howDoIEnterTheTextHere, x, y);) –  Mar 22 '16 at 15:06
  • getString(R.string.[STRING_ID]) will get you the string? YES you make the folder with a new name – Ahmed Hegazy Mar 22 '16 at 15:10
  • And is the selection of the strings automatic or manuel(e.g. do you have to select in settings what language you want it or is it based on the language of the device)? –  Mar 22 '16 at 15:21
  • http://stackoverflow.com/questions/7973023/what-is-the-list-of-supported-languages-locales-on-android – Ahmed Hegazy Mar 22 '16 at 15:21
  • It is selected based on the language of the device – Ahmed Hegazy Mar 22 '16 at 15:22
  • Is there a way to do it manually? so it is possible to change language manually without having to change the language of the device? –  Mar 22 '16 at 15:23
  • 1
    It's possible but not recommended http://stackoverflow.com/questions/4985805/set-locale-programatically – Ahmed Hegazy Mar 22 '16 at 15:25
  • That will do it. Thank you –  Mar 22 '16 at 15:27
  • http://stackoverflow.com/questions/2900023/change-language-programatically-in-android http://stackoverflow.com/questions/2264874/changing-locale-within-the-app-itself Other ways to do the same but as I told you it has some spontaneous bugs. – Ahmed Hegazy Mar 22 '16 at 15:28
1

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" />
parvez rafi
  • 464
  • 4
  • 20
0

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.

liangdrew
  • 16
  • 5
0

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