[Android] Migrate subtitles languages

Nicolas Pomepuy git at videolan.org
Thu Nov 21 13:19:10 UTC 2024


vlc-android | branch: master | Nicolas Pomepuy <nicolas at videolabs.io> | Fri Oct 25 07:29:04 2024 +0200| [0373d71abbdc5c2466bf1a812edaa7a4f98b5bd6] | committer: Duncan McNamara

Migrate subtitles languages

> https://code.videolan.org/videolan/vlc-android/commit/0373d71abbdc5c2466bf1a812edaa7a4f98b5bd6
---

 .../resources/src/main/res/values/arrays.xml       | 136 +++++++++++++++++++++
 .../main/java/org/videolan/tools/LocaleUtils.kt    |  14 ++-
 .../org/videolan/vlc/viewmodels/SubtitlesModel.kt  |  27 +++-
 3 files changed, 170 insertions(+), 7 deletions(-)

diff --git a/application/resources/src/main/res/values/arrays.xml b/application/resources/src/main/res/values/arrays.xml
index b5297b31e2..f8fdc1ccf9 100644
--- a/application/resources/src/main/res/values/arrays.xml
+++ b/application/resources/src/main/res/values/arrays.xml
@@ -165,6 +165,142 @@
         <item>ze</item>
     </string-array>
 
