Enable localization for app strings

A setting has been added to override the system default language, should a user want a different language for the app.
This commit is contained in:
lynxnb 2023-02-06 18:21:44 +01:00 committed by Niccolò Betto
parent 8e7455fb04
commit 787f2bde02
6 changed files with 86 additions and 0 deletions

View File

@ -40,6 +40,13 @@ android {
// Only enable separate process for release builds
manifestPlaceholders += [emulationProcess: ""]
def locales = ["en", "de", "el", "es", "es-419", "fr", "hu", "id", "it", "ja", "ko", "pl", "ru", "ta", "zh-Hans", "zh-Hant"]
// Add available locales to the build config so that they can be accessed from the app
buildConfigField "String[]", "AVAILABLE_APP_LANGUAGES", "new String[]{\"" + locales.join("\",\"") + "\"}"
// Uncomment the following line whenever AAPT2 will properly support BCP47 language tags
//resourceConfigurations += locales
}
/* JVM Bytecode Options */

View File

@ -20,6 +20,7 @@
android:icon="@drawable/logo_skyline"
android:isGame="true"
android:label="${appLabel}"
android:localeConfig="@xml/locales_config"
android:supportsRtl="true"
android:theme="@style/AppTheme"
tools:ignore="GoogleAppIndexingWarning,UnusedAttribute">
@ -116,6 +117,15 @@
</intent-filter>
</provider>
<service
android:name="androidx.appcompat.app.AppLocalesMetadataHolderService"
android:enabled="false"
android:exported="false">
<meta-data
android:name="autoStoreLocales"
android:value="true" />
</service>
<meta-data
android:name="com.android.graphics.injectLayers.enable"
android:value="true" />

View File

@ -0,0 +1,44 @@
/*
* SPDX-License-Identifier: MPL-2.0
* Copyright © 2023 Skyline Team and Contributors (https://github.com/skyline-emu/)
*/
package emu.skyline.preference
import android.content.Context
import android.util.AttributeSet
import androidx.appcompat.app.AppCompatDelegate
import androidx.core.os.LocaleListCompat
import androidx.preference.ListPreference
import androidx.preference.Preference.OnPreferenceChangeListener
import androidx.preference.Preference.SummaryProvider
import emu.skyline.BuildConfig
import emu.skyline.R
import java.util.*
private const val SystemLanguage = "syslang"
class LanguagePreference @JvmOverloads constructor(context : Context, attrs : AttributeSet? = null, defStyleAttr : Int = androidx.preference.R.attr.preferenceStyle) : ListPreference(context, attrs, defStyleAttr) {
init {
entries = arrayOf(systemEntry) + BuildConfig.AVAILABLE_APP_LANGUAGES.map { Locale.forLanguageTag(it).run { getDisplayName(this) } }
entryValues = arrayOf(SystemLanguage) + BuildConfig.AVAILABLE_APP_LANGUAGES
value = AppCompatDelegate.getApplicationLocales()[0]?.toLanguageTag() ?: SystemLanguage
onPreferenceChangeListener = OnPreferenceChangeListener { _, newValue ->
if (newValue == SystemLanguage) {
AppCompatDelegate.setApplicationLocales(LocaleListCompat.getEmptyLocaleList())
} else {
val newLocale = LocaleListCompat.forLanguageTags(newValue as String)
AppCompatDelegate.setApplicationLocales(newLocale)
}
true
}
summaryProvider = SummaryProvider<ListPreference> {
AppCompatDelegate.getApplicationLocales()[0]?.displayName ?: systemEntry
}
}
private val systemEntry : String get() = context.getString(R.string.app_language_default)
}

View File

@ -41,6 +41,8 @@
<!-- Settings - Appearance -->
<string name="appearance">Appearance</string>
<string name="theme">Theme</string>
<string name="app_language">App Language</string>
<string name="app_language_default">Use System Default</string>
<string name="layout_type">Game Display Layout</string>
<string name="sort_apps_by">Games Sorting Order</string>
<string name="group_by_format">Group Games By Format</string>

View File

@ -28,6 +28,10 @@
app:key="app_theme"
app:title="@string/theme"
app:useSimpleSummaryProvider="true" />
<emu.skyline.preference.LanguagePreference
app:key="app_language"
app:persistent="false"
app:title="@string/app_language" />
<emu.skyline.preference.IntegerListPreference
android:defaultValue="1"
android:entries="@array/layout_type"

View File

@ -0,0 +1,19 @@
<?xml version="1.0" encoding="utf-8"?>
<locale-config xmlns:android="http://schemas.android.com/apk/res/android">
<locale android:name="en" /> <!-- English (default) -->
<locale android:name="de" /> <!-- German -->
<locale android:name="el" /> <!-- Greek -->
<locale android:name="es" /> <!-- Spanish -->
<locale android:name="es-419" /> <!-- Latin American Spanish -->
<locale android:name="fr" /> <!-- French -->
<locale android:name="hu" /> <!-- Hungarian -->
<locale android:name="id" /> <!-- Indonesian -->
<locale android:name="it" /> <!-- Italian -->
<locale android:name="ja" /> <!-- Japanese -->
<locale android:name="ko" /> <!-- Korean -->
<locale android:name="pl" /> <!-- Polish -->
<locale android:name="ru" /> <!-- Russian -->
<locale android:name="ta" /> <!-- Tamil -->
<locale android:name="zh-Hans" /> <!-- Simplified Chinese -->
<locale android:name="zh-Hant" /> <!-- Traditional Chinese -->
</locale-config>