diff options
author | estade@chromium.org <estade@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2009-11-19 22:44:29 +0000 |
---|---|---|
committer | estade@chromium.org <estade@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2009-11-19 22:44:29 +0000 |
commit | c27324bdb7b9ee448a0c73d5c005c709282c8cbc (patch) | |
tree | d7ec31ab43e6493ef3f00eb81faeca0c79a793c3 /chrome/common/spellcheck_common.cc | |
parent | 5ef3748a651d073bf12928e880a042bf450e1aa8 (diff) | |
download | chromium_src-c27324bdb7b9ee448a0c73d5c005c709282c8cbc.zip chromium_src-c27324bdb7b9ee448a0c73d5c005c709282c8cbc.tar.gz chromium_src-c27324bdb7b9ee448a0c73d5c005c709282c8cbc.tar.bz2 |
Move Mac to using renderer spellchecker.
BUG=25677
Review URL: http://codereview.chromium.org/395007
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@32561 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'chrome/common/spellcheck_common.cc')
-rw-r--r-- | chrome/common/spellcheck_common.cc | 174 |
1 files changed, 174 insertions, 0 deletions
diff --git a/chrome/common/spellcheck_common.cc b/chrome/common/spellcheck_common.cc new file mode 100644 index 0000000..e06d2eb --- /dev/null +++ b/chrome/common/spellcheck_common.cc @@ -0,0 +1,174 @@ +// Copyright (c) 2009 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#include "chrome/common/spellcheck_common.h" + +#include "base/file_path.h" + +namespace SpellCheckCommon { + +static const struct { + // The language. + const char* language; + + // The corresponding language and region, used by the dictionaries. + const char* language_region; +} g_supported_spellchecker_languages[] = { + // Several languages are not to be included in the spellchecker list: + // th-TH, hu-HU, bg-BG, uk-UA + {"ca", "ca-ES"}, + {"cs", "cs-CZ"}, + {"da", "da-DK"}, + {"de", "de-DE"}, + {"el", "el-GR"}, + {"en-AU", "en-AU"}, + {"en-GB", "en-GB"}, + {"en-US", "en-US"}, + {"es", "es-ES"}, + {"et", "et-EE"}, + {"fr", "fr-FR"}, + {"he", "he-IL"}, + {"hi", "hi-IN"}, + {"hr", "hr-HR"}, + {"id", "id-ID"}, + {"it", "it-IT"}, + {"lt", "lt-LT"}, + {"lv", "lv-LV"}, + {"nb", "nb-NO"}, + {"nl", "nl-NL"}, + {"pl", "pl-PL"}, + {"pt-BR", "pt-BR"}, + {"pt-PT", "pt-PT"}, + {"ro", "ro-RO"}, + {"ru", "ru-RU"}, + {"sk", "sk-SK"}, + {"sl", "sl-SI"}, + {"sv", "sv-SE"}, + {"tr", "tr-TR"}, + {"vi", "vi-VN"}, +}; + +// This function returns the language-region version of language name. +// e.g. returns hi-IN for hi. +std::string GetSpellCheckLanguageRegion(const std::string& input_language) { + for (size_t i = 0; i < ARRAYSIZE_UNSAFE(g_supported_spellchecker_languages); + ++i) { + if (g_supported_spellchecker_languages[i].language == input_language) { + return std::string( + g_supported_spellchecker_languages[i].language_region); + } + } + + return input_language; +} + +FilePath GetVersionedFileName(const std::string& input_language, + const FilePath& dict_dir) { + // The default dictionary version is 1-2. These versions have been augmented + // with additional words found by the translation team. + static const char kDefaultVersionString[] = "-1-2"; + + // The following dictionaries have either not been augmented with additional + // words (version 1-1) or have new words, as well as an upgraded dictionary + // as of Feb 2009 (version 1-3). + static const struct { + // The language input. + const char* language; + + // The corresponding version. + const char* version; + } special_version_string[] = { + {"en-AU", "-1-1"}, + {"en-GB", "-1-1"}, + {"es-ES", "-1-1"}, + {"nl-NL", "-1-1"}, + {"ru-RU", "-1-1"}, + {"sv-SE", "-1-1"}, + {"he-IL", "-1-1"}, + {"el-GR", "-1-1"}, + {"hi-IN", "-1-1"}, + {"tr-TR", "-1-1"}, + {"et-EE", "-1-1"}, + {"fr-FR", "-1-4"}, // To fix a crash, fr dictionary was updated to 1.4. + {"lt-LT", "-1-3"}, + {"pl-PL", "-1-3"} + }; + + // Generate the bdict file name using default version string or special + // version string, depending on the language. + std::string language = GetSpellCheckLanguageRegion(input_language); + std::string versioned_bdict_file_name(language + kDefaultVersionString + + ".bdic"); + for (size_t i = 0; i < ARRAYSIZE_UNSAFE(special_version_string); ++i) { + if (language == special_version_string[i].language) { + versioned_bdict_file_name = + language + special_version_string[i].version + ".bdic"; + break; + } + } + + return dict_dir.AppendASCII(versioned_bdict_file_name); +} + +std::string GetCorrespondingSpellCheckLanguage(const std::string& language) { + // Look for exact match in the Spell Check language list. + for (size_t i = 0; i < ARRAYSIZE_UNSAFE(g_supported_spellchecker_languages); + ++i) { + // First look for exact match in the language region of the list. + std::string spellcheck_language( + g_supported_spellchecker_languages[i].language); + if (spellcheck_language == language) + return language; + + // Next, look for exact match in the language_region part of the list. + std::string spellcheck_language_region( + g_supported_spellchecker_languages[i].language_region); + if (spellcheck_language_region == language) + return g_supported_spellchecker_languages[i].language; + } + + // Look for a match by comparing only language parts. All the 'en-RR' + // except for 'en-GB' exactly matched in the above loop, will match + // 'en-US'. This is not ideal because 'en-ZA', 'en-NZ' had + // better be matched with 'en-GB'. This does not handle cases like + // 'az-Latn-AZ' vs 'az-Arab-AZ', either, but we don't use 3-part + // locale ids with a script code in the middle, yet. + // TODO(jungshik): Add a better fallback. + std::string language_part(language, 0, language.find('-')); + for (size_t i = 0; i < ARRAYSIZE_UNSAFE(g_supported_spellchecker_languages); + ++i) { + std::string spellcheck_language( + g_supported_spellchecker_languages[i].language_region); + if (spellcheck_language.substr(0, spellcheck_language.find('-')) == + language_part) { + return spellcheck_language; + } + } + + // No match found - return blank. + return std::string(); +} + + +void SpellCheckLanguages(std::vector<std::string>* languages) { + for (size_t i = 0; i < ARRAYSIZE_UNSAFE(g_supported_spellchecker_languages); + ++i) { + languages->push_back(g_supported_spellchecker_languages[i].language); + } +} + + +std::string GetLanguageFromLanguageRegion(std::string input_language) { + for (size_t i = 0; i < ARRAYSIZE_UNSAFE(g_supported_spellchecker_languages); + ++i) { + std::string language( + g_supported_spellchecker_languages[i].language_region); + if (language == input_language) + return std::string(g_supported_spellchecker_languages[i].language); + } + + return input_language; +} + +} // namespace SpellCheckCommon |