+    <string-array name="old_language_entries">
+        <item>Albanian</item>
+        <item>Arabic</item>
+        <item>Armenian</item>
+        <item>Basque</item>
+        <item>Bengali</item>
+        <item>Bosnian</item>
+        <item>Breton</item>
+        <item>Bulgarian</item>
+        <item>Burmese</item>
+        <item>Catalan</item>
+        <item>Chinese (simplified)</item>
+        <item>Croatian</item>
+        <item>Czech</item>
+        <item>Danish</item>
+        <item>Dutch</item>
+        <item>English</item>
+        <item>Esperanto</item>
+        <item>Estonian</item>
+        <item>Finnish</item>
+        <item>French</item>
+        <item>Galician</item>
+        <item>Georgian</item>
+        <item>German</item>
+        <item>Greek</item>
+        <item>Hebrew</item>
+        <item>Hindi</item>
+        <item>Hungarian</item>
+        <item>Icelandic</item>
+        <item>Indonesian</item>
+        <item>Italian</item>
+        <item>Japanese</item>
+        <item>Kazakh</item>
+        <item>Khmer</item>
+        <item>Korean</item>
+        <item>Latvian</item>
+        <item>Lithuanian</item>
+        <item>Luxembourgish</item>
+        <item>Macedonian</item>
+        <item>Malay</item>
+        <item>Malayalam</item>
+        <item>Mongolian</item>
+        <item>Norwegian</item>
+        <item>Occitan</item>
+        <item>Persian</item>
+        <item>Polish</item>
+        <item>Portuguese</item>
+        <item>Brazilian Portuguese</item>
+        <item>Romanian</item>
+        <item>Russian</item>
+        <item>Serbian</item>
+        <item>Sinhalese</item>
+        <item>Slovak</item>
+        <item>Slovenian</item>
+        <item>Spanish</item>
+        <item>Swahili</item>
+        <item>Swedish</item>
+        <item>Syriac</item>
+        <item>Tagalog</item>
+        <item>Tamil</item>
+        <item>Telugu</item>
+        <item>Thai</item>
+        <item>Turkish</item>
+        <item>Ukrainian</item>
+        <item>Urdu</item>
+        <item>Vietnamese</item>
+    </string-array>
+
+    <string-array name="old_language_values">
+        <item>alb</item>
+        <item>ara</item>
+        <item>arm</item>
+        <item>baq</item>
+        <item>ben</item>
+        <item>bos</item>
+        <item>bre</item>
+        <item>bul</item>
+        <item>bur</item>
+        <item>cat</item>
+        <item>chi</item>
+        <item>hrv</item>
+        <item>cze</item>
+        <item>dan</item>
+        <item>dut</item>
+        <item>eng</item>
+        <item>epo</item>
+        <item>est</item>
+        <item>fin</item>
+        <item>fre</item>
+        <item>glg</item>
+        <item>geo</item>
+        <item>ger</item>
+        <item>ell</item>
+        <item>heb</item>
+        <item>hin</item>
+        <item>hun</item>
+        <item>ice</item>
+        <item>ind</item>
+        <item>ita</item>
+        <item>jpn</item>
+        <item>kaz</item>
+        <item>khm</item>
+        <item>kor</item>
+        <item>lav</item>
+        <item>lit</item>
+        <item>ltz</item>
+        <item>mac</item>
+        <item>may</item>
+        <item>mal</item>
+        <item>mon</item>
+        <item>nor</item>
+        <item>oci</item>
+        <item>per</item>
+        <item>pol</item>
+        <item>por</item>
+        <item>pob</item>
+        <item>rum</item>
+        <item>rus</item>
+        <item>scc</item>
+        <item>sin</item>
+        <item>slo</item>
+        <item>slv</item>
+        <item>spa</item>
+        <item>swa</item>
+        <item>swe</item>
+        <item>syr</item>
+        <item>tgl</item>
+        <item>tam</item>
+        <item>tel</item>
+        <item>tha</item>
+        <item>tur</item>
+        <item>ukr</item>
+        <item>urd</item>
+        <item>vie</item>
+    </string-array>
+
     <string-array name="subtitles_size_entries">
         <item>@string/subtitles_size_small</item>
         <item>@string/subtitles_size_normal</item>
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 6d2e53cffb..3c156f4212 100644
--- a/application/tools/src/main/java/org/videolan/tools/LocaleUtils.kt
+++ b/application/tools/src/main/java/org/videolan/tools/LocaleUtils.kt
@@ -8,7 +8,10 @@ import java.util.*
 import kotlin.collections.ArrayList
 
 object LocaleUtils {
-    fun getLocalesUsedInProject(projectLocales: Array<String>, defaultLocaleText: String): LocalePair {
+    fun getLocalesUsedInProject(
+        projectLocales: Array<String>,
+        defaultLocaleText: String
+    ): LocalePair {
 
         val localesEntry = arrayOfNulls<String>(projectLocales.size)
         for (i in projectLocales.indices) {
@@ -22,7 +25,8 @@ object LocaleUtils {
             if (displayCountry.isEmpty()) {
                 localesEntry[i] = displayLanguage.firstLetterUppercase()
             } else {
-                localesEntry[i] = "${displayLanguage.firstLetterUppercase()} - ${displayCountry.firstLetterUppercase()}"
+                localesEntry[i] =
+                    "${displayLanguage.firstLetterUppercase()} - ${displayCountry.firstLetterUppercase()}"
             }
         }
 
@@ -32,7 +36,8 @@ object LocaleUtils {
             localeTreeMap[localesEntry[i]!!] = projectLocales[i]
         }
 
-        val finalLocaleEntries = ArrayList<String>(localeTreeMap.size + 1).apply { add(0, defaultLocaleText) }
+        val finalLocaleEntries =
+            ArrayList<String>(localeTreeMap.size + 1).apply { add(0, defaultLocaleText) }
         val finalLocaleEntryValues = ArrayList<String>(localeTreeMap.size + 1).apply { add(0, "") }
 
         var i = 1
@@ -61,7 +66,8 @@ object LocaleUtils {
         for (separator in separators) {
             //see if there is a language and a country
             if (string.contains(separator)) {
-                val splittedLocale = string.split(separator.toRegex()).dropLastWhile { it.isEmpty() }.toTypedArray()
+                val splittedLocale =
+                    string.split(separator.toRegex()).dropLastWhile { it.isEmpty() }.toTypedArray()
                 if (splittedLocale.size == 2) {
                     return Locale(splittedLocale[0], splittedLocale[1])
                 }
diff --git a/application/vlc-android/src/org/videolan/vlc/viewmodels/SubtitlesModel.kt b/application/vlc-android/src/org/videolan/vlc/viewmodels/SubtitlesModel.kt
index 0d09717129..812b6bf6f0 100644
--- a/application/vlc-android/src/org/videolan/vlc/viewmodels/SubtitlesModel.kt
+++ b/application/vlc-android/src/org/videolan/vlc/viewmodels/SubtitlesModel.kt
@@ -21,6 +21,7 @@ import org.videolan.resources.opensubtitles.OpenSubtitleRepository
 import org.videolan.resources.util.NoConnectivityException
 import org.videolan.tools.CoroutineContextProvider
 import org.videolan.tools.FileUtils
+import org.videolan.tools.LocaleUtils
 import org.videolan.tools.Settings
 import org.videolan.tools.putSingle
 import org.videolan.vlc.BuildConfig
@@ -47,6 +48,22 @@ class SubtitlesModel(private val context: Context, private val mediaUri: Uri, pr
     val observableMessage = ObservableField<String>()
     val observableError = ObservableField<Boolean>()
     val observableResultDescription = ObservableField<Spanned>()
+    val oldLanguagesMigration by lazy {
+        val newLangCodes =  context.resources.getStringArray(R.array.language_values)
+        val oldLangCodes =  context.resources.getStringArray(R.array.old_language_values)
+        val newLangEntries =  context.resources.getStringArray(R.array.language_entries)
+        val oldLangEntries =  context.resources.getStringArray(R.array.old_language_entries)
+        val mapping = HashMap<String, String>()
+        for (i in oldLangCodes.indices) {
+            for (j in newLangCodes.indices) {
+                if (newLangEntries[j] == oldLangEntries[i]) {
+                    mapping[oldLangCodes[i]] = newLangCodes[j]
+                    break
+                }
+            }
+        }
+        mapping
+    }
 
     private val apiResultLiveData: MutableLiveData<List<Data>> = MutableLiveData()
     private val downloadedLiveData = ExternalSubRepository.getInstance(context).getDownloadedSubtitles(mediaUri).map { list ->
@@ -197,11 +214,15 @@ class SubtitlesModel(private val context: Context, private val mediaUri: Uri, pr
 
     fun getLastUsedLanguage(): List<String> {
         val language = try {
-            Locale.getDefault().isO3Language
+            Locale.getDefault().language
         } catch (e: MissingResourceException) {
-            "eng"
+            "en"
         }
-        return Settings.getInstance(context).getStringSet(LAST_USED_LANGUAGES, setOf(language))?.map { it.getCompliantLanguageID() } ?: emptyList()
+        return Settings.getInstance(context).getStringSet(LAST_USED_LANGUAGES, setOf(language))?.map { if (it.length > 2) migrateFromOld(it) ?: it else it } ?: emptyList()
+    }
+
+    private fun migrateFromOld(it: String?): String? {
+        return oldLanguagesMigration[it]
     }
 
     fun saveLastUsedLanguage(lastUsedLanguages: List<String>) = Settings.getInstance(context).putSingle(LAST_USED_LANGUAGES, lastUsedLanguages)



More information about the Android mailing list