Skip to content

Commit

Permalink
Merge pull request #701 from OpenSRP/change-locale-not-work-after-mig…
Browse files Browse the repository at this point in the history
…ration-to-androidx

Fix change locale not working after migration to androidx bug
  • Loading branch information
ndegwamartin authored Nov 26, 2020
2 parents 99a954e + 528ca62 commit 20bc6c3
Show file tree
Hide file tree
Showing 6 changed files with 50 additions and 14 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.1-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,11 +1,15 @@
package org.smartregister.view.activity;

import android.content.res.Configuration;

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 @@ -15,7 +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);

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 20bc6c3

Please sign in to comment.