aboutsummaryrefslogtreecommitdiffstats
path: root/java/src/com/android/inputmethod/latin/LocaleUtils.java
diff options
context:
space:
mode:
authorJean Chalard <jchalard@google.com>2011-08-26 20:22:47 +0900
committerJean Chalard <jchalard@google.com>2011-08-29 12:29:35 +0900
commitef35cb631c45c8b106fe7ed9e0d1178c3e5fb963 (patch)
tree8c19634f9ecd37282ec088e594491bc5962bc926 /java/src/com/android/inputmethod/latin/LocaleUtils.java
parent0ada663f3da8fc2d64f82a070b09b27735bc7fab (diff)
downloadlatinime-ef35cb631c45c8b106fe7ed9e0d1178c3e5fb963.tar.gz
latinime-ef35cb631c45c8b106fe7ed9e0d1178c3e5fb963.tar.xz
latinime-ef35cb631c45c8b106fe7ed9e0d1178c3e5fb963.zip
Move locale-related utility methods to LocaleUtils.
Change-Id: I7e9e6e5bc4486d8618d0213b112308c3d305c15e
Diffstat (limited to 'java/src/com/android/inputmethod/latin/LocaleUtils.java')
-rw-r--r--java/src/com/android/inputmethod/latin/LocaleUtils.java51
1 files changed, 51 insertions, 0 deletions
diff --git a/java/src/com/android/inputmethod/latin/LocaleUtils.java b/java/src/com/android/inputmethod/latin/LocaleUtils.java
index 054f1f9b8..efa9bfee3 100644
--- a/java/src/com/android/inputmethod/latin/LocaleUtils.java
+++ b/java/src/com/android/inputmethod/latin/LocaleUtils.java
@@ -16,8 +16,13 @@
package com.android.inputmethod.latin;
+import android.content.res.Configuration;
+import android.content.res.Resources;
import android.text.TextUtils;
+import java.util.HashMap;
+import java.util.Locale;
+
/**
* A class to help with handling Locales in string form.
*
@@ -30,6 +35,10 @@ public class LocaleUtils {
private final static String TAG = LocaleUtils.class.getSimpleName();
+ private LocaleUtils() {
+ // Intentional empty constructor for utility class.
+ }
+
// Locale match level constants.
// A higher level of match is guaranteed to have a higher numerical value.
// Some room is left within constants to add match cases that may arise necessary
@@ -154,4 +163,46 @@ public class LocaleUtils {
public static boolean isMatch(int level) {
return LOCALE_MATCH <= level;
}
+
+ /**
+ * Sets the system locale for this process.
+ *
+ * @param res the resources to use. Pass current resources.
+ * @param newLocale the locale to change to.
+ * @return the old locale.
+ */
+ public static Locale setSystemLocale(final Resources res, final Locale newLocale) {
+ final Configuration conf = res.getConfiguration();
+ final Locale saveLocale = conf.locale;
+ conf.locale = newLocale;
+ res.updateConfiguration(conf, res.getDisplayMetrics());
+ return saveLocale;
+ }
+
+ private static final HashMap<String, Locale> sLocaleCache = new HashMap<String, Locale>();
+
+ /**
+ * Creates a locale from a string specification.
+ */
+ public static Locale constructLocaleFromString(final String localeStr) {
+ if (localeStr == null)
+ return null;
+ synchronized (sLocaleCache) {
+ if (sLocaleCache.containsKey(localeStr))
+ return sLocaleCache.get(localeStr);
+ Locale retval = null;
+ String[] localeParams = localeStr.split("_", 3);
+ if (localeParams.length == 1) {
+ retval = new Locale(localeParams[0]);
+ } else if (localeParams.length == 2) {
+ retval = new Locale(localeParams[0], localeParams[1]);
+ } else if (localeParams.length == 3) {
+ retval = new Locale(localeParams[0], localeParams[1], localeParams[2]);
+ }
+ if (retval != null) {
+ sLocaleCache.put(localeStr, retval);
+ }
+ return retval;
+ }
+ }
}