Build Multilingual Android Apps Using Custom Localization

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:

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;));
		}
	}

}

Now we need to consider the function that retrieves the value of a string resource, which is demonstrated in the following code snippet:

/**

* 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;

}

}

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.

Free book: Jump Start HTML5 Basics

Grab a free copy of one our latest ebooks! Packed with hints and tips on HTML5's most powerful new features.

No Reader comments

Comments on this post are closed.