Skip to content

Commit

Permalink
Group private Localization methods together.
Browse files Browse the repository at this point in the history
  • Loading branch information
Isira-Seneviratne committed Nov 9, 2022
1 parent 6d812b8 commit 5c1af6d
Showing 1 changed file with 33 additions and 37 deletions.
70 changes: 33 additions & 37 deletions app/src/main/java/org/schabi/newpipe/util/Localization.java
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,6 @@
*/

public final class Localization {

public static final String DOT_SEPARATOR = " • ";
private static PrettyTime prettyTime;

Expand Down Expand Up @@ -90,18 +89,6 @@ public static ContentCountry getPreferredContentCountry(final Context context) {
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);
}
Expand Down Expand Up @@ -176,13 +163,13 @@ public static String shortCount(final Context context, final long count) {

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);
Expand Down Expand Up @@ -219,21 +206,6 @@ public static String deletedDownloadCount(final Context context, final int delet
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;

Expand Down Expand Up @@ -307,18 +279,42 @@ public static String relativeTime(final OffsetDateTime offsetDateTime) {
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) {
return new BigDecimal(value).setScale(1, RoundingMode.HALF_UP).doubleValue();
}

private static double round(final double value, final int places) {
return new BigDecimal(value).setScale(places, 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);
}
}

0 comments on commit 5c1af6d

Please sign in to comment.