iamjazzar / rosetta

(Help wanted) Android library for multi-language support
https://www.ahmedjazzar.com
MIT License
66 stars 20 forks source link

Not Working on Android 8.0 #29

Open zeeshanrasool91 opened 6 years ago

zeeshanrasool91 commented 6 years ago

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

zeeshanrasool91 commented 6 years ago

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 {

/**

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

}

iamjazzar commented 6 years ago

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

zeeshanrasool91 commented 6 years ago

@ahmedaljazzar yes you can review buddy