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