Build Multilingual Android Apps Using Custom Localization

Share this article

In today’s tech-savvy world, companies are craving actionable feedback from their customers, typically in the form of surveys and other customer feedback mechanisms. This has become a common, efficient practice that provides organized data for the company and an easy, straightforward process for the customers themselves. Just a single click saves the data to the common server and eliminates the hassle of paper-based survey forms. Multilingual survey apps are even more useful and powerful, as they help surveyors gather region-specific data and conduct international surveys from a single system. The concept of Custom Localization in Android SDK plays a major role in providing strong support for multilingual interactivity. Custom Localization means that the user should be able to change the app language using the app settings without having to change the language of the entire mobile device. One of the limitations is that the multilingual support for Indian languages is not a part of the Android SDK, which poses a major challenge for developers performing custom localization on Android OS. Using the Android SDK platform, the concept of localization can be easily handled, but the major challenge is the insufficient support of localization for Indian native languages. For any foreign language, the device locale can be set to the language name. For example, users from France can change the language of their device to French, and all of the written content will be converted to the French language. One of the advantages of the following survey-based application is that it supports the regional languages of India, such as Tamil, Marathi, Hindi, and many more.

Description

Android SDK provides an API function that allows the usage of external fonts. These fonts allow rendering of characters and symbols used by different languages. To use this approach, each and every component is initialized in an activity class, and its font type is set to ensure that the correct font is rendered. This approach solves the problem of regional language support, but it cannot be considered a perfect solution, as it creates large amounts of unnecessary objects and references in the activity class. The following points should be noted to develop a multilingual support application: The Android SDK provides a resource type to create custom attributes for views. A custom attribute can be created as a “langtext.” Please refer to the following code snippet for clarity:
<!--?xml version="1.0" encoding="UTF-8"?-->
In the strings.xml file, the string values were created in a key-value pair manner. Below is a demonstration.
</pre>
error

Error // text in english

गलती //text in hindi

తప్పు //text in telugu
<pre>
These strings are created in such a way that by adding the language type (e.g: en, hi, tu) in the value of key_<somevalue>, it will return the actual string value in the selected language. A custom view can be created, which extends the TextView class of Android. In this class, override the setTypeface. Using this method, set the required font file to the textview. Please refer to the following code snippet: [sourcecode language=”java”] package com.wb.mobile.android.ot.ui.customviews; import android.content.Context; import android.content.res.TypedArray; import android.graphics.Typeface; import android.util.AttributeSet; import android.widget.TextView; import com.sample.mobile.android.ot.R; import com.sample.mobile.android.ot.ui.activities.SplashScreen; import com.sample.mobile.android.ot.utils.Constants; import com.sample.mobile.android.ot.utils.Helper; /** * Custom implementation of textview for supporting multiple languages. * * @author magarwal * */ public class MultiLingualTextView extends TextView { private String mLangText; public String getLangText() { return mLangText; } public MultiLingualTextView(Context context) { super(context); } public MultiLingualTextView(Context context, AttributeSet attrs) { super(context, attrs); TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.CustomTextView, 0, 0); String lang = SplashScreen.getUserSettings().getString( Constants.LANG_CONSTANST_STR, Constants.LANG_CONSTANST_EN); String langText = typedArray .getString(R.styleable.CustomTextView_langText); mLangText = langText; if (Helper.checkString(langText)) { this.setText(Helper.getStringResource(getContext(), lang + &amp;quot;_&amp;quot; + langText, &amp;quot;string&amp;quot;)); } } public MultiLingualTextView(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); } @Override public void setTypeface(Typeface tf) { String lang = SplashScreen.getUserSettings().getString( Constants.LANG_CONSTANST_STR, Constants.LANG_CONSTANST_EN); if (lang.equalsIgnoreCase(Constants.LANG_CONSTANST_EN)) { super.setTypeface(tf); } else { super.setTypeface(Helper.getTypeface(getContext())); } } @Override public void setTypeface(Typeface tf, int style) { String lang = SplashScreen.getUserSettings().getString( Constants.LANG_CONSTANST_STR, Constants.LANG_CONSTANST_EN); if (lang.equalsIgnoreCase(Constants.LANG_CONSTANST_EN)) { super.setTypeface(tf); } else { super.setTypeface(Helper.getTypeface(getContext())); } } public void setLangText(String langText) { mLangText = langText; String lang = SplashScreen.getUserSettings().getString( Constants.LANG_CONSTANST_STR, Constants.LANG_CONSTANST_EN); if (Helper.checkString(langText)) { this.setText(Helper.getStringResource(getContext(), lang + &amp;quot;_&amp;quot; + langText, &amp;quot;string&amp;quot;)); } } } [/sourcecode] Now we need to consider the function that retrieves the value of a string resource, which is demonstrated in the following code snippet: [sourcecode language=”java”] /** * get the translated string value associated with a key. * * @param context *            — activity reference for accessing API’s * @param name *            — key for which string is needed. * @param type *            — &amp;quot;string&amp;quot; as in R.string.sth * @return — translated value. */ public static String getStringResource(Context context, String name, String type) { try { int id = context.getResources().getIdentifier(name, type, context.getPackageName()); return context.getResources().getString(id); } catch (Exception e) { Helper.printStackTrace(e); return name; } } [/sourcecode] Applied to the Android OS, this code will render text content in the user’s language of choice, even if that language is one of the unsupported Indian languages. Our custom code makes for multilingual interactivity that could be used anywhere in the world. aalang aalang2
aalang3

