OLD | NEW |
(Empty) | |
| 1 // Copyright 2013 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/browser/translate/translate_language_list.h" |
| 6 |
| 7 #include <set> |
| 8 |
| 9 #include "base/command_line.h" |
| 10 #include "base/json/json_reader.h" |
| 11 #include "base/lazy_instance.h" |
| 12 #include "base/logging.h" |
| 13 #include "base/string_util.h" |
| 14 #include "base/values.h" |
| 15 #include "chrome/browser/browser_process.h" |
| 16 #include "chrome/browser/translate/translate_url_util.h" |
| 17 #include "chrome/common/chrome_switches.h" |
| 18 #include "googleurl/src/gurl.h" |
| 19 #include "net/base/load_flags.h" |
| 20 #include "net/base/url_util.h" |
| 21 #include "net/http/http_status_code.h" |
| 22 #include "net/url_request/url_fetcher.h" |
| 23 #include "net/url_request/url_request_status.h" |
| 24 |
| 25 namespace { |
| 26 |
| 27 // The default list of languages the Google translation server supports. |
| 28 // We use this list until we receive the list that the server exposes. |
| 29 // For information, here is the list of languages that Chrome can be run in |
| 30 // but that the translation server does not support: |
| 31 // am Amharic |
| 32 // bn Bengali |
| 33 // gu Gujarati |
| 34 // kn Kannada |
| 35 // ml Malayalam |
| 36 // mr Marathi |
| 37 // ta Tamil |
| 38 // te Telugu |
| 39 const char* const kDefaultSupportedLanguages[] = { |
| 40 "af", // Afrikaans |
| 41 "sq", // Albanian |
| 42 "ar", // Arabic |
| 43 "be", // Belarusian |
| 44 "bg", // Bulgarian |
| 45 "ca", // Catalan |
| 46 "zh-CN", // Chinese (Simplified) |
| 47 "zh-TW", // Chinese (Traditional) |
| 48 "hr", // Croatian |
| 49 "cs", // Czech |
| 50 "da", // Danish |
| 51 "nl", // Dutch |
| 52 "en", // English |
| 53 "eo", // Esperanto |
| 54 "et", // Estonian |
| 55 "tl", // Filipino |
| 56 "fi", // Finnish |
| 57 "fr", // French |
| 58 "gl", // Galician |
| 59 "de", // German |
| 60 "el", // Greek |
| 61 "ht", // Haitian Creole |
| 62 "iw", // Hebrew |
| 63 "hi", // Hindi |
| 64 "hu", // Hungarian |
| 65 "is", // Icelandic |
| 66 "id", // Indonesian |
| 67 "ga", // Irish |
| 68 "it", // Italian |
| 69 "ja", // Japanese |
| 70 "ko", // Korean |
| 71 "lv", // Latvian |
| 72 "lt", // Lithuanian |
| 73 "mk", // Macedonian |
| 74 "ms", // Malay |
| 75 "mt", // Maltese |
| 76 "no", // Norwegian |
| 77 "fa", // Persian |
| 78 "pl", // Polish |
| 79 "pt", // Portuguese |
| 80 "ro", // Romanian |
| 81 "ru", // Russian |
| 82 "sr", // Serbian |
| 83 "sk", // Slovak |
| 84 "sl", // Slovenian |
| 85 "es", // Spanish |
| 86 "sw", // Swahili |
| 87 "sv", // Swedish |
| 88 "th", // Thai |
| 89 "tr", // Turkish |
| 90 "uk", // Ukrainian |
| 91 "vi", // Vietnamese |
| 92 "cy", // Welsh |
| 93 "yi", // Yiddish |
| 94 }; |
| 95 |
| 96 // Constant URL string to fetch server supporting language list. |
| 97 const char kLanguageListFetchURL[] = |
| 98 "https://translate.googleapis.com/translate_a/l?client=chrome&cb=sl"; |
| 99 |
| 100 // Used in kTranslateScriptURL to request supporting languages list including |
| 101 // "alpha languages". |
| 102 const char kAlphaLanguageQueryName[] = "alpha"; |
| 103 const char kAlphaLanguageQueryValue[] = "1"; |
| 104 |
| 105 // Retry parameter for fetching supporting language list. |
| 106 const int kMaxRetryLanguageListFetch = 5; |
| 107 |
| 108 } // namespace |
| 109 |
| 110 // This must be kept in sync with the &cb= value in the kLanguageListFetchURL. |
| 111 const char TranslateLanguageList::kLanguageListCallbackName[] = "sl("; |
| 112 const char TranslateLanguageList::kTargetLanguagesKey[] = "tl"; |
| 113 |
| 114 TranslateLanguageList::TranslateLanguageList() { |
| 115 // We default to our hard coded list of languages in |
| 116 // |kDefaultSupportedLanguages|. This list will be overriden by a server |
| 117 // providing supported langauges list. |
| 118 for (size_t i = 0; i < arraysize(kDefaultSupportedLanguages); ++i) |
| 119 supported_languages_.insert(kDefaultSupportedLanguages[i]); |
| 120 } |
| 121 |
| 122 TranslateLanguageList::~TranslateLanguageList() {} |
| 123 |
| 124 void TranslateLanguageList::OnURLFetchComplete(const net::URLFetcher* source) { |
| 125 DCHECK(url_fetcher_.get() == source); |
| 126 scoped_ptr<const net::URLFetcher> delete_ptr(url_fetcher_.release()); |
| 127 |
| 128 if (source->GetStatus().status() == net::URLRequestStatus::SUCCESS && |
| 129 source->GetResponseCode() == net::HTTP_OK) { |
| 130 std::string data; |
| 131 source->GetResponseAsString(&data); |
| 132 SetSupportedLanguages(data); |
| 133 } else { |
| 134 // TODO(toyoshim): Try again. http://crbug.com/244202 . |
| 135 VLOG(9) << "Failed to Fetch languages from: " << kLanguageListFetchURL; |
| 136 } |
| 137 } |
| 138 |
| 139 void TranslateLanguageList::GetSupportedLanguages( |
| 140 std::vector<std::string>* languages) { |
| 141 DCHECK(languages && languages->empty()); |
| 142 std::set<std::string>::const_iterator iter = supported_languages_.begin(); |
| 143 for (; iter != supported_languages_.end(); ++iter) |
| 144 languages->push_back(*iter); |
| 145 } |
| 146 |
| 147 std::string TranslateLanguageList::GetLanguageCode( |
| 148 const std::string& chrome_locale) { |
| 149 // Only remove the country code for country specific languages we don't |
| 150 // support specifically yet. |
| 151 if (IsSupportedLanguage(chrome_locale)) |
| 152 return chrome_locale; |
| 153 |
| 154 size_t hypen_index = chrome_locale.find('-'); |
| 155 if (hypen_index == std::string::npos) |
| 156 return chrome_locale; |
| 157 return chrome_locale.substr(0, hypen_index); |
| 158 } |
| 159 |
| 160 bool TranslateLanguageList::IsSupportedLanguage( |
| 161 const std::string& page_language) { |
| 162 return supported_languages_.count(page_language) != 0; |
| 163 } |
| 164 |
| 165 void TranslateLanguageList::RequestLanguageList() { |
| 166 if (url_fetcher_.get()) |
| 167 return; |
| 168 |
| 169 GURL language_list_fetch_url = GURL(kLanguageListFetchURL); |
| 170 language_list_fetch_url = |
| 171 TranslateURLUtil::AddHostLocaleToUrl(language_list_fetch_url); |
| 172 language_list_fetch_url = |
| 173 TranslateURLUtil::AddApiKeyToUrl(language_list_fetch_url); |
| 174 |
| 175 const CommandLine& command_line = *CommandLine::ForCurrentProcess(); |
| 176 if (command_line.HasSwitch(switches::kEnableTranslateAlphaLanguages)) { |
| 177 language_list_fetch_url = net::AppendQueryParameter( |
| 178 language_list_fetch_url, |
| 179 kAlphaLanguageQueryName, |
| 180 kAlphaLanguageQueryValue); |
| 181 } |
| 182 |
| 183 VLOG(9) << "Fetch supporting language list from: " |
| 184 << language_list_fetch_url.spec().c_str(); |
| 185 |
| 186 url_fetcher_.reset(net::URLFetcher::Create(1, |
| 187 language_list_fetch_url, |
| 188 net::URLFetcher::GET, |
| 189 this)); |
| 190 url_fetcher_->SetLoadFlags(net::LOAD_DO_NOT_SEND_COOKIES | |
| 191 net::LOAD_DO_NOT_SAVE_COOKIES); |
| 192 url_fetcher_->SetRequestContext(g_browser_process->system_request_context()); |
| 193 url_fetcher_->SetMaxRetriesOn5xx(kMaxRetryLanguageListFetch); |
| 194 url_fetcher_->Start(); |
| 195 } |
| 196 |
| 197 void TranslateLanguageList::SetSupportedLanguages( |
| 198 const std::string& language_list) { |
| 199 // The format is: |
| 200 // sl({"sl": {"XX": "LanguageName", ...}, "tl": {"XX": "LanguageName", ...}}) |
| 201 // Where "sl(" is set in kLanguageListCallbackName |
| 202 // and "tl" is kTargetLanguagesKey |
| 203 if (!StartsWithASCII(language_list, |
| 204 TranslateLanguageList::kLanguageListCallbackName, |
| 205 false) || |
| 206 !EndsWith(language_list, ")", false)) { |
| 207 // We don't have a NOTREACHED here since this can happen in ui_tests, even |
| 208 // though the the BrowserMain function won't call us with parameters.ui_task |
| 209 // is NULL some tests don't set it, so we must bail here. |
| 210 return; |
| 211 } |
| 212 static const size_t kLanguageListCallbackNameLength = |
| 213 strlen(TranslateLanguageList::kLanguageListCallbackName); |
| 214 std::string languages_json = language_list.substr( |
| 215 kLanguageListCallbackNameLength, |
| 216 language_list.size() - kLanguageListCallbackNameLength - 1); |
| 217 scoped_ptr<Value> json_value( |
| 218 base::JSONReader::Read(languages_json, base::JSON_ALLOW_TRAILING_COMMAS)); |
| 219 if (json_value == NULL || !json_value->IsType(Value::TYPE_DICTIONARY)) { |
| 220 NOTREACHED(); |
| 221 return; |
| 222 } |
| 223 // The first level dictionary contains two sub-dict, one for source languages |
| 224 // and the other for target languages, we want to use the target languages. |
| 225 DictionaryValue* language_dict = |
| 226 static_cast<DictionaryValue*>(json_value.get()); |
| 227 DictionaryValue* target_languages = NULL; |
| 228 if (!language_dict->GetDictionary(TranslateLanguageList::kTargetLanguagesKey, |
| 229 &target_languages) || |
| 230 target_languages == NULL) { |
| 231 NOTREACHED(); |
| 232 return; |
| 233 } |
| 234 // Now we can clear our current state... |
| 235 supported_languages_.clear(); |
| 236 // ... and replace it with the values we just fetched from the server. |
| 237 for (DictionaryValue::Iterator iter(*target_languages); |
| 238 !iter.IsAtEnd(); |
| 239 iter.Advance()) { |
| 240 supported_languages_.insert(iter.key()); |
| 241 } |
| 242 } |
OLD | NEW |