| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "chrome/common/spellcheck_common.h" | 5 #include "components/spellcheck/common/spellcheck_common.h" |
| 6 | 6 |
| 7 #include "base/command_line.h" | 7 #include "base/command_line.h" |
| 8 #include "base/files/file_path.h" | 8 #include "base/files/file_path.h" |
| 9 #include "base/logging.h" | 9 #include "base/logging.h" |
| 10 #include "base/macros.h" | 10 #include "base/macros.h" |
| 11 #include "base/metrics/field_trial.h" | 11 #include "base/metrics/field_trial.h" |
| 12 #include "base/strings/string_util.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" | 13 #include "third_party/icu/source/common/unicode/uloc.h" |
| 15 #include "third_party/icu/source/common/unicode/urename.h" | 14 #include "third_party/icu/source/common/unicode/urename.h" |
| 16 #include "third_party/icu/source/common/unicode/utypes.h" | 15 #include "third_party/icu/source/common/unicode/utypes.h" |
| 17 | 16 |
| 18 namespace chrome { | 17 namespace spellcheck { |
| 19 namespace spellcheck_common { | |
| 20 | 18 |
| 21 struct LanguageRegion { | 19 struct LanguageRegion { |
| 22 const char* language; // The language. | 20 const char* language; // The language. |
| 23 const char* language_region; // language & region, used by dictionaries. | 21 const char* language_region; // language & region, used by dictionaries. |
| 24 }; | 22 }; |
| 25 | 23 |
| 26 struct LanguageVersion { | 24 struct LanguageVersion { |
| 27 const char* language; // The language input. | 25 const char* language; // The language input. |
| 28 const char* version; // The corresponding version. | 26 const char* version; // The corresponding version. |
| 29 }; | 27 }; |
| 30 | 28 |
| 31 static const LanguageRegion g_supported_spellchecker_languages[] = { | 29 static const LanguageRegion g_supported_spellchecker_languages[] = { |
| 32 // Several languages are not to be included in the spellchecker list: | 30 // Several languages are not to be included in the spellchecker list: |
| (...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 75 {"sr", "sr"}, | 73 {"sr", "sr"}, |
| 76 {"sv", "sv-SE"}, | 74 {"sv", "sv-SE"}, |
| 77 {"ta", "ta-IN"}, | 75 {"ta", "ta-IN"}, |
| 78 {"tg", "tg-TG"}, | 76 {"tg", "tg-TG"}, |
| 79 {"tr", "tr-TR"}, | 77 {"tr", "tr-TR"}, |
| 80 {"uk", "uk-UA"}, | 78 {"uk", "uk-UA"}, |
| 81 {"vi", "vi-VN"}, | 79 {"vi", "vi-VN"}, |
| 82 }; | 80 }; |
| 83 | 81 |
| 84 bool IsValidRegion(const std::string& region) { | 82 bool IsValidRegion(const std::string& region) { |
| 85 for (size_t i = 0; i < arraysize(g_supported_spellchecker_languages); | 83 for (size_t i = 0; i < arraysize(g_supported_spellchecker_languages); ++i) { |
| 86 ++i) { | |
| 87 if (g_supported_spellchecker_languages[i].language_region == region) | 84 if (g_supported_spellchecker_languages[i].language_region == region) |
| 88 return true; | 85 return true; |
| 89 } | 86 } |
| 90 return false; | 87 return false; |
| 91 } | 88 } |
| 92 | 89 |
| 93 // This function returns the language-region version of language name. | 90 // This function returns the language-region version of language name. |
| 94 // e.g. returns hi-IN for hi. | 91 // e.g. returns hi-IN for hi. |
| 95 std::string GetSpellCheckLanguageRegion(const std::string& input_language) { | 92 std::string GetSpellCheckLanguageRegion(const std::string& input_language) { |
| 96 for (size_t i = 0; i < arraysize(g_supported_spellchecker_languages); | 93 for (size_t i = 0; i < arraysize(g_supported_spellchecker_languages); ++i) { |
| 97 ++i) { | |
| 98 if (g_supported_spellchecker_languages[i].language == input_language) { | 94 if (g_supported_spellchecker_languages[i].language == input_language) { |
| 99 return std::string( | 95 return std::string(g_supported_spellchecker_languages[i].language_region); |
| 100 g_supported_spellchecker_languages[i].language_region); | |
| 101 } | 96 } |
| 102 } | 97 } |
| 103 | 98 |
| 104 return input_language; | 99 return input_language; |
| 105 } | 100 } |
| 106 | 101 |
| 107 base::FilePath GetVersionedFileName(const std::string& input_language, | 102 base::FilePath GetVersionedFileName(const std::string& input_language, |
| 108 const base::FilePath& dict_dir) { | 103 const base::FilePath& dict_dir) { |
| 109 // The default dictionary version is 3-0. This version indicates that the bdic | 104 // The default dictionary version is 3-0. This version indicates that the bdic |
| 110 // file contains a checksum. | 105 // file contains a checksum. |
| (...skipping 29 matching lines...) Expand all Loading... |
| 140 break; | 135 break; |
| 141 } | 136 } |
| 142 } | 137 } |
| 143 | 138 |
| 144 return dict_dir.AppendASCII(versioned_bdict_file_name); | 139 return dict_dir.AppendASCII(versioned_bdict_file_name); |
| 145 } | 140 } |
| 146 | 141 |
| 147 std::string GetCorrespondingSpellCheckLanguage(const std::string& language) { | 142 std::string GetCorrespondingSpellCheckLanguage(const std::string& language) { |
| 148 std::string best_match; | 143 std::string best_match; |
| 149 // Look for exact match in the Spell Check language list. | 144 // Look for exact match in the Spell Check language list. |
| 150 for (size_t i = 0; i < arraysize(g_supported_spellchecker_languages); | 145 for (size_t i = 0; i < arraysize(g_supported_spellchecker_languages); ++i) { |
| 151 ++i) { | |
| 152 // First look for exact match in the language region of the list. | 146 // First look for exact match in the language region of the list. |
| 153 std::string spellcheck_language( | 147 std::string spellcheck_language( |
| 154 g_supported_spellchecker_languages[i].language); | 148 g_supported_spellchecker_languages[i].language); |
| 155 if (spellcheck_language == language) | 149 if (spellcheck_language == language) |
| 156 return language; | 150 return language; |
| 157 | 151 |
| 158 // Next, look for exact match in the language_region part of the list. | 152 // Next, look for exact match in the language_region part of the list. |
| 159 std::string spellcheck_language_region( | 153 std::string spellcheck_language_region( |
| 160 g_supported_spellchecker_languages[i].language_region); | 154 g_supported_spellchecker_languages[i].language_region); |
| 161 if (spellcheck_language_region == language) { | 155 if (spellcheck_language_region == language) { |
| 162 if (best_match.empty()) | 156 if (best_match.empty()) |
| 163 best_match = g_supported_spellchecker_languages[i].language; | 157 best_match = g_supported_spellchecker_languages[i].language; |
| 164 } | 158 } |
| 165 } | 159 } |
| 166 | 160 |
| 167 // No match found - return best match, if any. | 161 // No match found - return best match, if any. |
| 168 return best_match; | 162 return best_match; |
| 169 } | 163 } |
| 170 | 164 |
| 171 void SpellCheckLanguages(std::vector<std::string>* languages) { | 165 void SpellCheckLanguages(std::vector<std::string>* languages) { |
| 172 for (size_t i = 0; i < arraysize(g_supported_spellchecker_languages); | 166 for (size_t i = 0; i < arraysize(g_supported_spellchecker_languages); ++i) { |
| 173 ++i) { | |
| 174 languages->push_back(g_supported_spellchecker_languages[i].language); | 167 languages->push_back(g_supported_spellchecker_languages[i].language); |
| 175 } | 168 } |
| 176 } | 169 } |
| 177 | 170 |
| 178 void GetISOLanguageCountryCodeFromLocale(const std::string& locale, | 171 void GetISOLanguageCountryCodeFromLocale(const std::string& locale, |
| 179 std::string* language_code, | 172 std::string* language_code, |
| 180 std::string* country_code) { | 173 std::string* country_code) { |
| 181 DCHECK(language_code); | 174 DCHECK(language_code); |
| 182 DCHECK(country_code); | 175 DCHECK(country_code); |
| 183 char language[ULOC_LANG_CAPACITY] = ULOC_ENGLISH; | 176 char language[ULOC_LANG_CAPACITY] = ULOC_ENGLISH; |
| 184 const char* country = "USA"; | 177 const char* country = "USA"; |
| 185 if (!locale.empty()) { | 178 if (!locale.empty()) { |
| 186 UErrorCode error = U_ZERO_ERROR; | 179 UErrorCode error = U_ZERO_ERROR; |
| 187 char id[ULOC_LANG_CAPACITY + ULOC_SCRIPT_CAPACITY + ULOC_COUNTRY_CAPACITY]; | 180 char id[ULOC_LANG_CAPACITY + ULOC_SCRIPT_CAPACITY + ULOC_COUNTRY_CAPACITY]; |
| 188 uloc_addLikelySubtags(locale.c_str(), id, arraysize(id), &error); | 181 uloc_addLikelySubtags(locale.c_str(), id, arraysize(id), &error); |
| 189 error = U_ZERO_ERROR; | 182 error = U_ZERO_ERROR; |
| 190 uloc_getLanguage(id, language, arraysize(language), &error); | 183 uloc_getLanguage(id, language, arraysize(language), &error); |
| 191 country = uloc_getISO3Country(id); | 184 country = uloc_getISO3Country(id); |
| 192 } | 185 } |
| 193 *language_code = std::string(language); | 186 *language_code = std::string(language); |
| 194 *country_code = std::string(country); | 187 *country_code = std::string(country); |
| 195 } | 188 } |
| 196 | 189 |
| 197 } // namespace spellcheck_common | 190 } // namespace spellcheck |
| 198 } // namespace chrome | |
| OLD | NEW |