Commit c4a58f56 authored by Niklas Fix's avatar Niklas Fix 🎓

fix change language functionality with Lingver lib

parent 112c5c99
......@@ -62,6 +62,9 @@ dependencies {
implementation 'com.github.bumptech.glide:glide:4.8.0'
implementation 'org.greenrobot:eventbus:3.2.0'
// LANGUAGE
implementation "com.github.YarikSOffice:lingver:1.3.0"
// FIREBASE
implementation 'com.google.firebase:firebase-analytics:17.4.4'
// Recommended: Add the Firebase SDK for Google Analytics.
......
......@@ -14,7 +14,6 @@
android:supportsRtl="true"
android:theme="@style/WhiteTheme"
tools:ignore="GoogleAppIndexingWarning">
<!--activity android:name=".features.crashlytics.CrashlyticsActivity"></activity-->
<activity
android:name=".features.summary.SummaryActivity"
android:screenOrientation="portrait" />
......@@ -69,6 +68,7 @@
<activity
android:name="de.akamu.tudarmstadt.features.settings.SettingsActivity"
android:label="@string/title_activity_settings"
android:configChanges="locale"
android:parentActivityName=".features.dashboard.MainActivity"
android:screenOrientation="portrait" />
......
......@@ -2,22 +2,25 @@ package de.akamu.tudarmstadt;
import android.app.Application;
import android.content.Context;
import android.content.res.Configuration;
import android.content.SharedPreferences;
import android.util.Log;
import androidx.appcompat.app.AppCompatDelegate;
import androidx.core.content.ContextCompat;
import androidx.preference.PreferenceManager;
import com.yariksoffice.lingver.Lingver;
import org.jetbrains.annotations.NotNull;
import org.json.JSONObject;
import java.util.Locale;
import java.util.Objects;
import de.akamu.tudarmstadt.data.settings.SettingsDataSource;
import de.akamu.tudarmstadt.data.settings.SettingsDataSourceImpl;
import de.akamu.tudarmstadt.interactors.SettingsInteractor;
import de.akamu.tudarmstadt.model.MyObjectBox;
import de.akamu.tudarmstadt.util.Constants;
import de.akamu.tudarmstadt.util.LocaleManager;
import de.akamu.tudarmstadt.util.SettingsPresenter;
import io.objectbox.BoxStore;
......@@ -36,6 +39,9 @@ public class App extends Application implements SettingsDataSource.FetchSettings
@Override
public void onCreate() {
super.onCreate();
SharedPreferences prefs = android.preference.PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
String defaultLanguage = prefs.getString(Constants.USER_LANG, getApplicationContext().getResources().getString(R.string.pref_language_eng_value));
Lingver.init(this, Objects.requireNonNull(defaultLanguage));
boolean isNightMode = PreferenceManager.getDefaultSharedPreferences(getApplicationContext()).getBoolean(Constants.IS_NIGHT_MODE, false);
if (isNightMode) {
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES);
......@@ -52,27 +58,6 @@ public class App extends Application implements SettingsDataSource.FetchSettings
return boxStore;
}
/**
* This method calls the LocaleManager to
* update change language settings.
*/
@Override
protected void attachBaseContext(Context base) {
super.attachBaseContext(LocaleManager.setLocale(base));
Log.d(TAG, "attachBaseContext");
}
/**
* This method calls the LocaleManager to
* update change language settings if configuration changes occur.
*/
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
LocaleManager.setLocale(this);
Log.d(TAG, "onConfigurationChanged: " + newConfig.locale.getLanguage());
}
@Override
public void onFetchSettingsSuccess(@NotNull JSONObject settings) {
System.out.println("Settings fetched successfully.");
......
package de.akamu.tudarmstadt
import android.content.Context
import android.content.Intent
import android.content.pm.PackageManager
import android.content.pm.PackageManager.GET_META_DATA
import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import de.akamu.tudarmstadt.api.V2API
import de.akamu.tudarmstadt.events.JWTRefreshedEvent
......@@ -12,37 +8,12 @@ import de.akamu.tudarmstadt.events.UnauthorizedEvent
import de.akamu.tudarmstadt.features.login.LoginActivity
import de.akamu.tudarmstadt.util.AppUserUtil
import de.akamu.tudarmstadt.util.Constants
import de.akamu.tudarmstadt.util.LocaleManager
import org.greenrobot.eventbus.EventBus
import org.greenrobot.eventbus.Subscribe
abstract class BaseActivity : AppCompatActivity() {
override fun attachBaseContext(newBase: Context?) {
super.attachBaseContext(LocaleManager.setLocale(newBase))
}
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
resetTitle()
}
/**
* Method for resetting the titles in the action bar
* This is used for displaying the correct action bar text
*/
private fun resetTitle() {
try {
val label = packageManager.getActivityInfo(componentName, GET_META_DATA).labelRes
if (label != 0) {
setTitle(label)
}
} catch (e: PackageManager.NameNotFoundException) {
e.printStackTrace()
}
}
public override fun onStart() {
super.onStart()
EventBus.getDefault().register(this)
......
package de.akamu.tudarmstadt.features.settings
import android.content.Intent
import android.content.SharedPreferences
import android.os.Build
import android.os.Bundle
import androidx.preference.*
import com.google.android.gms.oss.licenses.OssLicensesMenuActivity
import com.google.firebase.crashlytics.FirebaseCrashlytics
import com.yariksoffice.lingver.Lingver
import de.akamu.tudarmstadt.R
import de.akamu.tudarmstadt.features.dashboard.MainActivity
import de.akamu.tudarmstadt.features.dse.DSEActivity
import de.akamu.tudarmstadt.util.Constants
import de.akamu.tudarmstadt.util.LocaleManager
class SettingsFragment : PreferenceFragmentCompat(), Preference.OnPreferenceChangeListener,
......@@ -19,10 +20,10 @@ class SettingsFragment : PreferenceFragmentCompat(), Preference.OnPreferenceChan
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
// val langPref = findPreference<ListPreference>(getString(R.string.pref_language_key))
val langPref = findPreference<ListPreference>(getString(R.string.pref_language_key))
val closedDuelPref =
findPreference<ListPreference>(getString(R.string.pref_closed_duels_key))
// langPref!!.onPreferenceChangeListener = this
langPref!!.onPreferenceChangeListener = this
closedDuelPref!!.onPreferenceChangeListener = this
val dseInfoPref = findPreference<Preference>(getString(R.string.pref_privacy_policy_key))
dseInfoPref?.onPreferenceClickListener = this
......@@ -43,7 +44,7 @@ class SettingsFragment : PreferenceFragmentCompat(), Preference.OnPreferenceChan
if (preference != null) {
when (preference.key) {
getString(R.string.pref_language_key) -> {
changeLanguage()
changeLanguage((newValue as String))
return true
}
getString(R.string.pref_closed_duels_key) -> {
......@@ -80,11 +81,26 @@ class SettingsFragment : PreferenceFragmentCompat(), Preference.OnPreferenceChan
startActivity(dse)
}
private fun changeLanguage() {
LocaleManager.setLocale(context)
val refresh = Intent(context, activity!!::class.java)
startActivity(refresh)
private fun changeLanguage(lang: String) {
if (lang == requireActivity().resources.getString(R.string.pref_language_sys_value)) {
Lingver.getInstance().setFollowSystemLocale(requireActivity())
val intent = Intent(requireActivity(), MainActivity::class.java)
requireActivity().finish()
startActivity(intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK))
return
}
val currentLang = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
resources.configuration.locales.get(0).language
} else {
resources.configuration.locale.language
}
if (currentLang == lang) return
PreferenceManager.getDefaultSharedPreferences(requireActivity()).edit()
.putString(Constants.USER_LANG, lang).apply()
Lingver.getInstance().setLocale(requireActivity(), lang)
val intent = Intent(requireActivity(), MainActivity::class.java)
requireActivity().finish()
startActivity(intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK))
}
private fun changeClosedDuels(newValue: String) {
......
......@@ -52,5 +52,6 @@ class Constants {
const val CLOSED_DUELS_KEY = "CLOSED_DUELS_KEY"
const val BUG_REPORTS_ENABLED_KEY = "BUG_REPORTS_ENABLED_KEY"
const val IS_NIGHT_MODE = "IS_NIGHT_MODE"
const val USER_LANG = "USER_LANG"
}
}
\ No newline at end of file
package de.akamu.tudarmstadt.util;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.res.Configuration;
import android.content.res.Resources;
import android.preference.PreferenceManager;
import de.akamu.tudarmstadt.R;
import java.util.Locale;
/**
* This helper class is used for updating the app's locale.
*
* @author Patrick Christ
* @version 1.0.1
* @since 1.0.1
*/
public class LocaleManager {
/**
* Sets the given Context with an Locale including the currently supported language.
*
* @param c the current context
* @return the context set with the appropriate Locale
* @author Patrick Christ
* @version 1.0.1
* @since 1.0.1
*/
public static Context setLocale(Context c) {
return updateResources(c, getLanguage(c));
}
/**
* Returns the language currently stored in SharedPreferences.
*
* @param c the current context
* @return the language saved in SharedPreferences
* @author Patrick Christ
* @version 1.0.1
* @since 1.0.1
*/
private static String getLanguage(Context c) {
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(c);
return prefs.getString(c.getResources().getString(R.string.pref_language_key), c.getResources().getString(R.string.pref_language_eng_value));
}
/**
* Updates the App's resources with the given language.
*
* @param context the current context
* @param language the language to include in App's resources
* @return the context set with the appropriate Locale
* @author Patrick Christ
* @version 1.0.1
* @since 1.0.1
*/
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());
config.setLocale(locale);
context = context.createConfigurationContext(config);
return context;
}
}
\ No newline at end of file
......@@ -208,4 +208,5 @@
<string name="confirm_unfriend">Entfernen bestätigen</string>
<string name="answers">Antworten</string>
<string name="your_swipes">Deine Swipes</string>
<string name="pref_language_label_sys">Systemsprache</string>
</resources>
\ No newline at end of file
......@@ -262,4 +262,5 @@
<string name="confirm_unfriend_info">Do you really want to unfriend %1$s?</string>
<string name="answers">Answers</string>
<string name="your_swipes">Your Swipes</string>
<string name="pref_language_label_sys">System language</string>
</resources>
......@@ -47,10 +47,12 @@
</string-array>
<string-array name="lang_pref_entries">
<item>@string/pref_language_label_sys</item>
<item>@string/pref_language_label_eng</item>
<item>@string/pref_language_label_ger</item>
</string-array>
<string-array name="lang_pref_values">
<item>@string/pref_language_sys_value</item>
<item>@string/pref_language_eng_value</item>
<item>@string/pref_language_ger_value</item>
</string-array>
......
......@@ -166,6 +166,8 @@
<string name="pref_language_key"
translatable="false">language
</string>
<string name="pref_language_sys_value"
translatable="false">sys</string>
<string name="pref_language_eng_value"
translatable="false">en
</string>
......@@ -314,6 +316,7 @@
<string name="no">No</string>
<string name="report">report</string>
<string name="thanks_for_feedback">Thanks for your feedback!</string>
<string name="pref_language_label_sys">System language</string>
<string name="pref_language_label_eng">English</string>
<string name="pref_language_label">Language</string>
<string name="pref_language_label_ger">German</string>
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment