Add support for language switching (#1108)

* Add support for language switching

* Clean and improve code based on feedback

* Remove useless logging
This commit is contained in:
Mélanie Chauvel (ariasuni) 2019-03-08 20:33:42 +01:00 committed by Konrad Pozniak
parent 0506bfa7d6
commit bac131c6d5
8 changed files with 171 additions and 22 deletions

View File

@ -16,6 +16,7 @@
package com.keylesspalace.tusky;
import android.app.ActivityManager;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Bitmap;
@ -99,6 +100,11 @@ public abstract class BaseActivity extends AppCompatActivity implements Injectab
callList = new ArrayList<>();
}
@Override
protected void attachBaseContext(Context base) {
super.attachBaseContext(TuskyApplication.localeManager.setLocale(base));
}
protected boolean requiresLogin() {
return true;
}

View File

@ -125,16 +125,9 @@ class PreferencesActivity : BaseActivity(), SharedPreferences.OnSharedPreference
val theme = sharedPreferences.getNonNullString("appTheme", ThemeUtils.APP_THEME_DEFAULT)
Log.d("activeTheme", theme)
ThemeUtils().setAppNightMode(theme, this)
restartActivitiesOnExit = true
// recreate() could be used instead, but it doesn't have an animation B).
intent.flags = Intent.FLAG_ACTIVITY_CLEAR_TOP or Intent.FLAG_ACTIVITY_NEW_TASK
val savedInstanceState = Bundle()
saveInstanceState(savedInstanceState)
intent.putExtras(savedInstanceState)
startActivityWithSlideInAnimation(intent)
finish()
overridePendingTransition(R.anim.fade_in, R.anim.fade_out)
restartActivitiesOnExit = true
this.restartCurrentActivity()
// MODE_NIGHT_FOLLOW_SYSTEM workaround part 2 :/
when(theme){
@ -143,7 +136,6 @@ class PreferencesActivity : BaseActivity(), SharedPreferences.OnSharedPreference
}
}
//workaround end
}
"statusTextSize" -> {
restartActivitiesOnExit = true
@ -151,11 +143,24 @@ class PreferencesActivity : BaseActivity(), SharedPreferences.OnSharedPreference
"absoluteTimeView" -> {
restartActivitiesOnExit = true
}
"language" -> {
restartActivitiesOnExit = true
this.restartCurrentActivity()
}
}
eventHub.dispatch(PreferenceChangedEvent(key))
}
private fun restartCurrentActivity() {
intent.flags = Intent.FLAG_ACTIVITY_CLEAR_TOP or Intent.FLAG_ACTIVITY_NEW_TASK
val savedInstanceState = Bundle()
saveInstanceState(savedInstanceState)
intent.putExtras(savedInstanceState)
startActivityWithSlideInAnimation(intent)
finish()
overridePendingTransition(R.anim.fade_in, R.anim.fade_out)
}
override fun onBackPressed() {
/* Switching themes won't actually change the theme of activities on the back stack.

View File

@ -20,7 +20,10 @@ import android.app.Application;
import android.app.Service;
import androidx.room.Room;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.res.Configuration;
import android.preference.PreferenceManager;
import android.util.Log;
import androidx.emoji.text.EmojiCompat;
import com.evernote.android.job.JobManager;
@ -29,6 +32,7 @@ import com.keylesspalace.tusky.db.AccountManager;
import com.keylesspalace.tusky.db.AppDatabase;
import com.keylesspalace.tusky.di.AppInjector;
import com.keylesspalace.tusky.util.EmojiCompatFont;
import com.keylesspalace.tusky.util.LocaleManager;
import com.keylesspalace.tusky.util.NotificationPullJobCreator;
import com.squareup.picasso.Picasso;
@ -62,6 +66,10 @@ public class TuskyApplication extends Application implements HasActivityInjector
private ServiceLocator serviceLocator;
public static LocaleManager localeManager;
private final String TAG = "TuskyApplication";
@Override
public void onCreate() {
super.onCreate();
@ -103,6 +111,18 @@ public class TuskyApplication extends Application implements HasActivityInjector
Security.insertProviderAt(Conscrypt.newProvider(), 1);
}
@Override
protected void attachBaseContext(Context base) {
localeManager = new LocaleManager(base);
super.attachBaseContext(localeManager.setLocale(base));
}
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
localeManager.setLocale(this);
}
/**
* This method will load the EmojiCompat font which has been selected.
* If this font does not work or if the user hasn't selected one (yet), it will use a
@ -157,4 +177,4 @@ public class TuskyApplication extends Application implements HasActivityInjector
public interface ServiceLocator {
<T> T get(Class<T> clazz);
}
}
}

View File

@ -62,6 +62,8 @@ class PreferencesFragment : PreferenceFragmentCompat() {
true
}
val languagePreference: Preference = findPreference("language")
languagePreference.icon = IconicsDrawable(languagePreference.context, GoogleMaterial.Icon.gmd_translate).sizePx(iconSize).color(ThemeUtils.getColor(languagePreference.context, R.attr.toolbar_icon_tint))
}
override fun onResume() {
@ -75,22 +77,22 @@ class PreferencesFragment : PreferenceFragmentCompat() {
val sharedPreferences = preferenceManager.sharedPreferences
val httpProxyEnabled = sharedPreferences.getBoolean("httpProxyEnabled", false)
val httpProxyEnabled = sharedPreferences.getBoolean("httpProxyEnabled", false)
val httpServer = sharedPreferences.getNonNullString("httpProxyServer", "")
val httpServer = sharedPreferences.getNonNullString("httpProxyServer", "")
try {
val httpPort = sharedPreferences.getNonNullString("httpProxyPort", "-1").toInt()
try {
val httpPort = sharedPreferences.getNonNullString("httpProxyPort", "-1").toInt()
if (httpProxyEnabled && httpServer.isNotBlank() && httpPort > 0 && httpPort < 65535) {
httpProxyPref.summary = "$httpServer:$httpPort"
return
}
} catch (e: NumberFormatException) {
// user has entered wrong port, fall back to empty summary
if (httpProxyEnabled && httpServer.isNotBlank() && httpPort > 0 && httpPort < 65535) {
httpProxyPref.summary = "$httpServer:$httpPort"
return
}
} catch (e: NumberFormatException) {
// user has entered wrong port, fall back to empty summary
}
httpProxyPref.summary = ""
httpProxyPref.summary = ""
}

View File

@ -0,0 +1,46 @@
/* Copyright 2019 Mélanie Chauvel (ariasuni)
*
* This file is a part of Tusky.
*
* This program is free software; you can redistribute it and/or modify it under the terms of the
* GNU General Public License as published by the Free Software Foundation; either version 3 of the
* License, or (at your option) any later version.
*
* Tusky is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even
* the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
* Public License for more details.
*
* You should have received a copy of the GNU General Public License along with Tusky; if not,
* see <http://www.gnu.org/licenses>. */
package com.keylesspalace.tusky.util
import android.content.Context
import android.content.SharedPreferences
import android.content.res.Configuration
import android.content.res.Resources
import android.preference.PreferenceManager
import java.util.Locale
import com.keylesspalace.tusky.util.getNonNullString
class LocaleManager(context: Context) {
private var prefs: SharedPreferences = PreferenceManager.getDefaultSharedPreferences(context)
fun setLocale(context: Context): Context {
val language = prefs.getNonNullString("language", "default")
if (language.equals("default")) {
return context;
}
val locale = Locale.forLanguageTag(language)
Locale.setDefault(locale)
val res = context.getResources()
val config = Configuration(res.getConfiguration());
config.setLocale(locale)
return context.createConfigurationContext(config)
}
}

View File

@ -31,4 +31,65 @@
<item>auto_system</item>
</string-array>
<string-array name="language_entries">
<item>@string/system_default</item>
<item>Català</item>
<item>Cymraeg</item>
<item>Deutsch</item>
<item>English</item>
<item>Español</item>
<item>Euskara</item>
<item>Français</item>
<item>Italiano</item>
<item>Magyar</item>
<item>Nederlands</item>
<item>Occitan</item>
<item>Polski</item>
<item>Português (Brasil)</item>
<item>Svenska</item>
<item>Türkçe</item>
<item>Русский</item>
<item>العَرَبِيَّة‎</item>
<item>فارسی</item>
<item>தமிழ்</item>
<item>한국어</item>
<item>中文(台灣)</item>
<item>中文(新加坡)</item>
<item>中文(澳門)</item>
<item>中文(简体)</item>
<item>中文(香港)</item>
<item>日本語</item>
</string-array>
<string-array name="language_values">
<item>default</item>
<item>ca</item>
<item>cy</item>
<item>de</item>
<item>en</item>
<item>es</item>
<item>eu</item>
<item>fr</item>
<item>it</item>
<item>hu</item>
<item>nl</item>
<item>oc</item>
<item>pl</item>
<item>pt-BR</item>
<item>sv</item>
<item>tr</item>
<item>ru</item>
<item>ar</item>
<item>fa</item>
<item>ta</item>
<item>ko</item>
<item>zh-TW</item>
<item>zh-SG</item>
<item>zh-MO</item>
<item>zh-CN</item>
<item>zh-HK</item>
<item>ja</item>
</string-array>
<string name="pref_title_language_settings">Language</string>
</resources>

View File

@ -211,6 +211,7 @@
<string name="pref_title_browser_settings">Browser</string>
<string name="pref_title_custom_tabs">Use Chrome Custom Tabs</string>
<string name="pref_title_hide_follow_button">Hide compose button while scrolling</string>
<string name="pref_title_language">Language</string>
<string name="pref_title_status_filter">Timeline filtering</string>
<string name="pref_title_status_tabs">Tabs</string>
<string name="pref_title_show_boosts">Show boosts</string>

View File

@ -21,6 +21,14 @@
android:icon="@drawable/ic_emoji_24dp"
android:title="@string/emoji_style" />
<ListPreference
android:defaultValue="default"
android:entries="@array/language_entries"
android:entryValues="@array/language_values"
android:key="language"
android:summary="%s"
android:title="@string/pref_title_language" />
<ListPreference
android:defaultValue="medium"
android:entries="@array/status_text_size_names"