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 + &quot;_&quot;
+ langText, &quot;string&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 + &quot;_&quot;
+ langText, &quot;string&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
* — &quot;string&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.
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
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 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.