iamjazzar/rosetta

Not Working on Android 8.0

zeeshanrasool91 opened this issue · 3 comments

Hi Ahmed aljazzar Rosetta was working fine till Android N but not working on Android 8.0

Solution 1:
I Solved it i Added a MyContextWrapper Class
import android.content.Context;
import android.content.res.Configuration;
import android.content.res.Resources;
import android.os.Build;
import android.os.LocaleList;

import java.util.Locale;

public class ContextWrapper extends android.content.ContextWrapper {

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

public static ContextWrapper wrap(Context context, Locale newLocale) {

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

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
        configuration.setLocale(newLocale);

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

        context = context.createConfigurationContext(configuration);

    } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
        configuration.setLocale(newLocale);
        context = context.createConfigurationContext(configuration);

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

    return new ContextWrapper(context);
}

}

and then
I Create a Base Activity and Added Locale there which you were saving in shared preferences
and pass it at runtime.
import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.support.v7.app.AppCompatActivity;

import com.appabilities.occasions.android.rosetta.ContextWrapper;

import java.util.Locale;

public abstract class BaseActivity extends AppCompatActivity {

SharedPreferences mSharedPreferences;

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



@Override
protected void attachBaseContext(Context newBase) {
    mSharedPreferences= PreferenceManager.getDefaultSharedPreferences(newBase);
    Locale locale = new Locale(mSharedPreferences.getString("user_preferred_language", "en"));
    Context context = ContextWrapper.wrap(newBase, locale);
    super.attachBaseContext(context);
}

}

these two files need to be added. and boom solved

Solution 2:

A Locale Helper Class can also help with it
import android.annotation.TargetApi;
import android.content.Context;
import android.content.ContextWrapper;
import android.content.res.Configuration;
import android.os.Build;

import java.util.Locale;

public class LocaleHelper extends ContextWrapper {

/**

  • Created by Muhammad on 07/01/2017.

  • Main idea came from http://stackoverflow.com/questions/40221711/android-context-getresources-updateconfiguration-deprecated/40704077#40704077
    */

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

    @SuppressWarnings("deprecation")
    public static ContextWrapper wrap(Context context, String language) {
    Configuration config = context.getResources().getConfiguration();
    if (!language.equals("")) {
    Locale locale = new Locale(language);
    Locale.setDefault(locale);
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
    setSystemLocale(config, locale);
    } else {
    setSystemLocaleLegacy(config, locale);
    }
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
    config.setLayoutDirection(locale);
    context = context.createConfigurationContext(config);
    } else {
    context.getResources().updateConfiguration(config, context.getResources().getDisplayMetrics());
    }
    }
    return new LocaleHelper(context);
    }

    @SuppressWarnings("deprecation")
    public static Locale getSystemLocaleLegacy(Configuration config) {
    return config.locale;
    }

    @TargetApi(Build.VERSION_CODES.N)
    public static Locale getSystemLocale(Configuration config) {
    return config.getLocales().get(0);
    }

    @SuppressWarnings("deprecation")
    public static void setSystemLocaleLegacy(Configuration config, Locale locale) {
    config.locale = locale;
    }

    @TargetApi(Build.VERSION_CODES.N)
    public static void setSystemLocale(Configuration config, Locale locale) {
    config.setLocale(locale);
    }
    }

and Add this as a Base Activity

import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.support.v7.app.AppCompatActivity;

import com.ahmedjazzar.rosetta.ContextWrapper;

import java.util.Locale;

public abstract class BaseActivity extends AppCompatActivity {
SharedPreferences mSharedPreferences;

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

}

protected void attachBaseContext(Context newBase) {
    mSharedPreferences = PreferenceManager.getDefaultSharedPreferences(newBase);
    Context context = LocaleHelper.wrap(newBase, mSharedPreferences.getString("user_preferred_language", "en"));
    super.attachBaseContext(context);
}

}

Hello @zeeshanrasool91. Thanks for the prompt and I appreciate you trying to solve this. Would you like to open a PR for the fix?

@AhmedAljazzar yes you can review buddy