Skip to content

Commit 5c1af6d

Browse files
Group private Localization methods together.
1 parent 6d812b8 commit 5c1af6d

1 file changed

Lines changed: 33 additions & 37 deletions

File tree

app/src/main/java/org/schabi/newpipe/util/Localization.java

Lines changed: 33 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,6 @@
5656
*/
5757

5858
public final class Localization {
59-
6059
public static final String DOT_SEPARATOR = " • ";
6160
private static PrettyTime prettyTime;
6261

@@ -90,18 +89,6 @@ public static ContentCountry getPreferredContentCountry(final Context context) {
9089
return new ContentCountry(contentCountry);
9190
}
9291

93-
private static Locale getLocaleFromPrefs(final Context context, @StringRes final int prefKey) {
94-
final SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(context);
95-
final String defaultKey = context.getString(R.string.default_localization_key);
96-
final String languageCode = sp.getString(context.getString(prefKey), defaultKey);
97-
98-
if (languageCode.equals(defaultKey)) {
99-
return Locale.getDefault();
100-
} else {
101-
return Locale.forLanguageTag(languageCode);
102-
}
103-
}
104-
10592
public static Locale getPreferredLocale(final Context context) {
10693
return getLocaleFromPrefs(context, R.string.content_language_key);
10794
}
@@ -176,13 +163,13 @@ public static String shortCount(final Context context, final long count) {
176163

177164
final double value = (double) count;
178165
if (count >= 1000000000) {
179-
return localizeNumber(context, round(value / 1000000000, 1))
166+
return localizeNumber(context, round(value / 1000000000))
180167
+ context.getString(R.string.short_billion);
181168
} else if (count >= 1000000) {
182-
return localizeNumber(context, round(value / 1000000, 1))
169+
return localizeNumber(context, round(value / 1000000))
183170
+ context.getString(R.string.short_million);
184171
} else if (count >= 1000) {
185-
return localizeNumber(context, round(value / 1000, 1))
172+
return localizeNumber(context, round(value / 1000))
186173
+ context.getString(R.string.short_thousand);
187174
} else {
188175
return localizeNumber(context, value);
@@ -219,21 +206,6 @@ public static String deletedDownloadCount(final Context context, final int delet
219206
deletedCount, shortCount(context, deletedCount));
220207
}
221208

222-
private static String getQuantity(final Context context, @PluralsRes final int pluralId,
223-
@StringRes final int zeroCaseStringId, final long count,
224-
final String formattedCount) {
225-
if (count == 0) {
226-
return context.getString(zeroCaseStringId);
227-
}
228-
229-
// As we use the already formatted count
230-
// is not the responsibility of this method handle long numbers
231-
// (it probably will fall in the "other" category,
232-
// or some language have some specific rule... then we have to change it)
233-
final int safeCount = (int) MathUtils.clamp(count, Integer.MIN_VALUE, Integer.MAX_VALUE);
234-
return context.getResources().getQuantityString(pluralId, safeCount, formattedCount);
235-
}
236-
237209
public static String getDurationString(final long duration) {
238210
final String output;
239211

@@ -307,18 +279,42 @@ public static String relativeTime(final OffsetDateTime offsetDateTime) {
307279
return prettyTime.formatUnrounded(offsetDateTime);
308280
}
309281

310-
private static void changeAppLanguage(final Resources res, final Locale loc) {
282+
public static void assureCorrectAppLanguage(final Context c) {
283+
final Resources res = c.getResources();
311284
final DisplayMetrics dm = res.getDisplayMetrics();
312285
final Configuration conf = res.getConfiguration();
313-
conf.setLocale(loc);
286+
conf.setLocale(getAppLocale(c));
314287
res.updateConfiguration(conf, dm);
315288
}
316289

317-
public static void assureCorrectAppLanguage(final Context c) {
318-
changeAppLanguage(c.getResources(), getAppLocale(c));
290+
private static Locale getLocaleFromPrefs(final Context context, @StringRes final int prefKey) {
291+
final SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(context);
292+
final String defaultKey = context.getString(R.string.default_localization_key);
293+
final String languageCode = sp.getString(context.getString(prefKey), defaultKey);
294+
295+
if (languageCode.equals(defaultKey)) {
296+
return Locale.getDefault();
297+
} else {
298+
return Locale.forLanguageTag(languageCode);
299+
}
300+
}
301+
302+
private static double round(final double value) {
303+
return new BigDecimal(value).setScale(1, RoundingMode.HALF_UP).doubleValue();
319304
}
320305

321-
private static double round(final double value, final int places) {
322-
return new BigDecimal(value).setScale(places, RoundingMode.HALF_UP).doubleValue();
306+
private static String getQuantity(final Context context, @PluralsRes final int pluralId,
307+
@StringRes final int zeroCaseStringId, final long count,
308+
final String formattedCount) {
309+
if (count == 0) {
310+
return context.getString(zeroCaseStringId);
311+
}
312+
313+
// As we use the already formatted count
314+
// is not the responsibility of this method handle long numbers
315+
// (it probably will fall in the "other" category,
316+
// or some language have some specific rule... then we have to change it)
317+
final int safeCount = (int) MathUtils.clamp(count, Integer.MIN_VALUE, Integer.MAX_VALUE);
318+
return context.getResources().getQuantityString(pluralId, safeCount, formattedCount);
323319
}
324320
}

0 commit comments

Comments
 (0)