|
| 1 | +package org.wordpress.android.util; |
| 2 | + |
| 3 | +import android.content.Context; |
| 4 | + |
| 5 | +import java.util.Locale; |
| 6 | + |
| 7 | +/** |
| 8 | + * Methods for dealing with i18n messages |
| 9 | + */ |
| 10 | +public class LanguageUtils { |
| 11 | + |
| 12 | + public static Locale getCurrentDeviceLanguage(Context context) { |
| 13 | + //better use getConfiguration as it has the latest locale configuration change. |
| 14 | + //Otherwise Locale.getDefault().getLanguage() gets |
| 15 | + //the config upon application launch. |
| 16 | + Locale deviceLocale = context != null ? context.getResources().getConfiguration().locale : Locale.getDefault(); |
| 17 | + return deviceLocale; |
| 18 | + } |
| 19 | + |
| 20 | + public static String getCurrentDeviceLanguageCode(Context context) { |
| 21 | + String deviceLanguageCode = getCurrentDeviceLanguage(context).toString(); |
| 22 | + return deviceLanguageCode; |
| 23 | + } |
| 24 | + |
| 25 | + public static String getPatchedCurrentDeviceLanguage(Context context) { |
| 26 | + return patchDeviceLanguageCode(getCurrentDeviceLanguageCode(context)); |
| 27 | + } |
| 28 | + |
| 29 | + /** |
| 30 | + * Patches a deviceLanguageCode if any of deprecated values iw, id, or yi |
| 31 | + */ |
| 32 | + public static String patchDeviceLanguageCode(String deviceLanguageCode){ |
| 33 | + String patchedCode = deviceLanguageCode; |
| 34 | + /* |
| 35 | + <p>Note that Java uses several deprecated two-letter codes. The Hebrew ("he") language |
| 36 | + * code is rewritten as "iw", Indonesian ("id") as "in", and Yiddish ("yi") as "ji". This |
| 37 | + * rewriting happens even if you construct your own {@code Locale} object, not just for |
| 38 | + * instances returned by the various lookup methods. |
| 39 | + */ |
| 40 | + if (deviceLanguageCode != null) { |
| 41 | + if (deviceLanguageCode.startsWith("iw")) |
| 42 | + patchedCode = deviceLanguageCode.replace("iw", "he"); |
| 43 | + else if (deviceLanguageCode.startsWith("in")) |
| 44 | + patchedCode = deviceLanguageCode.replace("in", "id"); |
| 45 | + else if (deviceLanguageCode.startsWith("ji")) |
| 46 | + patchedCode = deviceLanguageCode.replace("ji", "yi"); |
| 47 | + } |
| 48 | + |
| 49 | + return patchedCode; |
| 50 | + } |
| 51 | + |
| 52 | +} |
0 commit comments