Add ICU message format support
[chromium-blink-merge.git] / chrome / browser / ui / webui / translate_internals / translate_internals_ui.cc
blobe9476f75dcecb0584f65e328664fe95973673229
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.
5 #include "chrome/browser/ui/webui/translate_internals/translate_internals_ui.h"
7 #include <string>
8 #include <vector>
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"
25 namespace {
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);
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);
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);
45 content::WebUIDataSource* CreateTranslateInternalsHTMLSource() {
46 content::WebUIDataSource* source =
47 content::WebUIDataSource::Create(chrome::kChromeUITranslateInternalsHost);
49 source->SetDefaultResource(IDR_TRANSLATE_INTERNALS_TRANSLATE_INTERNALS_HTML);
50 source->SetJsonPath("strings.js");
51 source->AddResourcePath("translate_internals.js",
52 IDR_TRANSLATE_INTERNALS_TRANSLATE_INTERNALS_JS);
54 base::DictionaryValue langs;
55 GetLanguages(&langs);
56 for (base::DictionaryValue::Iterator it(langs); !it.IsAtEnd(); it.Advance()) {
57 std::string key = "language-" + it.key();
58 std::string value;
59 it.value().GetAsString(&value);
60 source->AddString(key, value);
63 std::string cld_version = "";
64 std::string cld_data_source = "";
65 // The version strings are hardcoded here to avoid linking with the CLD
66 // library, see http://crbug.com/297777.
67 #if CLD_VERSION==1
68 cld_version = "1.6";
69 cld_data_source = "static"; // CLD1.x does not support dynamic data loading
70 #elif CLD_VERSION==2
71 cld_version = "2";
72 cld_data_source = translate::CldDataSource::Get()->GetName();
73 #else
74 NOTREACHED();
75 #endif
76 source->AddString("cld-version", cld_version);
77 source->AddString("cld-data-source", cld_data_source);
79 return source;
82 } // namespace
84 TranslateInternalsUI::TranslateInternalsUI(content::WebUI* web_ui)
85 : WebUIController(web_ui) {
86 web_ui->AddMessageHandler(new TranslateInternalsHandler);
88 Profile* profile = Profile::FromWebUI(web_ui);
89 content::WebUIDataSource::Add(profile, CreateTranslateInternalsHTMLSource());