X-Git-Url: http://gitweb.fperrin.net/?a=blobdiff_plain;f=src%2Fcom%2Fhughes%2Fandroid%2Fdictionary%2Fengine%2FLanguage.java;h=4bef67c6a9462ef9cc836fabf6f22d02000e91b8;hb=83d9dc7cd871082a82c2dd0dbb7a0ceabd7c83a0;hp=b19f450538a8c8f47c422afa5de5cae60741efe7;hpb=e383eb76e168500b9e3968a6751366c2ce8b441f;p=Dictionary.git diff --git a/src/com/hughes/android/dictionary/engine/Language.java b/src/com/hughes/android/dictionary/engine/Language.java index b19f450..4bef67c 100644 --- a/src/com/hughes/android/dictionary/engine/Language.java +++ b/src/com/hughes/android/dictionary/engine/Language.java @@ -1,147 +1,117 @@ -// 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 java.util.LinkedHashMap; -import java.util.Locale; -import java.util.Map; - -import com.ibm.icu.text.Collator; - -public class Language { - - public static final Map isoCodeToWikiName = new LinkedHashMap(); - static { - isoCodeToWikiName.put("AF", "Afrikaans"); - isoCodeToWikiName.put("SQ", "Albanian"); - isoCodeToWikiName.put("AR", "Arabic"); - isoCodeToWikiName.put("HY", "Armenian"); - isoCodeToWikiName.put("BE", "Belarusian"); - isoCodeToWikiName.put("BN", "Bengali"); - isoCodeToWikiName.put("BS", "Bosnian"); - isoCodeToWikiName.put("BG", "Bulgarian"); - isoCodeToWikiName.put("CA", "Catalan"); - isoCodeToWikiName.put("HR", "Croatian"); - isoCodeToWikiName.put("CS", "Czech"); - isoCodeToWikiName.put("ZH", "Chinese|Mandarin|Cantonese"); - isoCodeToWikiName.put("DA", "Danish"); - isoCodeToWikiName.put("NL", "Dutch"); - isoCodeToWikiName.put("EN", "English"); - isoCodeToWikiName.put("EO", "Esperanto"); - isoCodeToWikiName.put("ET", "Estonian"); - isoCodeToWikiName.put("FI", "Finnish"); - isoCodeToWikiName.put("FR", "French"); - isoCodeToWikiName.put("DE", "German"); - isoCodeToWikiName.put("EL", "Greek"); - isoCodeToWikiName.put("haw", "Hawaiian"); - isoCodeToWikiName.put("HE", "Hebrew"); - isoCodeToWikiName.put("HI", "Hindi"); - isoCodeToWikiName.put("HU", "Hungarian"); - isoCodeToWikiName.put("IS", "Icelandic"); - isoCodeToWikiName.put("ID", "Indonesian"); - isoCodeToWikiName.put("GA", "Irish"); - isoCodeToWikiName.put("IT", "Italian"); - isoCodeToWikiName.put("LA", "Latin"); - isoCodeToWikiName.put("LV", "Latvian"); - isoCodeToWikiName.put("LT", "Lithuanian"); - isoCodeToWikiName.put("JA", "Japanese"); - isoCodeToWikiName.put("KO", "Korean"); - isoCodeToWikiName.put("KU", "Kurdish"); - isoCodeToWikiName.put("MS", "Malay"); - isoCodeToWikiName.put("MI", "Maori"); - isoCodeToWikiName.put("MN", "Mongolian"); - isoCodeToWikiName.put("NE", "Nepali"); - isoCodeToWikiName.put("NO", "Norwegian"); - isoCodeToWikiName.put("FA", "Persian"); - isoCodeToWikiName.put("PL", "Polish"); - isoCodeToWikiName.put("PT", "Portuguese"); - isoCodeToWikiName.put("PA", "Punjabi"); - isoCodeToWikiName.put("RO", "Romanian"); - isoCodeToWikiName.put("RU", "Russian"); - isoCodeToWikiName.put("SA", "Sanskrit"); - isoCodeToWikiName.put("SR", "Serbian"); - isoCodeToWikiName.put("SO", "Somali"); - isoCodeToWikiName.put("ES", "Spanish"); - isoCodeToWikiName.put("SW", "Swahili"); - isoCodeToWikiName.put("SV", "Swedish"); - isoCodeToWikiName.put("TG", "Tajik"); - isoCodeToWikiName.put("TH", "Thai"); - isoCodeToWikiName.put("BO", "Tibetan"); - isoCodeToWikiName.put("TR", "Turkish"); - isoCodeToWikiName.put("UK", "Ukrainian"); - isoCodeToWikiName.put("VI", "Vietnamese"); - isoCodeToWikiName.put("CI", "Welsh"); - isoCodeToWikiName.put("YI", "Yiddish"); - isoCodeToWikiName.put("ZU", "Zulu"); - } - - static final Map symbolToLangauge = new LinkedHashMap(); - - final String symbol; - final Locale locale; - - private Collator collator; - - public Language(final Locale locale) { - this.symbol = locale.getLanguage(); - this.locale = locale; - - symbolToLangauge.put(symbol.toLowerCase(), this); - } - - @Override - public String toString() { - return locale.toString(); - } - - public String getSymbol() { - return symbol; - } - - public synchronized Collator getCollator() { - if (collator == null) { - this.collator = Collator.getInstance(locale); - this.collator.setStrength(Collator.IDENTICAL); - } - return collator; - } - - public String getDefaultNormalizerRules() { - return ":: Any-Latin; ' ' > ; :: Lower; :: NFD; :: [:Nonspacing Mark:] Remove; :: NFC ;"; - } - // ---------------------------------------------------------------- - - 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 getDefaultNormalizerRules() { - return ":: Lower; 'ae' > 'ä'; 'oe' > 'ö'; 'ue' > 'ü'; 'ß' > 'ss'; "; - } - }; - - // ---------------------------------------------------------------- - - public static synchronized Language lookup(final String symbol) { - Language lang = symbolToLangauge.get(symbol.toLowerCase()); - if (lang == null) { - lang = new Language(new Locale(symbol)); - } - return lang; - } - -} +// 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; + } + +}