Group private Localization methods together.

This commit is contained in:
Isira Seneviratne 2022-11-09 08:53:53 +05:30
parent 6d812b86aa
commit 5c1af6d296

View file

@ -56,7 +56,6 @@ import java.util.stream.Collectors;
*/ */
public final class Localization { public final class Localization {
public static final String DOT_SEPARATOR = ""; public static final String DOT_SEPARATOR = "";
private static PrettyTime prettyTime; private static PrettyTime prettyTime;
@ -90,18 +89,6 @@ public final class Localization {
return new ContentCountry(contentCountry); 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) { public static Locale getPreferredLocale(final Context context) {
return getLocaleFromPrefs(context, R.string.content_language_key); return getLocaleFromPrefs(context, R.string.content_language_key);
} }
@ -176,13 +163,13 @@ public final class Localization {
final double value = (double) count; final double value = (double) count;
if (count >= 1000000000) { if (count >= 1000000000) {
return localizeNumber(context, round(value / 1000000000, 1)) return localizeNumber(context, round(value / 1000000000))
+ context.getString(R.string.short_billion); + context.getString(R.string.short_billion);
} else if (count >= 1000000) { } else if (count >= 1000000) {
return localizeNumber(context, round(value / 1000000, 1)) return localizeNumber(context, round(value / 1000000))
+ context.getString(R.string.short_million); + context.getString(R.string.short_million);
} else if (count >= 1000) { } else if (count >= 1000) {
return localizeNumber(context, round(value / 1000, 1)) return localizeNumber(context, round(value / 1000))
+ context.getString(R.string.short_thousand); + context.getString(R.string.short_thousand);
} else { } else {
return localizeNumber(context, value); return localizeNumber(context, value);
@ -219,21 +206,6 @@ public final class Localization {
deletedCount, shortCount(context, deletedCount)); 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) { public static String getDurationString(final long duration) {
final String output; final String output;
@ -307,18 +279,42 @@ public final class Localization {
return prettyTime.formatUnrounded(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 DisplayMetrics dm = res.getDisplayMetrics();
final Configuration conf = res.getConfiguration(); final Configuration conf = res.getConfiguration();
conf.setLocale(loc); conf.setLocale(getAppLocale(c));
res.updateConfiguration(conf, dm); res.updateConfiguration(conf, dm);
} }
public static void assureCorrectAppLanguage(final Context c) { private static Locale getLocaleFromPrefs(final Context context, @StringRes final int prefKey) {
changeAppLanguage(c.getResources(), getAppLocale(c)); 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) { private static double round(final double value) {
return new BigDecimal(value).setScale(places, RoundingMode.HALF_UP).doubleValue(); 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);
} }
} }