Localization in Android

What is Localization?

Localization is more than just the translation of the strings in your application in other languages.The locale is a combination of the language and the country.

In this tutorial we will walk through the basic steps for Android localization with Android Studio.

How do I Implement?

Steps:

  1. Create new android studio project with name of LocalizationDemo.
  2. Open strings.xml in /res/values/strings.xml in your project.In this file where you declare all text/messages of your project.
  3. The localized string resources for your application must be in the res/values-<language code> or res/values-<language code>-<country code> folder.

Here is an example of your default string resources in res/values/strings.xml. We usually supply resources here in the en-US locale.

<?xml version="1.0" encoding="utf-8"?>                       <resources>
<string name="app_name">Localization Demo</string>
<string name="str_hello">Hello</string>
</resources>

An alternate version of the above resource file in Hindi as spoken in Hindi will be placed in /res/values-hi/strings.xml.

<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">स्थानीयकरण डेमो</string>
<string name="str_hello">हैलो</string>
</resources>

4. Create ContextWapper class in your project.

public class MyContextWrapper extends android.content.ContextWrapper {

public MyContextWrapper(Context base) {
super(base);
}

public static MyContextWrapper wrap(Context context) {

Resources res = context.getResources();
Configuration configuration = res.getConfiguration();

Locale newLocale = new Locale(AppUtil.getSavedLanguage(context));

if (Build.VERSION.SDK_INT >= 24) { // API at-least 24
configuration.setLocale(newLocale);

LocaleList localeList = new LocaleList(newLocale);
LocaleList.setDefault(localeList);
configuration.setLocales(localeList);

context = context.createConfigurationContext(configuration);

} else if (Build.VERSION.SDK_INT >= 17) { // API at-least 17
configuration.setLocale(newLocale);
context = context.createConfigurationContext(configuration);

} else {
configuration.locale = newLocale;
res.updateConfiguration(configuration, res.getDisplayMetrics());
}

return new MyContextWrapper(context);
}
}

5. In your MainActivity or BaseActivity , override below methods:

@Override
protected void attachBaseContext(Context newBase) {
Context context = MyContextWrapper.wrap(newBase);
super.attachBaseContext(context);
}
// If you don't override below method then It was not working in vivo,oppo,huwai device.@Override
public void applyOverrideConfiguration(Configuration overrideConfiguration) {
if (overrideConfiguration != null) {
int uiMode = overrideConfiguration.uiMode;
overrideConfiguration.setTo(getBaseContext().getResources().getConfiguration());
overrideConfiguration.uiMode = uiMode;
}
super.applyOverrideConfiguration(overrideConfiguration);}

6. Add Button to your .xml file using that button click you want to implement localization. You can open dialog to choose which language you want to convert on clicking on Button.

Here is the method:

// add this string into your SharedPrefernce class
public static final
String SELECTED_LANGUAGE = "SELECTED_LANGUAGE";
public static final String LANGUAGE_ENGLISH = "en";
public static final String LANGUAGE_HINDI = "hi";
private void showLanguageSelectionDialog() {
final String[] grpname = new String[]{"English","Hindi"};
// you can add text into string.xml file
String savedLanguage = SP.getPreferences(mContext, SP.SELECTED_LANGUAGE);

AlertDialog.Builder alt_bld = new AlertDialog.Builder(this);
alt_bld.setIcon(R.mipmap.logo);
alt_bld.setTitle(getString(R.string.change_language));
alt_bld.setSingleChoiceItems(grpname, savedLanguage.equalsIgnoreCase(SP.LANGUAGE_HINDI) ? 1 : 0, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
AppUtil.changeLocalization(MainActivity.this, which);
dialog.dismiss();
}
});
AlertDialog alert = alt_bld.create();
alert.show();

}

7. Create methods for store language which is used from dialog in your Utils Class.Here is method:

public static void changeLocalization(Activity activity, int position) {
String savedLanguage = SP.getPreferences(activity, SP.SELECTED_LANGUAGE);
String languagetTemp = "";
switch (position) {
case 0:
languagetTemp = SP.LANGUAGE_ENGLISH;
break;
case 1:
languagetTemp = SP.LANGUAGE_HINDI;
break;
}
SP.savePreferences(activity, SP.SELECTED_LANGUAGE, languagetTemp);
Intent intent = new Intent(activity.getString(R.string.broadcast_language_changed));
LocalBroadcastManager.getInstance(activity).sendBroadcast(intent);

activity.recreate();

}

public static String getSavedLanguage(Context context) {
String savedLanguage = SP.getPreferences(context, SP.SELECTED_LANGUAGE);
return !TextUtils.isEmpty(savedLanguage) ? savedLanguage : SP.LANGUAGE_ENGLISH;
}

8. Implement broadcast into your Base Activity(with step no: 5).Here is the code:

private BroadcastReceiver mMessageReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
recreate();
}
};
// register your broadcast into onCreate() method
LocalBroadcastManager.getInstance(this).registerReceiver(mMessageReceiver,newIntentFilter(getString(R.string.broadcast_language_changed)));
//override onDestroy() and unregister your broadcast
@Override
protected void onDestroy() {
LocalBroadcastManager.getInstance(this).unregisterReceiver(mMessageReceiver);
super.onDestroy();
}

Thank You.

--

--