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 "third_party/icu/source/common/unicode/uloc.h"
12 namespace spellcheck_common
{
14 struct LanguageRegion
{
15 const char* language
; // The language.
16 const char* language_region
; // language & region, used by dictionaries.
19 struct LanguageVersion
{
20 const char* language
; // The language input.
21 const char* version
; // The corresponding version.
24 static const LanguageRegion g_supported_spellchecker_languages
[] = {
25 // Several languages are not to be included in the spellchecker list:
71 bool IsValidRegion(const std::string
& region
) {
72 for (size_t i
= 0; i
< arraysize(g_supported_spellchecker_languages
);
74 if (g_supported_spellchecker_languages
[i
].language_region
== region
)
80 // This function returns the language-region version of language name.
81 // e.g. returns hi-IN for hi.
82 std::string
GetSpellCheckLanguageRegion(const std::string
& input_language
) {
83 for (size_t i
= 0; i
< arraysize(g_supported_spellchecker_languages
);
85 if (g_supported_spellchecker_languages
[i
].language
== input_language
) {
87 g_supported_spellchecker_languages
[i
].language_region
);
91 return input_language
;
94 base::FilePath
GetVersionedFileName(const std::string
& input_language
,
95 const base::FilePath
& dict_dir
) {
96 // The default dictionary version is 3-0. This version indicates that the bdic
97 // file contains a checksum.
98 static const char kDefaultVersionString
[] = "-3-0";
100 // Add non-default version strings here. Use the same version for all the
101 // dictionaries that you add at the same time. Increment the major version
102 // number if you're updating either dic or aff files. Increment the minor
103 // version number if you're updating only dic_delta files.
104 static LanguageVersion special_version_string
[] = {
105 {"tr-TR", "-4-0"}, // Jan 9, 2013: Add "FLAG num" to aff to avoid heapcheck
107 {"tg-TG", "-5-0"}, // Mar 4, 2014: Add Tajik dictionary.
109 // Oct 28, 2014: Update from upstream, add new words.
116 // Generate the bdict file name using default version string or special
117 // version string, depending on the language.
118 std::string language
= GetSpellCheckLanguageRegion(input_language
);
119 std::string
versioned_bdict_file_name(language
+ kDefaultVersionString
+
121 for (size_t i
= 0; i
< arraysize(special_version_string
); ++i
) {
122 if (language
== special_version_string
[i
].language
) {
123 versioned_bdict_file_name
=
124 language
+ special_version_string
[i
].version
+ ".bdic";
129 return dict_dir
.AppendASCII(versioned_bdict_file_name
);
132 std::string
GetCorrespondingSpellCheckLanguage(const std::string
& language
) {
133 // Look for exact match in the Spell Check language list.
134 for (size_t i
= 0; i
< arraysize(g_supported_spellchecker_languages
);
136 // First look for exact match in the language region of the list.
137 std::string
spellcheck_language(
138 g_supported_spellchecker_languages
[i
].language
);
139 if (spellcheck_language
== language
)
142 // Next, look for exact match in the language_region part of the list.
143 std::string
spellcheck_language_region(
144 g_supported_spellchecker_languages
[i
].language_region
);
145 if (spellcheck_language_region
== language
)
146 return g_supported_spellchecker_languages
[i
].language
;
149 // No match found - return blank.
150 return std::string();
153 void SpellCheckLanguages(std::vector
<std::string
>* languages
) {
154 for (size_t i
= 0; i
< arraysize(g_supported_spellchecker_languages
);
156 languages
->push_back(g_supported_spellchecker_languages
[i
].language
);
160 void GetISOLanguageCountryCodeFromLocale(const std::string
& locale
,
161 std::string
* language_code
,
162 std::string
* country_code
) {
163 DCHECK(language_code
);
164 DCHECK(country_code
);
165 char language
[ULOC_LANG_CAPACITY
] = ULOC_ENGLISH
;
166 const char* country
= "USA";
167 if (!locale
.empty()) {
168 UErrorCode error
= U_ZERO_ERROR
;
169 char id
[ULOC_LANG_CAPACITY
+ ULOC_SCRIPT_CAPACITY
+ ULOC_COUNTRY_CAPACITY
];
170 uloc_addLikelySubtags(locale
.c_str(), id
, arraysize(id
), &error
);
171 error
= U_ZERO_ERROR
;
172 uloc_getLanguage(id
, language
, arraysize(language
), &error
);
173 country
= uloc_getISO3Country(id
);
175 *language_code
= std::string(language
);
176 *country_code
= std::string(country
);
179 } // namespace spellcheck_common
180 } // namespace chrome