0

I am developing an Android application in which I want to give options for the user to set their language at runtime. But my data is coming from JSON in English, and I want to set that in the app according to the language selected.

I have tried using various values folders for different locales, but it does not fulfill my requirement, I want to change the data at runtime but my data is changing dynamically.

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
  • You can find a nice example here http://www.androidhive.info/2014/07/android-building-multi-language-supported-app/ – Mr Nice Jul 28 '14 at 10:08
  • It is good to know the [distinction between "language" and "locale"](http://developer.android.com/reference/java/util/Locale.html). – tar Jul 28 '14 at 10:28
  • I want idea to change whole data coming from json to diffent language at run time and set that to text view. – user3231034 Jul 28 '14 at 10:32

1 Answers1

0

Alright, so you want to change the locale at runtime? I've done this before and it works well. Search for "android change locale programmatically" and you will find many discussions, such as Set Locale programmatically. In my situation I wanted to revert to the default locale when the activity closed, but you may want to do something different. Actually, resetting the locale was harder than changing it in the first place, because I had to handle orientation changes.

Caveat: I edited this code a little bit in the Stack Overflow editor, so there may be a couple typos. It has not been run as-is :)

public class MyActivity extends Activity {

    private static final String LOG_TAG = MyActivity.class.getSimpleName();
    private static final String LOCALE_EXTRA = "locale";
    private Locale customLocale;
    private boolean restartingForLocaleChangeFlag;

    protected void useLocale(Locale locale) {
        Intent intent = getIntent();

        if (locale == null) {
            Locale def = Locale.getDefault();
            Log.i(LOG_TAG + ".useLocale", "restarting the activity" +
                                          " in the default locale " + def);
            intent.putExtra(LOCALE_EXTRA, def);
        } else {
            Log.i(LOG_TAG + ".useLocale", "restarting the activity in" +
                                          " the " + locale + " locale");
            intent.putExtra(LOCALE_EXTRA, locale);
        }

        restartingForLocaleChangeFlag = true;
        overridePendingTransition(0, 0);
        intent.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
        finish();
        overridePendingTransition(0, 0);
        startActivity(intent);
    }

    private void simpleSetLocale(Configuration config, Locale loc) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
            config.setLocale(loc);
        } else {
            config.locale = loc;
        }
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        Intent intent = getIntent();
        if (intent.hasExtra(LOCALE_EXTRA)) {
            Object extra = intent.getSerializableExtra(LOCALE_EXTRA);
            if (extra != null && extra instanceof Locale) {
                customLocale = (Locale) extra;
                Resources res = getResources();
                Configuration config = res.getConfiguration();
                simpleSetLocale(customLocale);
                res.updateConfiguration(config, res.getDisplayMetrics());
            }
        }
    }

    @Override
    public void onStop() {
        super.onStop();

        // reset locale
        if (customLocale != null && !restartingForLocaleChangeFlag) {
            customLocale = null;
            Locale defaultLocale = Locale.getDefault();
            Log.i(LOG_TAG + ".useLocale", "reverting the locale to" +
                                      " the default " + defaultLocale);
            Resources res = getResources();
            Configuration config = res.getConfiguration();
            simpleSetLocale(config, defaultLocale);
            res.updateConfiguration(config, res.getDisplayMetrics());
        }
    }

}

Gist

Community
  • 1
  • 1
tar
  • 1,538
  • 1
  • 20
  • 33