diff options
author | 2014-04-08 17:01:13 +0900 | |
---|---|---|
committer | 2014-04-08 19:12:31 +0900 | |
commit | 6bb3556ff7b24a5a38d7cc4276017bda3a9a4bba (patch) | |
tree | b18d291d6e3074da85aacc2dfded58c755f53645 /tools/make-keyboard-text | |
parent | 465a2ac534bf5c0dea38608dbe713ed3a9dc84eb (diff) | |
download | latinime-6bb3556ff7b24a5a38d7cc4276017bda3a9a4bba.tar.gz latinime-6bb3556ff7b24a5a38d7cc4276017bda3a9a4bba.tar.xz latinime-6bb3556ff7b24a5a38d7cc4276017bda3a9a4bba.zip |
Use locale to look up KeyboardTextsTable
Change-Id: I1e05c7fdbb757efef0e9407dc25482ef1115e49a
Diffstat (limited to 'tools/make-keyboard-text')
5 files changed, 76 insertions, 70 deletions
diff --git a/tools/make-keyboard-text/res/src/com/android/inputmethod/keyboard/internal/KeyboardTextsTable.tmpl b/tools/make-keyboard-text/res/src/com/android/inputmethod/keyboard/internal/KeyboardTextsTable.tmpl index b25bfb204..2b5494fa5 100644 --- a/tools/make-keyboard-text/res/src/com/android/inputmethod/keyboard/internal/KeyboardTextsTable.tmpl +++ b/tools/make-keyboard-text/res/src/com/android/inputmethod/keyboard/internal/KeyboardTextsTable.tmpl @@ -19,6 +19,7 @@ package com.android.inputmethod.keyboard.internal; import com.android.inputmethod.latin.utils.CollectionUtils; import java.util.HashMap; +import java.util.Locale; /** * !!!!! DO NOT EDIT THIS FILE !!!!! @@ -44,19 +45,19 @@ import java.util.HashMap; public final class KeyboardTextsTable { // Name to index map. private static final HashMap<String, Integer> sNameToIndexesMap = CollectionUtils.newHashMap(); - // Language to texts table map. - private static final HashMap<String, String[]> sLanguageToTextsTableMap = + // Locale to texts table map. + private static final HashMap<String, String[]> sLocaleToTextsTableMap = CollectionUtils.newHashMap(); // TODO: Remove this variable after debugging. - // Texts table to language maps. - private static final HashMap<String[], String> sTextsTableToLanguageMap = + // Texts table to locale maps. + private static final HashMap<String[], String> sTextsTableToLocaleMap = CollectionUtils.newHashMap(); public static String getText(final String name, final String[] textsTable) { final Integer indexObj = sNameToIndexesMap.get(name); if (indexObj == null) { - throw new RuntimeException("Unknown text name=" + name + " language=" - + sTextsTableToLanguageMap.get(textsTable)); + throw new RuntimeException("Unknown text name=" + name + " locale=" + + sTextsTableToLocaleMap.get(textsTable)); } final int index = indexObj; final String text = (index < textsTable.length) ? textsTable[index] : null; @@ -64,17 +65,24 @@ public final class KeyboardTextsTable { return text; } // Sanity check. - if (index >= 0 && index < LANGUAGE_DEFAULT.length) { - return LANGUAGE_DEFAULT[index]; + if (index >= 0 && index < TEXTS_DEFAULT.length) { + return TEXTS_DEFAULT[index]; } // Throw exception for debugging purpose. throw new RuntimeException("Illegal index=" + index + " for name=" + name - + " language=" + sTextsTableToLanguageMap.get(textsTable)); + + " locale=" + sTextsTableToLocaleMap.get(textsTable)); } - public static String[] getTextsTable(final String language) { - final String[] textsTable = sLanguageToTextsTableMap.get(language); - return textsTable != null ? textsTable : LANGUAGE_DEFAULT; + public static String[] getTextsTable(final Locale locale) { + final String localeKey = locale.toString(); + if (sLocaleToTextsTableMap.containsKey(localeKey)) { + return sLocaleToTextsTableMap.get(localeKey); + } + final String languageKey = locale.getLanguage(); + if (sLocaleToTextsTableMap.containsKey(languageKey)) { + return sLocaleToTextsTableMap.get(languageKey); + } + return TEXTS_DEFAULT; } private static final String[] NAMES = { @@ -85,16 +93,14 @@ public final class KeyboardTextsTable { private static final String EMPTY = ""; /* Default texts */ - private static final String[] LANGUAGE_DEFAULT = { + private static final String[] TEXTS_DEFAULT = { /* @DEFAULT_TEXTS@ */ }; /* @TEXTS@ */ - // TODO: Use the language + "_" + region representation for the locale string key. - // Currently we are dropping the region from the key. - private static final Object[] LANGUAGES_AND_TEXTS = { + private static final Object[] LOCALES_AND_TEXTS = { // "locale", TEXT_ARRAY, /* numberOfNonNullText/lengthOf_TEXT_ARRAY localeName */ - /* @LANGUAGES_AND_TEXTS@ */ + /* @LOCALES_AND_TEXTS@ */ }; static { @@ -102,11 +108,11 @@ public final class KeyboardTextsTable { sNameToIndexesMap.put(NAMES[index], index); } - for (int i = 0; i < LANGUAGES_AND_TEXTS.length; i += 2) { - final String language = (String)LANGUAGES_AND_TEXTS[i]; - final String[] textsTable = (String[])LANGUAGES_AND_TEXTS[i + 1]; - sLanguageToTextsTableMap.put(language, textsTable); - sTextsTableToLanguageMap.put(textsTable, language); + for (int i = 0; i < LOCALES_AND_TEXTS.length; i += 2) { + final String locale = (String)LOCALES_AND_TEXTS[i]; + final String[] textsTable = (String[])LOCALES_AND_TEXTS[i + 1]; + sLocaleToTextsTableMap.put(locale, textsTable); + sTextsTableToLocaleMap.put(textsTable, locale); } } } diff --git a/tools/make-keyboard-text/src/com/android/inputmethod/keyboard/tools/JarUtils.java b/tools/make-keyboard-text/src/com/android/inputmethod/keyboard/tools/JarUtils.java index 8e0f213f4..b892df236 100644 --- a/tools/make-keyboard-text/src/com/android/inputmethod/keyboard/tools/JarUtils.java +++ b/tools/make-keyboard-text/src/com/android/inputmethod/keyboard/tools/JarUtils.java @@ -85,24 +85,24 @@ public final class JarUtils { }); } - // The language is taken from string resource jar entry name (values-<language>/) - // or {@link LocaleUtils#DEFAULT_LANGUAGE_NAME} for the default string resource + // The locale is taken from string resource jar entry name (values-<locale>/) + // or {@link LocaleUtils#DEFAULT_LOCALE_KEY} for the default string resource // directory (values/). - public static String getLanguageFromEntryName(final String jarEntryName) { + public static String getLocaleFromEntryName(final String jarEntryName) { final String dirName = jarEntryName.substring(0, jarEntryName.lastIndexOf('/')); final int pos = dirName.lastIndexOf('/'); final String parentName = (pos >= 0) ? dirName.substring(pos + 1) : dirName; - final int languagePos = parentName.indexOf('-'); - if (languagePos < 0) { + final int localePos = parentName.indexOf('-'); + if (localePos < 0) { // Default resource name. - return LocaleUtils.DEFAULT_LANGUAGE_NAME; + return LocaleUtils.DEFAULT_LOCALE_KEY; } - final String language = parentName.substring(languagePos + 1); - final int countryPos = language.indexOf("-r"); - if (countryPos < 0) { - return language; + final String locale = parentName.substring(localePos + 1); + final int regionPos = locale.indexOf("-r"); + if (regionPos < 0) { + return locale; } - return language.replace("-r", "_"); + return locale.replace("-r", "_"); } public static void close(final Closeable stream) { diff --git a/tools/make-keyboard-text/src/com/android/inputmethod/keyboard/tools/LocaleUtils.java b/tools/make-keyboard-text/src/com/android/inputmethod/keyboard/tools/LocaleUtils.java index 3a0ba2bbd..d0f8b4292 100644 --- a/tools/make-keyboard-text/src/com/android/inputmethod/keyboard/tools/LocaleUtils.java +++ b/tools/make-keyboard-text/src/com/android/inputmethod/keyboard/tools/LocaleUtils.java @@ -26,9 +26,9 @@ import java.util.Locale; * for the make-keyboard-text tool. */ public final class LocaleUtils { - public static final String DEFAULT_LANGUAGE_NAME = "DEFAULT"; - public static final String NO_LANGUAGE_CODE = "zz"; - public static final String NO_LANGUAGE_DISPLAY_NAME = "Alphabet"; + public static final String DEFAULT_LOCALE_KEY = "DEFAULT"; + public static final String NO_LANGUAGE_LOCALE_CODE = "zz"; + public static final String NO_LANGUAGE_LOCALE_DISPLAY_NAME = "Alphabet"; private LocaleUtils() { // Intentional empty constructor for utility class. @@ -48,7 +48,8 @@ public final class LocaleUtils { if (retval != null) { return retval; } - String[] localeParams = localeStr.split("_", 3); + final String[] localeParams = localeStr.split("_", 3); + // TODO: Use JDK 7 Locale.Builder to handle a script name. if (localeParams.length == 1) { retval = new Locale(localeParams[0]); } else if (localeParams.length == 2) { @@ -63,11 +64,11 @@ public final class LocaleUtils { } } - public static String getLanguageDisplayName(final String language) { - if (language.equals(NO_LANGUAGE_CODE)) { - return NO_LANGUAGE_DISPLAY_NAME; + public static String getLocaleDisplayName(final String localeString) { + if (localeString.equals(NO_LANGUAGE_LOCALE_CODE)) { + return NO_LANGUAGE_LOCALE_DISPLAY_NAME; } - final Locale locale = constructLocaleFromString(language); + final Locale locale = constructLocaleFromString(localeString); return locale.getDisplayName(Locale.ENGLISH); } } diff --git a/tools/make-keyboard-text/src/com/android/inputmethod/keyboard/tools/MoreKeysResources.java b/tools/make-keyboard-text/src/com/android/inputmethod/keyboard/tools/MoreKeysResources.java index f72fefbf9..c1a9753cc 100644 --- a/tools/make-keyboard-text/src/com/android/inputmethod/keyboard/tools/MoreKeysResources.java +++ b/tools/make-keyboard-text/src/com/android/inputmethod/keyboard/tools/MoreKeysResources.java @@ -25,7 +25,6 @@ import java.util.ArrayList; import java.util.Collections; import java.util.Comparator; import java.util.HashMap; -import java.util.Locale; import java.util.TreeMap; import java.util.jar.JarFile; @@ -36,12 +35,13 @@ public class MoreKeysResources { private static final String MARK_NAMES = "@NAMES@"; private static final String MARK_DEFAULT_TEXTS = "@DEFAULT_TEXTS@"; private static final String MARK_TEXTS = "@TEXTS@"; - private static final String MARK_LANGUAGES_AND_TEXTS = "@LANGUAGES_AND_TEXTS@"; + private static final String TEXTS_ARRAY_NAME_PREFIX = "TEXTS_"; + private static final String MARK_LOCALES_AND_TEXTS = "@LOCALES_AND_TEXTS@"; private static final String EMPTY_STRING_VAR = "EMPTY"; private final JarFile mJar; // String resources maps sorted by its language. The language is determined from the jar entry - // name by calling {@link JarUtils#getLanguegFromEntryName(String)}. + // name by calling {@link JarUtils#getLocaleFromEntryName(String)}. private final TreeMap<String, StringResourceMap> mResourcesMap = new TreeMap<String, StringResourceMap>(); // Default string resources map. @@ -60,9 +60,9 @@ public class MoreKeysResources { jar, TEXT_RESOURCE_NAME); for (final String entryName : resourceEntryNames) { final StringResourceMap resMap = new StringResourceMap(entryName); - mResourcesMap.put(resMap.mLanguage, resMap); + mResourcesMap.put(resMap.mLocale, resMap); } - mDefaultResourceMap = mResourcesMap.get(LocaleUtils.DEFAULT_LANGUAGE_NAME); + mDefaultResourceMap = mResourcesMap.get(LocaleUtils.DEFAULT_LOCALE_KEY); // Initialize name histogram and names list. final HashMap<String, Integer> nameHistogram = mNameHistogram; @@ -72,12 +72,12 @@ public class MoreKeysResources { resourceNamesList.add(res.mName); } // Make name histogram. - for (final String language : mResourcesMap.keySet()) { - final StringResourceMap resMap = mResourcesMap.get(language); + for (final String locale : mResourcesMap.keySet()) { + final StringResourceMap resMap = mResourcesMap.get(locale); if (resMap == mDefaultResourceMap) continue; for (final StringResource res : resMap.getResources()) { if (!mDefaultResourceMap.contains(res.mName)) { - throw new RuntimeException(res.mName + " in " + language + throw new RuntimeException(res.mName + " in " + locale + " doesn't have default resource"); } final int histogramValue = nameHistogram.get(res.mName); @@ -143,8 +143,8 @@ public class MoreKeysResources { dumpDefaultTexts(out); } else if (line.contains(MARK_TEXTS)) { dumpTexts(out); - } else if (line.contains(MARK_LANGUAGES_AND_TEXTS)) { - dumpLanguageMap(out); + } else if (line.contains(MARK_LOCALES_AND_TEXTS)) { + dumpLocalesMap(out); } else { out.println(line); } @@ -165,17 +165,17 @@ public class MoreKeysResources { mDefaultResourceMap.setOutputArraySize(outputArraySize); } - private static String getArrayNameForLanguage(final String language) { - return "LANGUAGE_" + language; + private static String getArrayNameForLocale(final String locale) { + return TEXTS_ARRAY_NAME_PREFIX + locale; } private void dumpTexts(final PrintStream out) { for (final StringResourceMap resMap : mResourcesMap.values()) { - final String language = resMap.mLanguage; + final String locale = resMap.mLocale; if (resMap == mDefaultResourceMap) continue; - out.format(" /* Language %s: %s */\n", - language, LocaleUtils.getLanguageDisplayName(language)); - out.format(" private static final String[] " + getArrayNameForLanguage(language) + out.format(" /* Locale %s: %s */\n", + locale, LocaleUtils.getLocaleDisplayName(locale)); + out.format(" private static final String[] " + getArrayNameForLocale(locale) + " = {\n"); final int outputArraySize = dumpTextsInternal(out, resMap); resMap.setOutputArraySize(outputArraySize); @@ -183,17 +183,16 @@ public class MoreKeysResources { } } - private void dumpLanguageMap(final PrintStream out) { + private void dumpLocalesMap(final PrintStream out) { for (final StringResourceMap resMap : mResourcesMap.values()) { - final String language = resMap.mLanguage; - final Locale locale = LocaleUtils.constructLocaleFromString(language); - final String languageKeyToDump = locale.getCountry().isEmpty() - ? String.format("\"%s\"", language) - : String.format("\"%s\"", locale.getLanguage()); - out.format(" %s, %-15s /* %3d/%3d %s */\n", - languageKeyToDump, getArrayNameForLanguage(language) + ",", + final String locale = resMap.mLocale; + final String localeToDump = locale.equals(LocaleUtils.DEFAULT_LOCALE_KEY) + ? String.format("\"%s\"", locale) + : String.format("\"%s\"%s", locale, " ".substring(locale.length())); + out.format(" %s, %-12s /* %3d/%3d %s */\n", + localeToDump, getArrayNameForLocale(locale) + ",", resMap.getResources().size(), resMap.getOutputArraySize(), - LocaleUtils.getLanguageDisplayName(language)); + LocaleUtils.getLocaleDisplayName(locale)); } } diff --git a/tools/make-keyboard-text/src/com/android/inputmethod/keyboard/tools/StringResourceMap.java b/tools/make-keyboard-text/src/com/android/inputmethod/keyboard/tools/StringResourceMap.java index 8cdc17ded..d7e76ad77 100644 --- a/tools/make-keyboard-text/src/com/android/inputmethod/keyboard/tools/StringResourceMap.java +++ b/tools/make-keyboard-text/src/com/android/inputmethod/keyboard/tools/StringResourceMap.java @@ -34,8 +34,8 @@ import javax.xml.parsers.SAXParser; import javax.xml.parsers.SAXParserFactory; public class StringResourceMap { - // Lanugage name. - public final String mLanguage; + // Locale name. + public final String mLocale; // String resource list. private final List<StringResource> mResources; // Name to string resource map. @@ -44,11 +44,11 @@ public class StringResourceMap { // The length of String[] that is created from this {@link StringResourceMap}. The length is // calculated in {@link MoreKeysResources#dumpTexts(OutputStream)} and recorded by // {@link #setOutputArraySize(int)}. The recorded length is used as a part of comment by - // {@link MoreKeysResources#dumpLanguageMap(OutputStream)} via {@link #getOutputArraySize()}. + // {@link MoreKeysResources#dumpLocaleMap(OutputStream)} via {@link #getOutputArraySize()}. private int mOutputArraySize; public StringResourceMap(final String jarEntryName) { - mLanguage = JarUtils.getLanguageFromEntryName(jarEntryName); + mLocale = JarUtils.getLocaleFromEntryName(jarEntryName); final StringResourceHandler handler = new StringResourceHandler(); final SAXParserFactory factory = SAXParserFactory.newInstance(); factory.setNamespaceAware(true); |