Home | History | Annotate | Download | only in translate_internals
      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 "components/translate/content/common/cld_data_source.h"
     19 #include "content/public/browser/web_contents.h"
     20 #include "content/public/browser/web_ui.h"
     21 #include "content/public/browser/web_ui_data_source.h"
     22 #include "grit/translate_internals_resources.h"
     23 #include "ui/base/l10n/l10n_util.h"
     24 
     25 namespace {
     26 
     27 // Sets the languages to |dict|. Each key is a language code and each value is
     28 // a language name in the locale.
     29 void GetLanguages(base::DictionaryValue* dict) {
     30   DCHECK(dict);
     31 
     32   const std::string app_locale = g_browser_process->GetApplicationLocale();
     33   std::vector<std::string> language_codes;
     34   l10n_util::GetAcceptLanguagesForLocale(app_locale, &language_codes);
     35 
     36   for (std::vector<std::string>::iterator it = language_codes.begin();
     37        it != language_codes.end(); ++it) {
     38     const std::string& lang_code = *it;
     39     base::string16 lang_name =
     40         l10n_util::GetDisplayNameForLocale(lang_code, app_locale, false);
     41     dict->SetString(lang_code, lang_name);
     42   }
     43 }
     44 
     45 content::WebUIDataSource* CreateTranslateInternalsHTMLSource() {
     46   content::WebUIDataSource* source =
     47       content::WebUIDataSource::Create(chrome::kChromeUITranslateInternalsHost);
     48 
     49   source->SetUseJsonJSFormatV2();
     50   source->SetDefaultResource(IDR_TRANSLATE_INTERNALS_TRANSLATE_INTERNALS_HTML);
     51   source->SetJsonPath("strings.js");
     52   source->AddResourcePath("translate_internals.js",
     53                           IDR_TRANSLATE_INTERNALS_TRANSLATE_INTERNALS_JS);
     54 
     55   base::DictionaryValue langs;
     56   GetLanguages(&langs);
     57   for (base::DictionaryValue::Iterator it(langs); !it.IsAtEnd(); it.Advance()) {
     58     std::string key = "language-" + it.key();
     59     std::string value;
     60     it.value().GetAsString(&value);
     61     source->AddString(key, value);
     62   }
     63 
     64   std::string cld_version = "";
     65   std::string cld_data_source = "";
     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   cld_data_source = "static"; // CLD1.x does not support dynamic data loading
     71 #elif CLD_VERSION==2
     72   cld_version = "2";
     73   cld_data_source = translate::CldDataSource::GetName();
     74 #else
     75   NOTREACHED();
     76 #endif
     77   source->AddString("cld-version", cld_version);
     78   source->AddString("cld-data-source", cld_data_source);
     79 
     80   return source;
     81 }
     82 
     83 }  // namespace
     84 
     85 TranslateInternalsUI::TranslateInternalsUI(content::WebUI* web_ui)
     86     : WebUIController(web_ui) {
     87   web_ui->AddMessageHandler(new TranslateInternalsHandler);
     88 
     89   Profile* profile = Profile::FromWebUI(web_ui);
     90   content::WebUIDataSource::Add(profile, CreateTranslateInternalsHTMLSource());
     91 }
     92