|
56 | 56 | */ |
57 | 57 |
|
58 | 58 | public final class Localization { |
59 | | - |
60 | 59 | public static final String DOT_SEPARATOR = " • "; |
61 | 60 | private static PrettyTime prettyTime; |
62 | 61 |
|
@@ -90,18 +89,6 @@ public static ContentCountry getPreferredContentCountry(final Context context) { |
90 | 89 | return new ContentCountry(contentCountry); |
91 | 90 | } |
92 | 91 |
|
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 | | - |
105 | 92 | public static Locale getPreferredLocale(final Context context) { |
106 | 93 | return getLocaleFromPrefs(context, R.string.content_language_key); |
107 | 94 | } |
@@ -176,13 +163,13 @@ public static String shortCount(final Context context, final long count) { |
176 | 163 |
|
177 | 164 | final double value = (double) count; |
178 | 165 | if (count >= 1000000000) { |
179 | | - return localizeNumber(context, round(value / 1000000000, 1)) |
| 166 | + return localizeNumber(context, round(value / 1000000000)) |
180 | 167 | + context.getString(R.string.short_billion); |
181 | 168 | } else if (count >= 1000000) { |
182 | | - return localizeNumber(context, round(value / 1000000, 1)) |
| 169 | + return localizeNumber(context, round(value / 1000000)) |
183 | 170 | + context.getString(R.string.short_million); |
184 | 171 | } else if (count >= 1000) { |
185 | | - return localizeNumber(context, round(value / 1000, 1)) |
| 172 | + return localizeNumber(context, round(value / 1000)) |
186 | 173 | + context.getString(R.string.short_thousand); |
187 | 174 | } else { |
188 | 175 | return localizeNumber(context, value); |
@@ -219,21 +206,6 @@ public static String deletedDownloadCount(final Context context, final int delet |
219 | 206 | deletedCount, shortCount(context, deletedCount)); |
220 | 207 | } |
221 | 208 |
|
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 | | - |
237 | 209 | public static String getDurationString(final long duration) { |
238 | 210 | final String output; |
239 | 211 |
|
@@ -307,18 +279,42 @@ public static String relativeTime(final OffsetDateTime offsetDateTime) { |
307 | 279 | return prettyTime.formatUnrounded(offsetDateTime); |
308 | 280 | } |
309 | 281 |
|
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(); |
311 | 284 | final DisplayMetrics dm = res.getDisplayMetrics(); |
312 | 285 | final Configuration conf = res.getConfiguration(); |
313 | | - conf.setLocale(loc); |
| 286 | + conf.setLocale(getAppLocale(c)); |
314 | 287 | res.updateConfiguration(conf, dm); |
315 | 288 | } |
316 | 289 |
|
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(); |
319 | 304 | } |
320 | 305 |
|
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); |
323 | 319 | } |
324 | 320 | } |
0 commit comments