1 // Copyright 2011 Google Inc. All Rights Reserved.
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
7 // http://www.apache.org/licenses/LICENSE-2.0
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
15 package com.hughes.android.dictionary.engine;
17 import com.hughes.android.dictionary.CollatorWrapper;
18 import com.hughes.android.dictionary.DictionaryApplication;
20 import java.util.Comparator;
21 import java.util.HashMap;
22 import java.util.Locale;
25 public class Language {
27 public static final class LanguageResources {
28 final String englishName;
29 public final int nameId;
30 public final int flagId;
32 public LanguageResources(final String englishName, int nameId, int flagId) {
33 this.englishName = englishName;
38 public LanguageResources(final String englishName, int nameId) {
39 this(englishName, nameId, 0);
43 private static final Map<String, Language> registry = new HashMap<>();
45 private final String isoCode;
46 private final Locale locale;
48 private Language(final Locale locale, final String isoCode) {
50 this.isoCode = isoCode;
52 registry.put(isoCode.toLowerCase(), this);
56 public String toString() {
57 return locale.toString();
60 public String getIsoCode() {
64 public synchronized Comparator<Object> getCollator() {
65 if (!DictionaryApplication.USE_COLLATOR)
66 return new Comparator<Object>() {
68 public int compare(Object o, Object t1) {
69 return ((String)o).compareToIgnoreCase((String)t1);
72 // TODO: consider if this should be cached - but must be thread-safe
73 return CollatorWrapper.getInstanceStrengthIdentical(locale);
76 public String getDefaultNormalizerRules() {
77 return ":: Any-Latin; ' ' > ; :: Lower; :: NFD; :: [:Nonspacing Mark:] Remove; :: NFC ;";
81 * A practical pattern to identify strong RTL characters. This pattern is
82 * not completely correct according to the Unicode standard. It is
83 * simplified for performance and small code size.
85 private static final String rtlChars =
86 "\u0591-\u07FF\uFB1D-\uFDFD\uFE70-\uFEFC";
88 @SuppressWarnings("unused")
89 public static String fixBidiText(String text) {
90 // TODO: RTL text (e.g. arabic) in parenthesis might need extra
91 // \u200e markers sometimes - check what exactly is going on there.
95 // ----------------------------------------------------------------
97 public static final Language en = new Language(Locale.ENGLISH, "EN");
98 public static final Language it = new Language(Locale.ITALIAN, "IT");
100 public static final Language de = new Language(Locale.GERMAN, "DE") {
102 public String getDefaultNormalizerRules() {
103 return ":: Lower; 'ae' > 'ä'; 'oe' > 'ö'; 'ue' > 'ü'; 'ß' > 'ss'; ";
107 // ----------------------------------------------------------------
109 public static synchronized Language lookup(final String isoCode) {
110 Language lang = registry.get(isoCode.toLowerCase());
112 lang = new Language(new Locale(isoCode), isoCode);