[Android] Prepend the system locales to the locales used for default audio and default subtitles

Nicolas Pomepuy git at videolan.org
Tue Mar 25 13:19:18 UTC 2025


vlc-android | branch: master | Nicolas Pomepuy <nicolas at videolabs.io> | Tue Mar 25 07:53:58 2025 +0100| [199929eaa8b162fe7bbab2f218f8946d4fef601d] | committer: Duncan McNamara

Prepend the system locales to the locales used for default audio and default subtitles

Fixes #3179

> https://code.videolan.org/videolan/vlc-android/commit/199929eaa8b162fe7bbab2f218f8946d4fef601d
---

 .../television/ui/preferences/PreferencesAudio.kt  |  4 +--
 .../ui/preferences/PreferencesSubtitles.kt         |  3 +-
 .../main/java/org/videolan/tools/LocaleUtils.kt    | 34 ++++++++++++++++++++--
 .../vlc/gui/preferences/PreferencesAudio.kt        |  4 +--
 .../vlc/gui/preferences/PreferencesSubtitles.kt    |  3 +-
 5 files changed, 39 insertions(+), 9 deletions(-)

diff --git a/application/television/src/main/java/org/videolan/television/ui/preferences/PreferencesAudio.kt b/application/television/src/main/java/org/videolan/television/ui/preferences/PreferencesAudio.kt
index 39b29f9c44..4bc9655abd 100644
--- a/application/television/src/main/java/org/videolan/television/ui/preferences/PreferencesAudio.kt
+++ b/application/television/src/main/java/org/videolan/television/ui/preferences/PreferencesAudio.kt
@@ -43,9 +43,9 @@ import org.videolan.libvlc.util.AndroidUtil
 import org.videolan.resources.VLCInstance
 import org.videolan.tools.AUDIO_DUCKING
 import org.videolan.tools.LocaleUtils
+import org.videolan.tools.LocaleUtils.getLocales
 import org.videolan.tools.RESUME_PLAYBACK
 import org.videolan.tools.Settings
-import org.videolan.tools.putSingle
 import org.videolan.vlc.BuildConfig
 import org.videolan.vlc.R
 import org.videolan.vlc.VlcMigrationHelper
@@ -213,7 +213,7 @@ class PreferencesAudio : BasePreferenceFragment(), SharedPreferences.OnSharedPre
     }
 
     private fun prepareLocaleList() {
-        val localePair = LocaleUtils.getLocalesUsedInProject(BuildConfig.TRANSLATION_ARRAY, getString(R.string.no_track_preference))
+        val localePair = LocaleUtils.getLocalesUsedInProject(BuildConfig.TRANSLATION_ARRAY, getString(R.string.no_track_preference), activity.getLocales())
         preferredAudioTrack.entries = localePair.localeEntries
         preferredAudioTrack.entryValues = localePair.localeEntryValues
     }
diff --git a/application/television/src/main/java/org/videolan/television/ui/preferences/PreferencesSubtitles.kt b/application/television/src/main/java/org/videolan/television/ui/preferences/PreferencesSubtitles.kt
index 9c184b6e74..d2a195829e 100644
--- a/application/television/src/main/java/org/videolan/television/ui/preferences/PreferencesSubtitles.kt
+++ b/application/television/src/main/java/org/videolan/television/ui/preferences/PreferencesSubtitles.kt
@@ -43,6 +43,7 @@ import org.videolan.television.ui.COLOR_PICKER_SELECTED_COLOR
 import org.videolan.television.ui.COLOR_PICKER_TITLE
 import org.videolan.television.ui.ColorPickerActivity
 import org.videolan.tools.LocaleUtils
+import org.videolan.tools.LocaleUtils.getLocales
 import org.videolan.tools.Settings
 import org.videolan.vlc.BuildConfig
 import org.videolan.vlc.R
@@ -255,7 +256,7 @@ class PreferencesSubtitles : BasePreferenceFragment(), SharedPreferences.OnShare
 
 
     private fun prepareLocaleList() {
-        val localePair = LocaleUtils.getLocalesUsedInProject(BuildConfig.TRANSLATION_ARRAY, getString(R.string.no_track_preference))
+        val localePair = LocaleUtils.getLocalesUsedInProject(BuildConfig.TRANSLATION_ARRAY, getString(R.string.no_track_preference), activity.getLocales())
         preferredSubtitleTrack.entries = localePair.localeEntries
         preferredSubtitleTrack.entryValues = localePair.localeEntryValues
     }
diff --git a/application/tools/src/main/java/org/videolan/tools/LocaleUtils.kt b/application/tools/src/main/java/org/videolan/tools/LocaleUtils.kt
index 426626f263..d407d37e1b 100644
--- a/application/tools/src/main/java/org/videolan/tools/LocaleUtils.kt
+++ b/application/tools/src/main/java/org/videolan/tools/LocaleUtils.kt
@@ -4,13 +4,31 @@ import android.annotation.TargetApi
 import android.content.Context
 import android.content.ContextWrapper
 import android.os.Build
