0

I was able to change localization programatically during runtime using the methods explained in this article. I was able to change the language of the app. + layout direction + toolbar language.

Here's My code:

build.gradle

 buildTypes {
        debug {
            pseudoLocalesEnabled true
        }
dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation 'androidx.appcompat:appcompat:1.1.0'
    implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
    implementation 'com.jakewharton:process-phoenix:2.0.0'
}

public class App extends Application {

    @Override
    protected void attachBaseContext(Context base) {
        super.attachBaseContext(LocaleManager.setLocale(base));
    }

    @Override
    public void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);
        LocaleManager.setLocale(this);
        if (Build.VERSION.SDK_INT >= 26) {
            ProcessPhoenix.triggerRebirth(this); //An activity must have a category of Default in Manifest!
        }
    }
}

public class BaseActivity extends AppCompatActivity {

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if(Build.VERSION.SDK_INT>=26) {
            LocaleManager.setLocale(this);
        }
        startActivity(new Intent(this,MainActivity.class));
        finish();
    }
}

public class LocaleManager {
private static final String SELECTED_LANGUAGE = "Locale.Helper.Selected.Language";
private static final String DEFAULT_LANGUAGE = "en";

public static Context setLocale(Context c) {
    return setNewLocale(c, getLanguage(c));
}

public static Context setNewLocale(Context c, String language) {
    persistLanguage(c, language);
    return updateResources(c, language);
}

public static String getLanguage(Context c) {
    SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(c);
    return preferences.getString(SELECTED_LANGUAGE, DEFAULT_LANGUAGE);
}

public static void persistLanguage(Context c, String language) {
    SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(c);
    SharedPreferences.Editor editor = preferences.edit();
    editor.putString(SELECTED_LANGUAGE, language);
    editor.commit(); 
}

private static Context updateResources(Context context, String language) {
    Locale locale = new Locale(language);
    Locale.setDefault(locale);

    Resources res = context.getResources();
    Configuration config = new Configuration(res.getConfiguration());

    if(Build.VERSION.SDK_INT >= 17) {
        config.setLocale(locale);
        res.updateConfiguration(config, res.getDisplayMetrics());
    }
    return context;
}

}


public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
}

//Change English to arabic when user clicked the button.
public void changeLanguage(View view) {
    LocaleManager.persistLanguage(this ,"ar");
    LocaleManager.setLocale(this);
    ProcessPhoenix.triggerRebirth(this); //An activity must have a category of Default in Manifest!

The problem: This code has been working perfectly up till the point where I migrated to androidX. Now, it only works for devices with Android Oreo+. I checked a backup version before the migration to androidX and it is working perfectly. Is there something I am missing here?

Mena
  • 3,019
  • 1
  • 25
  • 54

2 Answers2

0

Changing implementation of 'androidx.appcompat:appcompat:1.1.0' to version 1.0.0

and keeping 'androidx.constraintlayout:constraintlayout:1.1.3' to version 1.1.3

solves the problem.

Also, if you use implementation 'com.google.android.material:material:1.1.0', change it to version 1.0.0

Mena
  • 3,019
  • 1
  • 25
  • 54
0

Runtime Localization Complete solution using SDK 30 (2020)

  • This is tested on emulators starting OS version 19 up to 30. It solves the issues that were reported lately concerning issues on OS Marshmallow + Nougat and using the latest appcompat + material + constrainlayout dependencies.
  • It also handles layout direction for RTL support.

Steps:

  1. Create String XML files for the different languages you want your app to support using proper qualifiers. In my case, I used English and Arabic qualifiers to test Layout RTL.

  2. Add this line pseudoLocalesEnabled true in your module build.gradle under android->build types in both debug and release.

  3. Add this dependency to be able to restart your app easily:

    'implementation 'com.jakewharton:process-phoenix:2.0.0'

  4. Create the following "LocaleManager" java class to handle saving the chosen locale to shared preferences and to apply necessary runtime locale changes.

    public class LocaleManager {
    private static final String SELECTED_LANGUAGE = "Locale.Helper.Selected.Language";
    private static final String DEFAULT_LANGUAGE = "en";
    
    public static Context setLocale(Context c) {
        return setNewLocale(c, getLanguage(c));
    }
    
    public static Context setNewLocale(Context c, String language) {
        persistLanguage(c, language);
        return updateResources(c, language);
    }
    
    public static String getLanguage(Context c) {
        SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(c);
        return preferences.getString(SELECTED_LANGUAGE, DEFAULT_LANGUAGE);
    }
    
    public static void persistLanguage(Context c, String language) {
        SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(c);
        SharedPreferences.Editor editor = preferences.edit();
        editor.putString(SELECTED_LANGUAGE, language);
        editor.commit(); //U must use Commit and not apply, to avoid opening a new thread, causing a delayed writting in a separate thread!
    }
    
    private static Context updateResources(Context context, String language) {
        Locale locale = new Locale(language);
        Locale.setDefault(locale);
    
        Resources res = context.getResources();
        Configuration config = new Configuration(res.getConfiguration());
    
        if(Build.VERSION.SDK_INT >= 17) {
            config.setLocale(locale);
            res.updateConfiguration(config, res.getDisplayMetrics());
        }
        return context;
    }
    
    }
    
  5. Create the following "App" class that extends Application. Make sure that your MainActivity or the launcher Activity has a category of Default in the Manifest.

    public class App extends Application {
    
    @Override
    protected void attachBaseContext(Context base) {
        super.attachBaseContext(LocaleManager.setLocale(base));
        Log.e("lang", LocaleManager.getLanguage(base));
    }
    
    //Handles screen rotation only up till API 28 pie
    @Override
    public void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);
        LocaleManager.setLocale(this);
        if (Build.VERSION.SDK_INT >= 26) {
            ProcessPhoenix.triggerRebirth(this); //An activity must have a category of Default in Manifest!
        }
    }
    }
    

Manifest

<activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.DEFAULT" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

<application
        android:name=".App"
        
  1. Create a button that switches the language in your MainActivity as such:

    public class MainActivity extends AppCompatActivity {
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }
    
    //Change English to arabic when user clicked the button.
    public void changeLanguageToArabic(View view) {
        LocaleManager.persistLanguage(this, "ar");
        LocaleManager.setLocale(this);
        ProcessPhoenix.triggerRebirth(this); //An activity must have a category of Default in Manifest!
    }
    
    
    public void goToSecondActivity(View view) {
        Intent intent = new Intent(this, MainActivity2.class);
        startActivity(intent);
    }
    

Your chosen locale will persist across activities and upon restarting your app.

Mena
  • 3,019
  • 1
  • 25
  • 54