translate_internals_ui.cc revision c5cede9ae108bb15f6b7a8aea21c7e1fefa2834c
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/ui/webui/translate_internals/translate_internals_ui.h" 6 7#include <string> 8#include <vector> 9 10#include "base/command_line.h" 11#include "base/memory/scoped_ptr.h" 12#include "base/values.h" 13#include "chrome/browser/browser_process.h" 14#include "chrome/browser/profiles/profile.h" 15#include "chrome/browser/ui/webui/translate_internals/translate_internals_handler.h" 16#include "chrome/common/chrome_switches.h" 17#include "chrome/common/url_constants.h" 18#include "content/public/browser/web_contents.h" 19#include "content/public/browser/web_ui.h" 20#include "content/public/browser/web_ui_data_source.h" 21#include "grit/browser_resources.h" 22#include "grit/translate_internals_resources.h" 23#include "ui/base/l10n/l10n_util.h" 24#include "ui/base/resource/resource_bundle.h" 25 26namespace { 27 28// Sets the languages to |dict|. Each key is a language code and each value is 29// a language name in the locale. 30void GetLanguages(base::DictionaryValue* dict) { 31 DCHECK(dict); 32 33 const std::string app_locale = g_browser_process->GetApplicationLocale(); 34 std::vector<std::string> language_codes; 35 l10n_util::GetAcceptLanguagesForLocale(app_locale, &language_codes); 36 37 for (std::vector<std::string>::iterator it = language_codes.begin(); 38 it != language_codes.end(); ++it) { 39 const std::string& lang_code = *it; 40 base::string16 lang_name = 41 l10n_util::GetDisplayNameForLocale(lang_code, app_locale, false); 42 dict->SetString(lang_code, lang_name); 43 } 44} 45 46content::WebUIDataSource* CreateTranslateInternalsHTMLSource() { 47 content::WebUIDataSource* source = 48 content::WebUIDataSource::Create(chrome::kChromeUITranslateInternalsHost); 49 50 source->SetUseJsonJSFormatV2(); 51 source->SetDefaultResource(IDR_TRANSLATE_INTERNALS_TRANSLATE_INTERNALS_HTML); 52 source->SetJsonPath("strings.js"); 53 source->AddResourcePath("translate_internals.js", 54 IDR_TRANSLATE_INTERNALS_TRANSLATE_INTERNALS_JS); 55 56 base::DictionaryValue langs; 57 GetLanguages(&langs); 58 for (base::DictionaryValue::Iterator it(langs); !it.IsAtEnd(); it.Advance()) { 59 std::string key = "language-" + it.key(); 60 std::string value; 61 it.value().GetAsString(&value); 62 source->AddString(key, value); 63 } 64 65 std::string cld_version = ""; 66 // The version strings are hardcoded here to avoid linking with the CLD 67 // library, see http://crbug.com/297777. 68#if CLD_VERSION==1 69 cld_version = "1.6"; 70#elif CLD_VERSION==2 71 cld_version = "2"; 72#else 73 NOTREACHED(); 74#endif 75 source->AddString("cld-version", cld_version); 76 77 return source; 78} 79 80} // namespace 81 82TranslateInternalsUI::TranslateInternalsUI(content::WebUI* web_ui) 83 : WebUIController(web_ui) { 84 web_ui->AddMessageHandler(new TranslateInternalsHandler); 85 86 Profile* profile = Profile::FromWebUI(web_ui); 87 content::WebUIDataSource::Add(profile, CreateTranslateInternalsHTMLSource()); 88} 89