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