[Android] Improve supported language list

Nicolas Pomepuy git at videolan.org
Thu Apr 11 18:23:54 CEST 2019


vlc-android | branch: master | Nicolas Pomepuy <nicolas.pomepuy at gmail.com> | Thu Apr 11 09:42:10 2019 +0200| [70f5de757470bb55dd47daf501b8a0adf56de691] | committer: Geoffrey Métais

Improve supported language list

> https://code.videolan.org/videolan/vlc-android/commit/70f5de757470bb55dd47daf501b8a0adf56de691
---

 vlc-android/build.gradle                           | 21 +++++++
 .../src/org/videolan/vlc/gui/helpers/UiTools.java  | 72 +++++++++++-----------
 2 files changed, 57 insertions(+), 36 deletions(-)

diff --git a/vlc-android/build.gradle b/vlc-android/build.gradle
index e66765081..2f80d6643 100644
--- a/vlc-android/build.gradle
+++ b/vlc-android/build.gradle
@@ -199,6 +199,27 @@ task generateSources (type: Jar) {
     from android.sourceSets.main.java.srcDirs
 }
 
+task buildTranslationArray << {
+    def foundLocales = new StringBuilder()
+    foundLocales.append("new String[]{")
+
+    fileTree("res").visit { FileVisitDetails details ->
+        println 'details: '+details
+        if(details.file.path.endsWith("strings.xml")){
+            def languageCode = details.file.parent.tokenize('/').last().replaceAll('values-','').replaceAll('-r','-')
+            languageCode = (languageCode == "values") ? "en" : languageCode;
+            foundLocales.append("\"").append(languageCode).append("\"").append(",")
+        }
+    }
+
+    foundLocales.append("}")
+    //Don't forget to remove the trailing comma
+    def foundLocalesString = foundLocales.toString().replaceAll(',}','}')
+    android.defaultConfig.buildConfigField "String[]", "TRANSLATION_ARRAY", foundLocalesString
+
+}
+preBuild.dependsOn buildTranslationArray
+
 dependencies {
     devImplementation project(':libvlc')
     devImplementation project(':medialibrary')
diff --git a/vlc-android/src/org/videolan/vlc/gui/helpers/UiTools.java b/vlc-android/src/org/videolan/vlc/gui/helpers/UiTools.java
index a9de37840..6989a0150 100644
--- a/vlc-android/src/org/videolan/vlc/gui/helpers/UiTools.java
+++ b/vlc-android/src/org/videolan/vlc/gui/helpers/UiTools.java
@@ -32,6 +32,7 @@ import android.content.DialogInterface;
 import android.content.Intent;
 import android.content.SharedPreferences;
 import android.content.res.Configuration;
+import android.content.res.Resources;
 import android.content.res.TypedArray;
 import android.graphics.Bitmap;
 import android.graphics.drawable.BitmapDrawable;
@@ -48,6 +49,7 @@ import android.renderscript.RenderScript;
 import android.renderscript.ScriptIntrinsicBlur;
 import android.text.Html;
 import android.text.TextUtils;
+import android.util.DisplayMetrics;
 import android.view.Display;
 import android.view.DragAndDropPermissions;
 import android.view.DragEvent;
@@ -70,6 +72,7 @@ import org.videolan.libvlc.util.AndroidUtil;
 import org.videolan.medialibrary.Medialibrary;
 import org.videolan.medialibrary.media.MediaLibraryItem;
 import org.videolan.medialibrary.media.MediaWrapper;
+import org.videolan.vlc.BuildConfig;
 import org.videolan.vlc.MediaParsingService;
 import org.videolan.vlc.R;
 import org.videolan.vlc.VLCApplication;
@@ -82,9 +85,11 @@ import org.videolan.vlc.util.LocalePair;
 import org.videolan.vlc.util.Settings;
 import org.videolan.vlc.viewmodels.SortableModel;
 
+import java.util.HashSet;
 import java.util.List;
 import java.util.Locale;
 import java.util.Map;
+import java.util.Set;
 import java.util.TreeMap;
 
 import androidx.annotation.NonNull;
@@ -545,25 +550,7 @@ public class UiTools {
         // Are we using advanced debugging - locale?
         String p = VLCApplication.getLocale();
         if (!p.equals("")) {
-            Locale locale;
-            // workaround due to region code
-            if (p.equals("zh-TW")) {
-                locale = Locale.TRADITIONAL_CHINESE;
-            } else if (p.startsWith("zh")) {
-                locale = Locale.CHINA;
-            } else if (p.equals("pt-BR")) {
-                locale = new Locale("pt", "BR");
-            } else if (p.equals("bn-IN") || p.startsWith("bn")) {
-                locale = new Locale("bn", "IN");
-            } else {
-                /**
-                 * Avoid a crash of
-                 * java.lang.AssertionError: couldn't initialize LocaleData for locale
-                 * if the user enters nonsensical region codes.
-                 */
-                if (p.contains("-")) p = p.substring(0, p.indexOf('-'));
-                locale = new Locale(p);
-            }
+            Locale locale = getLocaleFromString(p);
             Locale.setDefault(locale);
             final Configuration config = new Configuration();
             config.locale = locale;
@@ -588,30 +575,15 @@ public class UiTools {
     }
 
     public static LocalePair getLocalesUsedInProject(Context context) {
-        final String[] localesEntryValues = context.getAssets().getLocales();
+        final String[] localesEntryValues = BuildConfig.TRANSLATION_ARRAY;
 
 
         final String[] localesEntry = new String[localesEntryValues.length];
         for (int i = 0; i < localesEntryValues.length; i++) {
 
             final String localesEntryValue = localesEntryValues[i];
-            String language;
-            String country = "";
-
-            //see if there is a language and a country
-            if (localesEntryValue.contains("-")) {
-                String[] splittedLocale = localesEntryValue.split("-");
-                if (splittedLocale.length == 2) {
-                    language = splittedLocale[0];
-                    country = splittedLocale[1];
-                } else {
-                    language = localesEntryValue;
-                }
-            } else {
-                language = localesEntryValue;
-            }
 
-            final Locale locale = new Locale(language, country);
+            Locale locale = getLocaleFromString(localesEntryValue);
 
             final String displayLanguage = locale.getDisplayLanguage(locale);
             final String displayCountry = locale.getDisplayCountry(locale);
@@ -646,6 +618,34 @@ public class UiTools {
         return new LocalePair(finalLocaleEntries, finalLocaleEntryValues);
     }
 
+
+    private static Locale getLocaleFromString(String string) {
+
+        /**
+         * See {@link android.content.res.AssetManager#getLocales()}
+         */
+        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
+            return Locale.forLanguageTag(string);
+        }
+
+        //Best effort on determining the locale
+
+        String[] separators = {"_", "-"};
+
+        for (String separator : separators) {
+            //see if there is a language and a country
+            if (string.contains(separator)) {
+                String[] splittedLocale = string.split(separator);
+                if (splittedLocale.length == 2) {
+                    return new Locale(splittedLocale[0], splittedLocale[1]);
+                }
+            }
+        }
+
+
+        return new Locale(string);
+    }
+
     private static String firstLetterUpper(String string) {
         if (string == null) {
             return null;



More information about the Android mailing list