]> gitweb.fperrin.net Git - Dictionary.git/blob - src/com/hughes/android/dictionary/engine/Language.java
4091ded1fbb1db7978a2e64b7f27b0a2e2a470d2
[Dictionary.git] / src / com / hughes / android / dictionary / engine / Language.java
1 // Copyright 2011 Google Inc. All Rights Reserved.
2 //
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
6 //
7 //     http://www.apache.org/licenses/LICENSE-2.0
8 //
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.
14
15 package com.hughes.android.dictionary.engine;
16
17 import com.hughes.android.dictionary.CollatorWrapper;
18 import com.hughes.android.dictionary.DictionaryApplication;
19
20 import java.util.Comparator;
21 import java.util.HashMap;
22 import java.util.Locale;
23 import java.util.Map;
24 import java.util.regex.Pattern;
25
26 public class Language {
27
28     public static final class LanguageResources {
29         public final String englishName;
30         public final int nameId;
31         public final int flagId;
32
33         public LanguageResources(final String englishName, int nameId, int flagId) {
34             this.englishName = englishName;
35             this.nameId = nameId;
36             this.flagId = flagId;
37         }
38
39         public LanguageResources(final String englishName, int nameId) {
40             this(englishName, nameId, 0);
41         }
42     }
43
44     private static final Map<String, Language> registry = new HashMap<String, Language>();
45
46     final String isoCode;
47     final Locale locale;
48
49     private Comparator collator;
50
51     private Language(final Locale locale, final String isoCode) {
52         this.locale = locale;
53         this.isoCode = isoCode;
54
55         registry.put(isoCode.toLowerCase(), this);
56     }
57
58     @Override
59     public String toString() {
60         return locale.toString();
61     }
62
63     public String getIsoCode() {
64         return isoCode;
65     }
66
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 = CollatorWrapper.getInstanceStrengthIdentical(locale);
73         // }
74         return collator;
75     }
76
77     public String getDefaultNormalizerRules() {
78         return ":: Any-Latin; ' ' > ; :: Lower; :: NFD; :: [:Nonspacing Mark:] Remove; :: NFC ;";
79     }
80
81     /**
82      * A practical pattern to identify strong RTL characters. This pattern is
83      * not completely correct according to the Unicode standard. It is
84      * simplified for performance and small code size.
85      */
86     private static final String rtlChars =
87         "\u0591-\u07FF\uFB1D-\uFDFD\uFE70-\uFEFC";
88
89     private static final String puncChars =
90         "\\[\\]\\(\\)\\{\\}\\=";
91
92     private static final Pattern RTL_LEFT_BOUNDARY = Pattern.compile("([" + puncChars + "])(["
93             + rtlChars + "])");
94     private static final Pattern RTL_RIGHT_BOUNDARY = Pattern.compile("([" + rtlChars + "])(["
95             + puncChars + "])");
96
97     public static String fixBidiText(String text) {
98         // text = RTL_LEFT_BOUNDARY.matcher(text).replaceAll("$1\u200e $2");
99         // text = RTL_RIGHT_BOUNDARY.matcher(text).replaceAll("$1 \u200e$2");
100         return text;
101     }
102
103     // ----------------------------------------------------------------
104
105     public static final Language en = new Language(Locale.ENGLISH, "EN");
106     public static final Language fr = new Language(Locale.FRENCH, "FR");
107     public static final Language it = new Language(Locale.ITALIAN, "IT");
108
109     public static final Language de = new Language(Locale.GERMAN, "DE") {
110         @Override
111         public String getDefaultNormalizerRules() {
112             return ":: Lower; 'ae' > 'ä'; 'oe' > 'ö'; 'ue' > 'ü'; 'ß' > 'ss'; ";
113         }
114     };
115
116     // ----------------------------------------------------------------
117
118     public static synchronized Language lookup(final String isoCode) {
119         Language lang = registry.get(isoCode.toLowerCase());
120         if (lang == null) {
121             lang = new Language(new Locale(isoCode), isoCode);
122         }
123         return lang;
124     }
125
126 }