-import java.util.*
-import kotlin.collections.ArrayList
+import java.util.Locale
+import java.util.TreeMap
 
 object LocaleUtils {
+
+    fun Context.getLocales(): List<Locale> =
+        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N)
+            ArrayList<Locale>().apply {
+                for (i in 0..resources.configuration.locales.size() - 1)
+                    add(resources.configuration.locales[i])
+            } else
+            listOf(resources.configuration.locale)
+
+    /**
+     * Get locales used in project (the one the app has translations for), prepend the given locales if provided
+     *
+     * @param projectLocales project locales
+     * @param defaultLocaleText the default string to use for the default locale
+     * @param localesToPrepend locales to prepend to the list of project locales
+     * @return a [LocalePair] containing the entries and entry values for the list of locales
+     */
     fun getLocalesUsedInProject(
         projectLocales: Array<String>,
-        defaultLocaleText: String
+        defaultLocaleText: String,
+        localesToPrepend: List<Locale>? = null
     ): LocalePair {
 
         val localesEntry = arrayOfNulls<String>(projectLocales.size)
@@ -47,6 +65,16 @@ object LocaleUtils {
             i++
         }
 
+        localesToPrepend?.reversed()?.forEach {
+            if (finalLocaleEntryValues.contains(it.language)) {
+                val indexToRemove = finalLocaleEntryValues.indexOf(it.language)
+                finalLocaleEntryValues.removeAt(indexToRemove)
+                finalLocaleEntries.removeAt(indexToRemove)
+            }
+            finalLocaleEntries.add(1, it.getDisplayLanguage(it).firstLetterUppercase())
+            finalLocaleEntryValues.add(1, it.language)
+        }
+
         return LocalePair(finalLocaleEntries.toTypedArray(), finalLocaleEntryValues.toTypedArray())
     }
 
diff --git a/application/vlc-android/src/org/videolan/vlc/gui/preferences/PreferencesAudio.kt b/application/vlc-android/src/org/videolan/vlc/gui/preferences/PreferencesAudio.kt
index b1dfc32454..6ea76f3a22 100644
--- a/application/vlc-android/src/org/videolan/vlc/gui/preferences/PreferencesAudio.kt
+++ b/application/vlc-android/src/org/videolan/vlc/gui/preferences/PreferencesAudio.kt
@@ -43,9 +43,9 @@ import org.videolan.resources.AndroidDevices
 import org.videolan.resources.VLCInstance
 import org.videolan.tools.AUDIO_DUCKING
 import org.videolan.tools.LocaleUtils
+import org.videolan.tools.LocaleUtils.getLocales
 import org.videolan.tools.RESUME_PLAYBACK
 import org.videolan.tools.Settings
-import org.videolan.tools.putSingle
 import org.videolan.vlc.BuildConfig
 import org.videolan.vlc.R
 import org.videolan.vlc.VlcMigrationHelper
@@ -196,7 +196,7 @@ class PreferencesAudio : BasePreferenceFragment(), SharedPreferences.OnSharedPre
     }
 
     private fun prepareLocaleList() {
-        val localePair = LocaleUtils.getLocalesUsedInProject(BuildConfig.TRANSLATION_ARRAY, getString(R.string.no_track_preference))
+        val localePair = LocaleUtils.getLocalesUsedInProject(BuildConfig.TRANSLATION_ARRAY, getString(R.string.no_track_preference), requireActivity().getLocales())
         preferredAudioTrack.entries = localePair.localeEntries
         preferredAudioTrack.entryValues = localePair.localeEntryValues
     }
diff --git a/application/vlc-android/src/org/videolan/vlc/gui/preferences/PreferencesSubtitles.kt b/application/vlc-android/src/org/videolan/vlc/gui/preferences/PreferencesSubtitles.kt
index c98402368e..91b2049eb5 100644
--- a/application/vlc-android/src/org/videolan/vlc/gui/preferences/PreferencesSubtitles.kt
+++ b/application/vlc-android/src/org/videolan/vlc/gui/preferences/PreferencesSubtitles.kt
@@ -34,6 +34,7 @@ import com.jaredrummler.android.colorpicker.ColorPreferenceCompat
 import kotlinx.coroutines.launch
 import org.videolan.resources.VLCInstance
 import org.videolan.tools.LocaleUtils
+import org.videolan.tools.LocaleUtils.getLocales
 import org.videolan.tools.Settings
 import org.videolan.vlc.BuildConfig
 import org.videolan.vlc.R
@@ -202,7 +203,7 @@ class PreferencesSubtitles : BasePreferenceFragment(), SharedPreferences.OnShare
     }
 
     private fun prepareLocaleList() {
-        val localePair = LocaleUtils.getLocalesUsedInProject(BuildConfig.TRANSLATION_ARRAY, getString(R.string.no_track_preference))
+        val localePair = LocaleUtils.getLocalesUsedInProject(BuildConfig.TRANSLATION_ARRAY, getString(R.string.no_track_preference), requireActivity().getLocales())
         preferredSubtitleTrack.entries = localePair.localeEntries
         preferredSubtitleTrack.entryValues = localePair.localeEntryValues
     }



More information about the Android mailing list