Skip to content

Commit

Permalink
Refactor language switcher implementation ♻️
Browse files Browse the repository at this point in the history
- Fixes dynamic language switching no longer works
  • Loading branch information
ndegwamartin committed Nov 25, 2020
1 parent 03ef5dd commit 528ca62
Show file tree
Hide file tree
Showing 6 changed files with 47 additions and 27 deletions.
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
VERSION_NAME=3.3.2-SNAPSHOT
VERSION_NAME=4.0.0-SNAPSHOT
VERSION_CODE=1
GROUP=org.smartregister
POM_SETTING_DESCRIPTION=OpenSRP Client Core Application
Expand Down
2 changes: 1 addition & 1 deletion opensrp-app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ dependencies {
implementation 'net.zetetic:android-database-sqlcipher:4.2.0'
implementation 'androidx.multidex:multidex:2.0.1'
implementation 'org.codehaus.jackson:jackson-core-asl:1.9.13'
implementation 'androidx.appcompat:appcompat:1.1.0'
implementation 'androidx.appcompat:appcompat:1.2.0'
implementation 'androidx.legacy:legacy-support-v4:1.0.0'
implementation "org.apache.httpcomponents:httpmime:4.5.6"
implementation group: 'commons-codec', name: 'commons-codec', version: '1.10'
Expand Down
31 changes: 24 additions & 7 deletions opensrp-app/src/main/java/org/smartregister/util/LangUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
import android.content.Context;
import android.content.res.Configuration;
import android.content.res.Resources;
import android.os.Build;
import android.os.LocaleList;
import android.preference.PreferenceManager;

import org.smartregister.repository.AllSharedPreferences;
Expand All @@ -25,19 +27,34 @@ public static String getLanguage(Context ctx) {
return allSharedPreferences.fetchLanguagePreference();
}

public static Context setAppLocale(Context context, String language) {
Locale locale = new Locale(language);

public static Configuration setAppLocale(Context context, String language) {
Resources res = context.getResources();
Configuration conf = res.getConfiguration();
conf.setLocale(locale);
Configuration configuration = res.getConfiguration();

try {
context = context.createConfigurationContext(conf);
Locale locale = new Locale(language);


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

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

context.createConfigurationContext(configuration);

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


} catch (Exception e) {
Timber.e(e);
}

return context;
return configuration;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.res.Configuration;
import android.os.Bundle;
import android.os.Vibrator;
import android.util.SparseArray;
Expand All @@ -31,12 +32,15 @@ public class BarcodeScanActivity extends Activity implements Detector.Processor<
private CameraSource cameraSource;
private CameraSourcePreview cameraSourcePreview;


@Override
protected void attachBaseContext(Context base) {
// get language from prefs
String lang = LangUtils.getLanguage(base.getApplicationContext());
super.attachBaseContext(LangUtils.setAppLocale(base, lang));
Configuration newConfiguration = LangUtils.setAppLocale(base, lang);

super.attachBaseContext(base);

applyOverrideConfiguration(newConfiguration);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
package org.smartregister.view.activity;

import android.content.res.Configuration;
import android.os.Build;

import androidx.appcompat.app.AppCompatActivity;

import org.smartregister.util.LangUtils;


/** Use this class to ensure your activities have multi-language support
/**
* Use this class to ensure your activities have multi-language support
*
* @author Rodgers Andati
* @since 2019-05-03
*/
Expand All @@ -18,20 +19,12 @@ public class MultiLanguageActivity extends AppCompatActivity {
protected void attachBaseContext(android.content.Context base) {
// get language from prefs
String lang = LangUtils.getLanguage(base.getApplicationContext());
super.attachBaseContext(LangUtils.setAppLocale(base, lang));
}
Configuration newConfiguration = LangUtils.setAppLocale(base, lang);

//solution borrowed from
// https://stackoverflow.com/questions/55265834/change-locale-not-work-after-migrate-to-androidx/61420643#61420643
@Override
public void applyOverrideConfiguration(Configuration overrideConfiguration) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP &&
Build.VERSION.SDK_INT < Build.VERSION_CODES.O) {
// add this to fix androidx.appcompat:appcompat 1.1.0 bug
// which happens on Android 6.x ~ 7.x
getResources();
}
super.applyOverrideConfiguration(overrideConfiguration);
super.attachBaseContext(base);

applyOverrideConfiguration(newConfiguration);
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.content.res.Configuration;
import android.os.Bundle;
import android.preference.EditTextPreference;
import android.preference.Preference;
Expand Down Expand Up @@ -32,8 +33,13 @@ public class SettingsActivity extends PreferenceActivity implements Preference.O
@Override
protected void attachBaseContext(android.content.Context base) {
// get language from prefs

String lang = LangUtils.getLanguage(base.getApplicationContext());
super.attachBaseContext(LangUtils.setAppLocale(base, lang));
Configuration newConfiguration = LangUtils.setAppLocale(base, lang);

super.attachBaseContext(base);

applyOverrideConfiguration(newConfiguration);
}

@Override
Expand Down

0 comments on commit 528ca62

Please sign in to comment.