]> gitweb.fperrin.net Git - Dictionary.git/blob - src/com/hughes/android/dictionary/engine/Language.java
Some lint fixes.
[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
25 public class Language {
26
27     public static final class LanguageResources {
28         final String englishName;
29         public final int nameId;
30         public final int flagId;
31
32         public LanguageResources(final String englishName, int nameId, int flagId) {
33             this.englishName = englishName;
34             this.nameId = nameId;
35             this.flagId = flagId;
36         }
37
38         public LanguageResources(final String englishName, int nameId) {
39             this(englishName, nameId, 0);
40         }
41     }
42
43     private static final Map<String, Language> registry = new HashMap<>();
44
45     private final String isoCode;
46     private final Locale locale;
47
48     private Language(final Locale locale, final String isoCode) {
49         this.locale = locale;
50         this.isoCode = isoCode;
51
52         registry.put(isoCode.toLowerCase(), this);
53     }
54
55     @Override
56     public String toString() {
57         return locale.toString();
58     }
59
60     public String getIsoCode() {
61         return isoCode;
62     }
63
64     public synchronized Comparator<Object> getCollator() {
65         if (!DictionaryApplication.USE_COLLATOR)
66             return new Comparator<Object>() {
67                 @Override
68                 public int compare(Object o, Object t1) {
69                     return ((String)o).compareToIgnoreCase((String)t1);
70                 }
71             };
72         // TODO: consider if this should be cached - but must be thread-safe
73         return CollatorWrapper.getInstanceStrengthIdentical(locale);
74     }
75
76     public String getDefaultNormalizerRules() {
77         return ":: Any-Latin; ' ' > ; :: Lower; :: NFD; :: [:Nonspacing Mark:] Remove; :: NFC ;";
78     }
79
80     /**
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.
84      */
85     private static final String rtlChars =
86         "\u0591-\u07FF\uFB1D-\uFDFD\uFE70-\uFEFC";
87
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.
92         return text;
93     }
94
95     // ----------------------------------------------------------------
96
97     public static final Language en = new Language(Locale.ENGLISH, "EN");
98     public static final Language it = new Language(Locale.ITALIAN, "IT");
99
100     public static final Language de = new Language(Locale.GERMAN, "DE") {
101         @Override
102         public String getDefaultNormalizerRules() {
103             return ":: Lower; 'ae' > 'ä'; 'oe' > 'ö'; 'ue' > 'ü'; 'ß' > 'ss'; ";
104         }
105     };
106
107     // ----------------------------------------------------------------
108
109     public static synchronized Language lookup(final String isoCode) {
110         Language lang = registry.get(isoCode.toLowerCase());
111         if (lang == null) {
112             lang = new Language(new Locale(isoCode), isoCode);
113         }
114         return lang;
115     }
116
117 }