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.DictionaryApplication;
18 import java.text.Collator;
20 import java.util.Comparator;
21 import java.util.HashMap;
22 import java.util.Locale;
24 import java.util.regex.Pattern;
26 public class Language {
28 public static final class LanguageResources {
29 public final String englishName;
30 public final int nameId;
31 public final int flagId;
33 public LanguageResources(final String englishName, int nameId, int flagId) {
34 this.englishName = englishName;
39 public LanguageResources(final String englishName, int nameId) {
40 this(englishName, nameId, 0);
44 private static final Map<String, Language> registry = new HashMap<String, Language>();
49 private Collator collator;
51 private Language(final Locale locale, final String isoCode) {
53 this.isoCode = isoCode;
55 registry.put(isoCode.toLowerCase(), this);
59 public String toString() {
60 return locale.toString();
63 public String getIsoCode() {
67 public synchronized Comparator getCollator() {
68 if (!DictionaryApplication.USE_COLLATOR)
69 return String.CASE_INSENSITIVE_ORDER;
70 // Don't think this is thread-safe...
71 // if (collator == null) {
72 this.collator = Collator.getInstance(locale);
73 this.collator.setStrength(Collator.IDENTICAL);
78 public String getDefaultNormalizerRules() {
79 return ":: Any-Latin; ' ' > ; :: Lower; :: NFD; :: [:Nonspacing Mark:] Remove; :: NFC ;";
83 * A practical pattern to identify strong RTL characters. This pattern is
84 * not completely correct according to the Unicode standard. It is
85 * simplified for performance and small code size.
87 private static final String rtlChars =
88 "\u0591-\u07FF\uFB1D-\uFDFD\uFE70-\uFEFC";
90 private static final String puncChars =
91 "\\[\\]\\(\\)\\{\\}\\=";
93 private static final Pattern RTL_LEFT_BOUNDARY = Pattern.compile("([" + puncChars + "])(["
95 private static final Pattern RTL_RIGHT_BOUNDARY = Pattern.compile("([" + rtlChars + "])(["
98 public static String fixBidiText(String text) {
99 // text = RTL_LEFT_BOUNDARY.matcher(text).replaceAll("$1\u200e $2");
100 // text = RTL_RIGHT_BOUNDARY.matcher(text).replaceAll("$1 \u200e$2");
104 // ----------------------------------------------------------------
106 public static final Language en = new Language(Locale.ENGLISH, "EN");
107 public static final Language fr = new Language(Locale.FRENCH, "FR");
108 public static final Language it = new Language(Locale.ITALIAN, "IT");
110 public static final Language de = new Language(Locale.GERMAN, "DE") {
112 public String getDefaultNormalizerRules() {
113 return ":: Lower; 'ae' > 'ä'; 'oe' > 'ö'; 'ue' > 'ü'; 'ß' > 'ss'; ";
117 // ----------------------------------------------------------------
119 public static synchronized Language lookup(final String isoCode) {
120 Language lang = registry.get(isoCode.toLowerCase());
122 lang = new Language(new Locale(isoCode), isoCode);