OLD | NEW |
| (Empty) |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | |
2 // Use of this source code is governed by a BSD-style license that can be | |
3 // found in the LICENSE file. | |
4 | |
5 #include "chrome/common/spellcheck_common.h" | |
6 | |
7 #include "base/command_line.h" | |
8 #include "base/files/file_path.h" | |
9 #include "base/logging.h" | |
10 #include "base/macros.h" | |
11 #include "base/metrics/field_trial.h" | |
12 #include "base/strings/string_util.h" | |
13 #include "chrome/common/chrome_switches.h" | |
14 #include "third_party/icu/source/common/unicode/uloc.h" | |
15 #include "third_party/icu/source/common/unicode/urename.h" | |
16 #include "third_party/icu/source/common/unicode/utypes.h" | |
17 | |
18 namespace chrome { | |
19 namespace spellcheck_common { | |
20 | |
21 struct LanguageRegion { | |
22 const char* language; // The language. | |
23 const char* language_region; // language & region, used by dictionaries. | |
24 }; | |
25 | |
26 struct LanguageVersion { | |
27 const char* language; // The language input. | |
28 const char* version; // The corresponding version. | |
29 }; | |
30 | |
31 static const LanguageRegion g_supported_spellchecker_languages[] = { | |
32 // Several languages are not to be included in the spellchecker list: | |
33 // th-TH, vi-VI. | |
34 {"af", "af-ZA"}, | |
35 {"bg", "bg-BG"}, | |
36 {"ca", "ca-ES"}, | |
37 {"cs", "cs-CZ"}, | |
38 {"da", "da-DK"}, | |
39 {"de", "de-DE"}, | |
40 {"el", "el-GR"}, | |
41 {"en-AU", "en-GB"}, | |
42 {"en-CA", "en-CA"}, | |
43 {"en-GB", "en-GB"}, | |
44 {"en-US", "en-US"}, | |
45 {"es", "es-ES"}, | |
46 {"es-419", "es-ES"}, | |
47 {"es-AR", "es-ES"}, | |
48 {"es-ES", "es-ES"}, | |
49 {"es-MX", "es-ES"}, | |
50 {"es-US", "es-ES"}, | |
51 {"et", "et-EE"}, | |
52 {"fa", "fa-IR"}, | |
53 {"fo", "fo-FO"}, | |
54 {"fr", "fr-FR"}, | |
55 {"he", "he-IL"}, | |
56 {"hi", "hi-IN"}, | |
57 {"hr", "hr-HR"}, | |
58 {"hu", "hu-HU"}, | |
59 {"id", "id-ID"}, | |
60 {"it", "it-IT"}, | |
61 {"ko", "ko"}, | |
62 {"lt", "lt-LT"}, | |
63 {"lv", "lv-LV"}, | |
64 {"nb", "nb-NO"}, | |
65 {"nl", "nl-NL"}, | |
66 {"pl", "pl-PL"}, | |
67 {"pt-BR", "pt-BR"}, | |
68 {"pt-PT", "pt-PT"}, | |
69 {"ro", "ro-RO"}, | |
70 {"ru", "ru-RU"}, | |
71 {"sh", "sh"}, | |
72 {"sk", "sk-SK"}, | |
73 {"sl", "sl-SI"}, | |
74 {"sq", "sq"}, | |
75 {"sr", "sr"}, | |
76 {"sv", "sv-SE"}, | |
77 {"ta", "ta-IN"}, | |
78 {"tg", "tg-TG"}, | |
79 {"tr", "tr-TR"}, | |
80 {"uk", "uk-UA"}, | |
81 {"vi", "vi-VN"}, | |
82 }; | |
83 | |
84 bool IsValidRegion(const std::string& region) { | |
85 for (size_t i = 0; i < arraysize(g_supported_spellchecker_languages); | |
86 ++i) { | |
87 if (g_supported_spellchecker_languages[i].language_region == region) | |
88 return true; | |
89 } | |
90 return false; | |
91 } | |
92 | |
93 // This function returns the language-region version of language name. | |
94 // e.g. returns hi-IN for hi. | |
95 std::string GetSpellCheckLanguageRegion(const std::string& input_language) { | |
96 for (size_t i = 0; i < arraysize(g_supported_spellchecker_languages); | |
97 ++i) { | |
98 if (g_supported_spellchecker_languages[i].language == input_language) { | |
99 return std::string( | |
100 g_supported_spellchecker_languages[i].language_region); | |
101 } | |
102 } | |
103 | |
104 return input_language; | |
105 } | |
106 | |
107 base::FilePath GetVersionedFileName(const std::string& input_language, | |
108 const base::FilePath& dict_dir) { | |
109 // The default dictionary version is 3-0. This version indicates that the bdic | |
110 // file contains a checksum. | |
111 static const char kDefaultVersionString[] = "-3-0"; | |
112 | |
113 // Add non-default version strings here. Use the same version for all the | |
114 // dictionaries that you add at the same time. Increment the major version | |
115 // number if you're updating either dic or aff files. Increment the minor | |
116 // version number if you're updating only dic_delta files. | |
117 static LanguageVersion special_version_string[] = { | |
118 {"tr-TR", "-4-0"}, // Jan 9, 2013: Add "FLAG num" to aff to avoid heapcheck | |
119 // crash. | |
120 {"tg-TG", "-5-0"}, // Mar 4, 2014: Add Tajik dictionary. | |
121 | |
122 // April 2016: Local fixes | |
123 {"en-CA", "-7-1"}, | |
124 {"en-GB", "-7-1"}, | |
125 {"en-US", "-7-1"}, | |
126 | |
127 // March 2016: Initial check-in of Persian | |
128 {"fa-IR", "-7-0"}, | |
129 }; | |
130 | |
131 // Generate the bdict file name using default version string or special | |
132 // version string, depending on the language. | |
133 std::string language = GetSpellCheckLanguageRegion(input_language); | |
134 std::string versioned_bdict_file_name(language + kDefaultVersionString + | |
135 ".bdic"); | |
136 for (size_t i = 0; i < arraysize(special_version_string); ++i) { | |
137 if (language == special_version_string[i].language) { | |
138 versioned_bdict_file_name = | |
139 language + special_version_string[i].version + ".bdic"; | |
140 break; | |
141 } | |
142 } | |
143 | |
144 return dict_dir.AppendASCII(versioned_bdict_file_name); | |
145 } | |
146 | |
147 std::string GetCorrespondingSpellCheckLanguage(const std::string& language) { | |
148 std::string best_match; | |
149 // Look for exact match in the Spell Check language list. | |
150 for (size_t i = 0; i < arraysize(g_supported_spellchecker_languages); | |
151 ++i) { | |
152 // First look for exact match in the language region of the list. | |
153 std::string spellcheck_language( | |
154 g_supported_spellchecker_languages[i].language); | |
155 if (spellcheck_language == language) | |
156 return language; | |
157 | |
158 // Next, look for exact match in the language_region part of the list. | |
159 std::string spellcheck_language_region( | |
160 g_supported_spellchecker_languages[i].language_region); | |
161 if (spellcheck_language_region == language) { | |
162 if (best_match.empty()) | |
163 best_match = g_supported_spellchecker_languages[i].language; | |
164 } | |
165 } | |
166 | |
167 // No match found - return best match, if any. | |
168 return best_match; | |
169 } | |
170 | |
171 void SpellCheckLanguages(std::vector<std::string>* languages) { | |
172 for (size_t i = 0; i < arraysize(g_supported_spellchecker_languages); | |
173 ++i) { | |
174 languages->push_back(g_supported_spellchecker_languages[i].language); | |
175 } | |
176 } | |
177 | |
178 void GetISOLanguageCountryCodeFromLocale(const std::string& locale, | |
179 std::string* language_code, | |
180 std::string* country_code) { | |
181 DCHECK(language_code); | |
182 DCHECK(country_code); | |
183 char language[ULOC_LANG_CAPACITY] = ULOC_ENGLISH; | |
184 const char* country = "USA"; | |
185 if (!locale.empty()) { | |
186 UErrorCode error = U_ZERO_ERROR; | |
187 char id[ULOC_LANG_CAPACITY + ULOC_SCRIPT_CAPACITY + ULOC_COUNTRY_CAPACITY]; | |
188 uloc_addLikelySubtags(locale.c_str(), id, arraysize(id), &error); | |
189 error = U_ZERO_ERROR; | |
190 uloc_getLanguage(id, language, arraysize(language), &error); | |
191 country = uloc_getISO3Country(id); | |
192 } | |
193 *language_code = std::string(language); | |
194 *country_code = std::string(country); | |
195 } | |
196 | |
197 } // namespace spellcheck_common | |
198 } // namespace chrome | |
OLD | NEW |