From 6d812b86aaf755c3c459b99354822091f45303ff Mon Sep 17 00:00:00 2001 From: Isira Seneviratne Date: Mon, 22 Aug 2022 08:20:16 +0530 Subject: [PATCH 1/2] Use Locale.forLanguageTag(). --- .../org/schabi/newpipe/util/Localization.java | 60 ++++++------------- 1 file changed, 17 insertions(+), 43 deletions(-) diff --git a/app/src/main/java/org/schabi/newpipe/util/Localization.java b/app/src/main/java/org/schabi/newpipe/util/Localization.java index e20955a76..68497e998 100644 --- a/app/src/main/java/org/schabi/newpipe/util/Localization.java +++ b/app/src/main/java/org/schabi/newpipe/util/Localization.java @@ -76,16 +76,8 @@ public final class Localization { public static org.schabi.newpipe.extractor.localization.Localization getPreferredLocalization( final Context context) { - final String contentLanguage = PreferenceManager - .getDefaultSharedPreferences(context) - .getString(context.getString(R.string.content_language_key), - context.getString(R.string.default_localization_key)); - if (contentLanguage.equals(context.getString(R.string.default_localization_key))) { - return org.schabi.newpipe.extractor.localization.Localization - .fromLocale(Locale.getDefault()); - } return org.schabi.newpipe.extractor.localization.Localization - .fromLocalizationCode(contentLanguage); + .fromLocale(getPreferredLocale(context)); } public static ContentCountry getPreferredContentCountry(final Context context) { @@ -98,23 +90,24 @@ public final class Localization { return new ContentCountry(contentCountry); } - public static Locale getPreferredLocale(final Context context) { + private static Locale getLocaleFromPrefs(final Context context, @StringRes final int prefKey) { final SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(context); + final String defaultKey = context.getString(R.string.default_localization_key); + final String languageCode = sp.getString(context.getString(prefKey), defaultKey); - final String languageCode = sp.getString(context.getString(R.string.content_language_key), - context.getString(R.string.default_localization_key)); - - try { - if (languageCode.length() == 2) { - return new Locale(languageCode); - } else if (languageCode.contains("_")) { - final String country = languageCode.substring(languageCode.indexOf("_")); - return new Locale(languageCode.substring(0, 2), country); - } - } catch (final Exception ignored) { + if (languageCode.equals(defaultKey)) { + return Locale.getDefault(); + } else { + return Locale.forLanguageTag(languageCode); } + } - return Locale.getDefault(); + public static Locale getPreferredLocale(final Context context) { + return getLocaleFromPrefs(context, R.string.content_language_key); + } + + public static Locale getAppLocale(final Context context) { + return getLocaleFromPrefs(context, R.string.app_language_key); } public static String localizeNumber(final Context context, final long number) { @@ -314,34 +307,15 @@ public final class Localization { return prettyTime.formatUnrounded(offsetDateTime); } - private static void changeAppLanguage(final Locale loc, final Resources res) { + private static void changeAppLanguage(final Resources res, final Locale loc) { final DisplayMetrics dm = res.getDisplayMetrics(); final Configuration conf = res.getConfiguration(); conf.setLocale(loc); res.updateConfiguration(conf, dm); } - public static Locale getAppLocale(final Context context) { - final SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context); - String lang = prefs.getString(context.getString(R.string.app_language_key), "en"); - final Locale loc; - if (lang.equals(context.getString(R.string.default_localization_key))) { - loc = Locale.getDefault(); - } else if (lang.matches(".*-.*")) { - //to differentiate different versions of the language - //for example, pt (portuguese in Portugal) and pt-br (portuguese in Brazil) - final String[] localisation = lang.split("-"); - lang = localisation[0]; - final String country = localisation[1]; - loc = new Locale(lang, country); - } else { - loc = new Locale(lang); - } - return loc; - } - public static void assureCorrectAppLanguage(final Context c) { - changeAppLanguage(getAppLocale(c), c.getResources()); + changeAppLanguage(c.getResources(), getAppLocale(c)); } private static double round(final double value, final int places) { From 5c1af6d29608ee28146dd991ab70c06d1f38fcd8 Mon Sep 17 00:00:00 2001 From: Isira Seneviratne Date: Wed, 9 Nov 2022 08:53:53 +0530 Subject: [PATCH 2/2] Group private Localization methods together. --- .../org/schabi/newpipe/util/Localization.java | 70 +++++++++---------- 1 file changed, 33 insertions(+), 37 deletions(-) diff --git a/app/src/main/java/org/schabi/newpipe/util/Localization.java b/app/src/main/java/org/schabi/newpipe/util/Localization.java index 68497e998..916b902f0 100644 --- a/app/src/main/java/org/schabi/newpipe/util/Localization.java +++ b/app/src/main/java/org/schabi/newpipe/util/Localization.java @@ -56,7 +56,6 @@ import java.util.stream.Collectors; */ public final class Localization { - public static final String DOT_SEPARATOR = " • "; private static PrettyTime prettyTime; @@ -90,18 +89,6 @@ public final class Localization { return new ContentCountry(contentCountry); } - private static Locale getLocaleFromPrefs(final Context context, @StringRes final int prefKey) { - final SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(context); - final String defaultKey = context.getString(R.string.default_localization_key); - final String languageCode = sp.getString(context.getString(prefKey), defaultKey); - - if (languageCode.equals(defaultKey)) { - return Locale.getDefault(); - } else { - return Locale.forLanguageTag(languageCode); - } - } - public static Locale getPreferredLocale(final Context context) { return getLocaleFromPrefs(context, R.string.content_language_key); } @@ -176,13 +163,13 @@ public final class Localization { final double value = (double) count; if (count >= 1000000000) { - return localizeNumber(context, round(value / 1000000000, 1)) + return localizeNumber(context, round(value / 1000000000)) + context.getString(R.string.short_billion); } else if (count >= 1000000) { - return localizeNumber(context, round(value / 1000000, 1)) + return localizeNumber(context, round(value / 1000000)) + context.getString(R.string.short_million); } else if (count >= 1000) { - return localizeNumber(context, round(value / 1000, 1)) + return localizeNumber(context, round(value / 1000)) + context.getString(R.string.short_thousand); } else { return localizeNumber(context, value); @@ -219,21 +206,6 @@ public final class Localization { deletedCount, shortCount(context, deletedCount)); } - private static String getQuantity(final Context context, @PluralsRes final int pluralId, - @StringRes final int zeroCaseStringId, final long count, - final String formattedCount) { - if (count == 0) { - return context.getString(zeroCaseStringId); - } - - // As we use the already formatted count - // is not the responsibility of this method handle long numbers - // (it probably will fall in the "other" category, - // or some language have some specific rule... then we have to change it) - final int safeCount = (int) MathUtils.clamp(count, Integer.MIN_VALUE, Integer.MAX_VALUE); - return context.getResources().getQuantityString(pluralId, safeCount, formattedCount); - } - public static String getDurationString(final long duration) { final String output; @@ -307,18 +279,42 @@ public final class Localization { return prettyTime.formatUnrounded(offsetDateTime); } - private static void changeAppLanguage(final Resources res, final Locale loc) { + public static void assureCorrectAppLanguage(final Context c) { + final Resources res = c.getResources(); final DisplayMetrics dm = res.getDisplayMetrics(); final Configuration conf = res.getConfiguration(); - conf.setLocale(loc); + conf.setLocale(getAppLocale(c)); res.updateConfiguration(conf, dm); } - public static void assureCorrectAppLanguage(final Context c) { - changeAppLanguage(c.getResources(), getAppLocale(c)); + private static Locale getLocaleFromPrefs(final Context context, @StringRes final int prefKey) { + final SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(context); + final String defaultKey = context.getString(R.string.default_localization_key); + final String languageCode = sp.getString(context.getString(prefKey), defaultKey); + + if (languageCode.equals(defaultKey)) { + return Locale.getDefault(); + } else { + return Locale.forLanguageTag(languageCode); + } } - private static double round(final double value, final int places) { - return new BigDecimal(value).setScale(places, RoundingMode.HALF_UP).doubleValue(); + private static double round(final double value) { + return new BigDecimal(value).setScale(1, RoundingMode.HALF_UP).doubleValue(); + } + + private static String getQuantity(final Context context, @PluralsRes final int pluralId, + @StringRes final int zeroCaseStringId, final long count, + final String formattedCount) { + if (count == 0) { + return context.getString(zeroCaseStringId); + } + + // As we use the already formatted count + // is not the responsibility of this method handle long numbers + // (it probably will fall in the "other" category, + // or some language have some specific rule... then we have to change it) + final int safeCount = (int) MathUtils.clamp(count, Integer.MIN_VALUE, Integer.MAX_VALUE); + return context.getResources().getQuantityString(pluralId, safeCount, formattedCount); } }