Merge pull request #712 from ultrasonic/addLocaleChooser

Add locale chooser
This commit is contained in:
tzugen 2022-03-21 11:23:10 +01:00 committed by GitHub
commit 44d68a71da
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
12 changed files with 135 additions and 16 deletions

View File

@ -15,9 +15,10 @@ android {
minSdkVersion versions.minSdk
targetSdkVersion versions.targetSdk
resConfigs 'cs', 'de', 'en', 'es', 'fr', 'hu', 'it', 'nl', 'pl', 'pt', 'pt-rBR', 'ru', 'zh-rCN', 'zh-rTW'
}
bundle.language.enableSplit = false
buildTypes {
release {
minifyEnabled true

View File

@ -7,6 +7,8 @@
package org.moire.ultrasonic.activity
import android.app.SearchManager
import android.content.Context
import android.content.ContextWrapper
import android.content.Intent
import android.content.res.ColorStateList
import android.content.res.Resources
@ -53,6 +55,7 @@ import org.moire.ultrasonic.service.RxBus
import org.moire.ultrasonic.subsonic.ImageLoaderProvider
import org.moire.ultrasonic.util.Constants
import org.moire.ultrasonic.util.InfoDialog
import org.moire.ultrasonic.util.LocaleHelper
import org.moire.ultrasonic.util.ServerColor
import org.moire.ultrasonic.util.Settings
import org.moire.ultrasonic.util.Storage
@ -351,6 +354,15 @@ class NavigationActivity : AppCompatActivity() {
}
}
/**
* Apply the customized language settings if needed
*/
override fun attachBaseContext(newBase: Context?) {
val locale = Settings.overrideLanguage
val localeUpdatedContext: ContextWrapper = LocaleHelper.wrap(newBase, locale)
super.attachBaseContext(localeUpdatedContext)
}
private fun loadSettings() {
PreferenceManager.setDefaultValues(this, R.xml.settings, false)
}

View File

@ -115,6 +115,7 @@ object Constants {
const val PREFERENCES_KEY_PAUSE_ON_BLUETOOTH_DEVICE = "pauseOnBluetoothDevice"
const val PREFERENCES_KEY_SINGLE_BUTTON_PLAY_PAUSE = "singleButtonPlayPause"
const val PREFERENCES_KEY_DEBUG_LOG_TO_FILE = "debugLogToFile"
const val PREFERENCES_KEY_OVERRIDE_LANGUAGE = "overrideLanguage"
const val PREFERENCE_VALUE_ALL = 0
const val PREFERENCE_VALUE_A2DP = 1
const val PREFERENCE_VALUE_DISABLED = 2

View File

@ -0,0 +1,50 @@
/*
* LocaleHelper.kt
* Copyright (C) 2009-2021 Ultrasonic developers
*
* Distributed under terms of the GNU GPLv3 license.
*/
package org.moire.ultrasonic.util
import android.annotation.TargetApi
import android.content.Context
import android.content.ContextWrapper
import android.content.res.Configuration
import android.os.Build
import java.util.Locale
/**
* Simple Helper class to "wrap" a context with a new locale.
*/
class LocaleHelper(base: Context?) : ContextWrapper(base) {
companion object {
fun wrap(ctx: Context?, language: String): ContextWrapper {
var context = ctx
if (context != null && language != "") {
val config = context.resources.configuration
val locale = Locale.forLanguageTag(language)
Locale.setDefault(locale)
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
setSystemLocale(config, locale)
} else {
setSystemLocaleLegacy(config, locale)
}
config.setLayoutDirection(locale)
context = context.createConfigurationContext(config)
}
return LocaleHelper(context)
}
@Suppress("DEPRECATION")
private fun setSystemLocaleLegacy(config: Configuration, locale: Locale?) {
config.locale = locale
}
@TargetApi(Build.VERSION_CODES.N)
fun setSystemLocale(config: Configuration, locale: Locale?) {
config.setLocale(locale)
}
}
}

View File

@ -308,12 +308,16 @@ object Settings {
Constants.PREFERENCE_VALUE_A2DP
)
@JvmStatic
var debugLogToFile by BooleanSetting(Constants.PREFERENCES_KEY_DEBUG_LOG_TO_FILE, false)
@JvmStatic
val preferences: SharedPreferences
get() = PreferenceManager.getDefaultSharedPreferences(Util.appContext())
@JvmStatic
val overrideLanguage by StringSetting(Constants.PREFERENCES_KEY_OVERRIDE_LANGUAGE, "")
var useFiveStarRating by BooleanSetting(Constants.PREFERENCES_KEY_USE_FIVE_STAR_RATING, false)
// TODO: Remove in December 2022

View File

@ -399,7 +399,6 @@
<string name="api.subsonic.upgrade_server">Inkompatible Versionen. Bitte den subsonic Server aktualisieren.</string>
<!-- Subsonic features -->
<string name="settings.features_title">Besonderheiten</string>
<string name="settings.five_star_rating_title">Fünf-Stern Bewertung</string>
<string name="settings.five_star_rating_description">Benutze Bewertungssystem mit fünf Sternen anstatt Lieder mit bloß einem Stern zu markieren.</string>

View File

@ -461,7 +461,6 @@
<string name="api.subsonic.upgrade_server">Versiones incompatibles. Por favor actualiza el servidor de Subsonic.</string>
<!-- Subsonic features -->
<string name="settings.features_title">Características</string>
<string name="settings.five_star_rating_title">Use cinco estrellas para las canciones</string>
<string name="settings.five_star_rating_description">Utilice el sistema de calificación de cinco estrellas para canciones
en lugar de simplemente marcar / desmarcar elementos.

View File

@ -461,7 +461,6 @@
<string name="api.subsonic.upgrade_server">Incompatibele versies. Werk je Subsonic-server bij.</string>
<!-- Subsonic features -->
<string name="settings.features_title">Functies</string>
<string name="settings.five_star_rating_title">Vijf sterren gebruiken voor nummers</string>
<string name="settings.five_star_rating_description">Toon vijf sterren om nummers te beoordelen
in plaats van items toe te voegen aan of te verwijderen uit de favorieten.

View File

@ -444,7 +444,6 @@
<string name="api.subsonic.upgrade_server">不兼容的版本。请升级Subsonic 服务。</string>
<!-- Subsonic features -->
<string name="settings.features_title">特性</string>
<string name="settings.five_star_rating_title">为歌曲使用五星评分</string>
<string name="settings.five_star_rating_description">对歌曲使用五星级评级系统
而不是简单地为项目加星标/取消星标。</string>

View File

@ -236,5 +236,40 @@
<item>@string/settings.playback.bluetooth_a2dp</item>
<item>@string/settings.playback.bluetooth_disabled</item>
</string-array>
<string-array name="languageNames" translatable="false">
<item>@string/language.default</item>
<item>@string/language.zh_CN</item>
<item>@string/language.zh_TW</item>
<item>@string/language.cs</item>
<item>@string/language.nl</item>
<item>@string/language.en</item>
<item>@string/language.fr</item>
<item>@string/language.de</item>
<item>@string/language.hu</item>
<item>@string/language.it</item>
<item>@string/language.es</item>
<item>@string/language.pl</item>
<item>@string/language.pt</item>
<item>@string/language.pt_BR</item>
<item>@string/language.ru</item>
</string-array>
<string-array name="languageValues" translatable="false">
<!--suppress CheckTagEmptyBody -->
<item></item>
<item>zh-CN</item>
<item>zh-TW</item>
<item>cs</item>
<item>nl</item>
<item>en</item>
<item>fr</item>
<item>de</item>
<item>hu</item>
<item>it</item>
<item>es</item>
<item>pl</item>
<item>pt</item>
<item>pt-BR</item>
<item>ru</item>
</string-array>
</resources>

View File

@ -96,6 +96,21 @@
<string name="error.label">Error</string>
<string name="jukebox.is_default">Jukebox By Default</string>
<string name="lyrics.nomatch">No lyrics found</string>
<string name="language.default">System default</string>
<string name="language.zh_CN">Chinese (China)</string>
<string name="language.zh_TW">Chinese (Taiwan)</string>
<string name="language.cs">Czech</string>
<string name="language.nl">Dutch</string>
<string name="language.en">English</string>
<string name="language.fr">French</string>
<string name="language.de">German</string>
<string name="language.hu">Hungarian</string>
<string name="language.it">Italian</string>
<string name="language.es">Spanish</string>
<string name="language.pl">Polish</string>
<string name="language.pt">Portuguese</string>
<string name="language.pt_BR">Portuguese (Brazil)</string>
<string name="language.ru">Russian</string>
<string name="main.albums_alphaByArtist">By Artist</string>
<string name="main.albums_alphaByName">By Name</string>
<string name="main.albums_frequent">Most Played</string>
@ -257,6 +272,8 @@
<string name="settings.notifications_title">Notifications</string>
<string name="settings.network_title">Network</string>
<string name="settings.other_title">Other Settings</string>
<string name="settings.override_language">Override the language</string>
<string name="settings.override_language_summary">You need to restart the app after changing the language</string>
<string name="settings.playback_control_title">Playback Control Settings</string>
<string name="settings.preload">Songs To Preload</string>
<string name="settings.preload_1">1 song</string>
@ -466,7 +483,6 @@
<string name="api.subsonic.upgrade_server">Incompatible versions. Please upgrade Subsonic server.</string>
<!-- Subsonic features -->
<string name="settings.features_title">Features</string>
<string name="settings.five_star_rating_title">Use five star rating for songs</string>
<string name="settings.five_star_rating_description">Use five star rating system for songs instead of simply starring/unstarring items.
</string>

View File

@ -122,6 +122,12 @@
a:summary="@string/settings.playback.single_button_bluetooth_device_summary"
a:title="@string/settings.playback.single_button_bluetooth_device"
app:iconSpaceReserved="false"/>
<CheckBoxPreference
a:defaultValue="false"
a:key="use_five_star_rating"
a:summary="@string/settings.five_star_rating_description"
a:title="@string/settings.five_star_rating_title"
app:iconSpaceReserved="false" />
</PreferenceCategory>
<PreferenceCategory
a:title="@string/settings.notifications_title"
@ -343,16 +349,14 @@
a:summary="@string/settings.hide_media_summary"
a:title="@string/settings.hide_media_title"
app:iconSpaceReserved="false"/>
</PreferenceCategory>
<PreferenceCategory
a:title="@string/settings.features_title"
app:iconSpaceReserved="false">
<CheckBoxPreference
a:defaultValue="false"
a:key="use_five_star_rating"
a:summary="@string/settings.five_star_rating_description"
a:title="@string/settings.five_star_rating_title"
app:iconSpaceReserved="false" />
<ListPreference
a:defaultValue=""
a:entries="@array/languageNames"
a:entryValues="@array/languageValues"
a:key="overrideLanguage"
a:title="@string/settings.override_language"
a:summary="@string/settings.override_language_summary"
app:iconSpaceReserved="false"/>
</PreferenceCategory>
<PreferenceCategory
a:title="@string/settings.debug.title"