X-Git-Url: http://gitweb.fperrin.net/?a=blobdiff_plain;f=src%2Fcom%2Fhughes%2Fandroid%2Fdictionary%2Fengine%2FLanguage.java;h=4bef67c6a9462ef9cc836fabf6f22d02000e91b8;hb=83d9dc7cd871082a82c2dd0dbb7a0ceabd7c83a0;hp=b4d8558358a86a91ff4513fa2381c5fe6d13727f;hpb=b2be3ae02bbbf99129418738083cf92b1751c3b9;p=Dictionary.git diff --git a/src/com/hughes/android/dictionary/engine/Language.java b/src/com/hughes/android/dictionary/engine/Language.java old mode 100755 new mode 100644 index b4d8558..4bef67c --- a/src/com/hughes/android/dictionary/engine/Language.java +++ b/src/com/hughes/android/dictionary/engine/Language.java @@ -1,129 +1,117 @@ -package com.hughes.android.dictionary.engine; - -import java.util.Comparator; -import java.util.LinkedHashMap; -import java.util.Locale; -import java.util.Map; - -import com.ibm.icu.text.Collator; - -public class Language { - - static final Map symbolToLangauge = new LinkedHashMap(); - - final String symbol; - final Locale locale; - - Collator sortCollator; - final Comparator sortComparator; - - private Collator findCollator; - final Comparator findComparator; - - public Language(final Locale locale) { - this.symbol = locale.getLanguage(); - this.locale = locale; - - this.sortComparator = new Comparator() { - public int compare(final String s1, final String s2) { - return getSortCollator().compare(textNorm(s1, false), textNorm(s2, false)); - } - }; - - this.findComparator = new Comparator() { - public int compare(final String s1, final String s2) { - return getFindCollator().compare(textNorm(s1, false), textNorm(s2, false)); - } - }; - - symbolToLangauge.put(symbol.toLowerCase(), this); - } - - public String textNorm(final String s, final boolean toLower) { - return toLower ? s.toLowerCase() : s; - } - - @Override - public String toString() { - return locale.toString(); - } - - public String getSymbol() { - return symbol; - } - - public synchronized Collator getFindCollator() { - if (findCollator == null) { - findCollator = Collator.getInstance(locale); - findCollator.setDecomposition(Collator.CANONICAL_DECOMPOSITION); - findCollator.setStrength(Collator.SECONDARY); - } - return findCollator; - } - - public synchronized Collator getSortCollator() { - if (sortCollator == null) { - sortCollator = Collator.getInstance(locale); - sortCollator.setDecomposition(Collator.CANONICAL_DECOMPOSITION); - sortCollator.setStrength(Collator.IDENTICAL); - } - return sortCollator; - } - - // ---------------------------------------------------------------- - - public static final Language en = new Language(Locale.ENGLISH); - public static final Language fr = new Language(Locale.FRENCH); - public static final Language it = new Language(Locale.ITALIAN); - - public static final Language de = new Language(Locale.GERMAN) { - @Override - public String textNorm(String token, final boolean toLower) { - if (toLower) { - token = token.toLowerCase(); - } - boolean sub = false; - // This is meant to be fast: occurrences of ae, oe, ue are probably rare. - for (int ePos = token.indexOf('e', 1); ePos != -1; ePos = token.indexOf( - 'e', ePos + 1)) { - final char pre = Character.toLowerCase(token.charAt(ePos - 1)); - if (pre == 'a' || pre == 'o' || pre == 'u') { - sub = true; - break; - } - } - if (!sub) { - return token; - } - - token = token.replaceAll("ae", "ä"); - token = token.replaceAll("oe", "ö"); - token = token.replaceAll("ue", "ü"); - - token = token.replaceAll("Ae", "Ä"); - token = token.replaceAll("Oe", "Ö"); - token = token.replaceAll("Ue", "Ü"); - - token = token.replaceAll("AE", "Ä"); - token = token.replaceAll("OE", "Ö"); - token = token.replaceAll("UE", "Ü"); - - return token; - } - }; - - static { - for (final String lang : Locale.getISOLanguages()) { - if (lookup(lang) == null) { - new Language(new Locale(lang)); - } - } - } - - // ---------------------------------------------------------------- - - public static Language lookup(final String symbol) { - return symbolToLangauge.get(symbol.toLowerCase()); - } - -} +// Copyright 2011 Google Inc. All Rights Reserved. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package com.hughes.android.dictionary.engine; + +import com.hughes.android.dictionary.CollatorWrapper; +import com.hughes.android.dictionary.DictionaryApplication; + +import java.util.Comparator; +import java.util.HashMap; +import java.util.Locale; +import java.util.Map; + +public class Language { + + public static final class LanguageResources { + final String englishName; + public final int nameId; + public final int flagId; + + public LanguageResources(final String englishName, int nameId, int flagId) { + this.englishName = englishName; + this.nameId = nameId; + this.flagId = flagId; + } + + public LanguageResources(final String englishName, int nameId) { + this(englishName, nameId, 0); + } + } + + private static final Map registry = new HashMap<>(); + + private final String isoCode; + private final Locale locale; + + private Language(final Locale locale, final String isoCode) { + this.locale = locale; + this.isoCode = isoCode; + + registry.put(isoCode.toLowerCase(), this); + } + + @Override + public String toString() { + return locale.toString(); + } + + public String getIsoCode() { + return isoCode; + } + + public synchronized Comparator getCollator() { + if (!DictionaryApplication.USE_COLLATOR) + return new Comparator() { + @Override + public int compare(Object o, Object t1) { + return String.class.cast(o).compareToIgnoreCase(String.class.cast(t1)); + } + }; + // TODO: consider if this should be cached - but must be thread-safe + return CollatorWrapper.getInstanceStrengthIdentical(locale); + } + + public String getDefaultNormalizerRules() { + return ":: Any-Latin; ' ' > ; :: Lower; :: NFD; :: [:Nonspacing Mark:] Remove; :: NFC ;"; + } + + /** + * A practical pattern to identify strong RTL characters. This pattern is + * not completely correct according to the Unicode standard. It is + * simplified for performance and small code size. + */ + private static final String rtlChars = + "\u0591-\u07FF\uFB1D-\uFDFD\uFE70-\uFEFC"; + + @SuppressWarnings("unused") + public static String fixBidiText(String text) { + // TODO: RTL text (e.g. arabic) in parenthesis might need extra + // \u200e markers sometimes - check what exactly is going on there. + return text; + } + + // ---------------------------------------------------------------- + + public static final Language en = new Language(Locale.ENGLISH, "EN"); + public static final Language it = new Language(Locale.ITALIAN, "IT"); + + public static final Language de = new Language(Locale.GERMAN, "DE") { + @Override + public String getDefaultNormalizerRules() { + return ":: Lower; 'ae' > 'ä'; 'oe' > 'ö'; 'ue' > 'ü'; 'ß' > 'ss'; "; + } + }; + + // ---------------------------------------------------------------- + + public static synchronized Language lookup(final String isoCode) { + Language lang = registry.get(isoCode.toLowerCase()); + if (lang == null) { + lang = new Language(new Locale(isoCode), isoCode); + } + return lang; + } + +}