Conclusion

If you’re not catering to users beyond your own nation’s borders, you’re likely missing out on huge opportunities. If you’re intimidated by making your app multilingual, hopefully this demonstration shows Android’s multilingual capabilities, as well as custom coding that can accommodate even the unsupported languages. If you’re looking for more users, a bigger audience, or entry into international marketplaces, you’d be crazy not to build multilingual support into your Android app.

Frequently Asked Questions (FAQs) about Building a Multilingual Survey App in Android OS

How can I add more languages to my Android survey app?

Adding more languages to your Android survey app involves creating additional values folders for each language in your project’s res directory. Each folder should contain a strings.xml file with translated text for all the strings used in your app. For example, for French, you would create a folder named values-fr and include a strings.xml file with French translations. Remember to follow the correct locale naming convention.

What is the role of the Locale class in Android app localization?

The Locale class in Android is a crucial component in app localization. It provides a way to describe a specific geographical, political, or cultural region to an application. This information is used to tailor the user experience, including the presentation of numbers, dates, and strings, to the user’s region.

How can I test the localization of my Android survey app?

You can test the localization of your Android survey app by changing the device’s language settings. Alternatively, you can use the Android Emulator to simulate different locales. Remember to test all aspects of your app, including layout, text, and images, to ensure they adapt correctly to different languages and regions.

How can I handle right-to-left languages in my Android survey app?

Android provides support for right-to-left (RTL) languages. To enable RTL layout mirroring, you need to add android:supportsRtl=”true” to the element in your manifest file. Then, use “start” and “end” instead of “left” and “right” in your layout files.

What is the best practice for storing localized strings in Android?

The best practice for storing localized strings in Android is to use the strings.xml file. This file should be located in the values folder of your project’s res directory. For each language you support, create a separate values folder with a suffix indicating the language code, and include a strings.xml file with the translated text.

How can I ensure my Android survey app’s UI adapts to different languages?

To ensure your app’s UI adapts to different languages, you should avoid hard-coding text in your layout files. Instead, use string resources that can be translated and loaded dynamically based on the user’s locale. Also, consider potential layout changes due to longer or shorter text in different languages.

Can I localize my Android survey app’s images and graphics?

Yes, you can localize your app’s images and graphics. Android allows you to provide alternative resources for different locales. Just like with strings, you can create separate drawable folders for each locale and include the localized images.

How can I handle language changes at runtime in my Android survey app?

To handle language changes at runtime, you can use the setLocale() method of the Configuration class. This allows you to change the app’s locale programmatically. However, you need to ensure that your app updates its UI when the locale changes.

What are some common challenges in Android app localization?

Some common challenges in Android app localization include handling text expansion, adapting layouts to different text directions, managing cultural differences in images and colors, and testing the app in different locales.

How can I manage translations for my Android survey app?

You can manage translations for your Android survey app by using a translation management system. This can help you organize and keep track of your translations, collaborate with translators, and ensure consistency across your app. Google also provides the Google Play App Translation Service, which you can use to order professional translations for your app.

Christa JoeChrista Joe
View Author

Christa Joe is a keen blogger and enjoys reading and writing about the latest technology. Software development, mobile software development, and mobile applications are some of her key area of interests. She's also a freelance writer and writes for various software development companies like Q3 technologies. Follow her at her Google+ profile.

Android DiscussionsAndroid Tutorials
Share this article
Read Next
Get the freshest news and resources for developers, designers and digital creators in your inbox each week