language_options_handler_common.cc revision 5821806d5e7f356e8fa4b058a389a808ea183019
1// Copyright (c) 2012 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/options/language_options_handler_common.h"
6
7#include <map>
8#include <string>
9#include <utility>
10#include <vector>
11
12#include "base/basictypes.h"
13#include "base/bind.h"
14#include "base/command_line.h"
15#include "base/stringprintf.h"
16#include "base/utf_string_conversions.h"
17#include "base/values.h"
18#include "chrome/browser/browser_process.h"
19#include "chrome/browser/prefs/pref_service.h"
20#include "chrome/browser/ui/browser_list.h"
21#include "chrome/common/chrome_switches.h"
22#include "chrome/common/pref_names.h"
23#include "chrome/common/spellcheck_common.h"
24#include "content/public/browser/user_metrics.h"
25#include "content/public/browser/web_ui.h"
26#include "grit/chromium_strings.h"
27#include "grit/generated_resources.h"
28#include "ui/base/l10n/l10n_util.h"
29
30using content::UserMetricsAction;
31
32namespace options {
33
34LanguageOptionsHandlerCommon::LanguageOptionsHandlerCommon() {
35}
36
37LanguageOptionsHandlerCommon::~LanguageOptionsHandlerCommon() {
38}
39
40void LanguageOptionsHandlerCommon::GetLocalizedValues(
41    DictionaryValue* localized_strings) {
42  DCHECK(localized_strings);
43  string16 product_name = GetProductName();
44  localized_strings->SetString("add_button",
45      l10n_util::GetStringUTF16(IDS_OPTIONS_SETTINGS_LANGUAGES_ADD_BUTTON));
46  localized_strings->SetString("languages",
47      l10n_util::GetStringUTF16(IDS_OPTIONS_SETTINGS_LANGUAGES_LANGUAGES));
48  localized_strings->SetString("add_language_instructions",
49      l10n_util::GetStringUTF16(
50          IDS_OPTIONS_SETTINGS_LANGUAGES_ADD_LANGUAGE_INSTRUCTIONS));
51  localized_strings->SetString("cannot_be_displayed_in_this_language",
52      l10n_util::GetStringFUTF16(
53          IDS_OPTIONS_SETTINGS_LANGUAGES_CANNOT_BE_DISPLAYED_IN_THIS_LANGUAGE,
54          product_name));
55  localized_strings->SetString("is_displayed_in_this_language",
56      l10n_util::GetStringFUTF16(
57          IDS_OPTIONS_SETTINGS_LANGUAGES_IS_DISPLAYED_IN_THIS_LANGUAGE,
58          product_name));
59  localized_strings->SetString("display_in_this_language",
60      l10n_util::GetStringFUTF16(
61          IDS_OPTIONS_SETTINGS_LANGUAGES_DISPLAY_IN_THIS_LANGUAGE,
62          product_name));
63  localized_strings->SetString("restart_required",
64          l10n_util::GetStringUTF16(IDS_OPTIONS_RELAUNCH_REQUIRED));
65  // OS X uses the OS native spellchecker so no need for these strings.
66#if !defined(OS_MACOSX)
67  localized_strings->SetString("use_this_for_spell_checking",
68      l10n_util::GetStringUTF16(
69          IDS_OPTIONS_SETTINGS_USE_THIS_FOR_SPELL_CHECKING));
70  localized_strings->SetString("cannot_be_used_for_spell_checking",
71      l10n_util::GetStringUTF16(
72          IDS_OPTIONS_SETTINGS_CANNOT_BE_USED_FOR_SPELL_CHECKING));
73  localized_strings->SetString("is_used_for_spell_checking",
74      l10n_util::GetStringUTF16(
75          IDS_OPTIONS_SETTINGS_IS_USED_FOR_SPELL_CHECKING));
76  localized_strings->SetString("enable_spell_check",
77          l10n_util::GetStringUTF16(IDS_OPTIONS_ENABLE_SPELLCHECK));
78  localized_strings->SetString("enable_auto_spell_correction",
79          l10n_util::GetStringUTF16(IDS_OPTIONS_ENABLE_AUTO_SPELL_CORRECTION));
80#endif  // !OS_MACOSX
81  localized_strings->SetString("add_language_title",
82          l10n_util::GetStringUTF16(IDS_OPTIONS_LANGUAGES_ADD_TITLE));
83  localized_strings->SetString("add_language_select_label",
84          l10n_util::GetStringUTF16(IDS_OPTIONS_LANGUAGES_ADD_SELECT_LABEL));
85  localized_strings->SetString("restart_button",
86      l10n_util::GetStringUTF16(
87          IDS_OPTIONS_SETTINGS_LANGUAGES_RELAUNCH_BUTTON));
88
89  // The following are resources, rather than local strings.
90  std::string application_locale = g_browser_process->GetApplicationLocale();
91  localized_strings->SetString("currentUiLanguageCode", application_locale);
92  std::string prospective_locale =
93      g_browser_process->local_state()->GetString(prefs::kApplicationLocale);
94  localized_strings->SetString("prospectiveUiLanguageCode",
95      !prospective_locale.empty() ? prospective_locale : application_locale);
96  localized_strings->Set("spellCheckLanguageCodeSet",
97                         GetSpellCheckLanguageCodeSet());
98  localized_strings->Set("uiLanguageCodeSet", GetUILanguageCodeSet());
99
100  const CommandLine& command_line = *CommandLine::ForCurrentProcess();
101  bool enable_spelling_auto_correct =
102      command_line.HasSwitch(switches::kEnableSpellingAutoCorrect);
103  localized_strings->SetBoolean("enableSpellingAutoCorrect",
104      enable_spelling_auto_correct);
105}
106
107void LanguageOptionsHandlerCommon::RegisterMessages() {
108  web_ui()->RegisterMessageCallback("languageOptionsOpen",
109      base::Bind(
110          &LanguageOptionsHandlerCommon::LanguageOptionsOpenCallback,
111          base::Unretained(this)));
112  web_ui()->RegisterMessageCallback("spellCheckLanguageChange",
113      base::Bind(
114          &LanguageOptionsHandlerCommon::SpellCheckLanguageChangeCallback,
115          base::Unretained(this)));
116  web_ui()->RegisterMessageCallback("uiLanguageChange",
117      base::Bind(
118          &LanguageOptionsHandlerCommon::UiLanguageChangeCallback,
119          base::Unretained(this)));
120}
121
122DictionaryValue* LanguageOptionsHandlerCommon::GetUILanguageCodeSet() {
123  DictionaryValue* dictionary = new DictionaryValue();
124  const std::vector<std::string>& available_locales =
125      l10n_util::GetAvailableLocales();
126  for (size_t i = 0; i < available_locales.size(); ++i) {
127    dictionary->SetBoolean(available_locales[i], true);
128  }
129  return dictionary;
130}
131
132DictionaryValue* LanguageOptionsHandlerCommon::GetSpellCheckLanguageCodeSet() {
133  DictionaryValue* dictionary = new DictionaryValue();
134  std::vector<std::string> spell_check_languages;
135  chrome::spellcheck_common::SpellCheckLanguages(&spell_check_languages);
136  for (size_t i = 0; i < spell_check_languages.size(); ++i) {
137    dictionary->SetBoolean(spell_check_languages[i], true);
138  }
139  return dictionary;
140}
141
142void LanguageOptionsHandlerCommon::LanguageOptionsOpenCallback(
143    const ListValue* args) {
144  content::RecordAction(UserMetricsAction("LanguageOptions_Open"));
145}
146
147void LanguageOptionsHandlerCommon::UiLanguageChangeCallback(
148    const ListValue* args) {
149  const std::string language_code = UTF16ToASCII(ExtractStringValue(args));
150  CHECK(!language_code.empty());
151  const std::string action = base::StringPrintf(
152      "LanguageOptions_UiLanguageChange_%s", language_code.c_str());
153  content::RecordComputedAction(action);
154  SetApplicationLocale(language_code);
155  StringValue language_value(language_code);
156  web_ui()->CallJavascriptFunction("options.LanguageOptions.uiLanguageSaved",
157                                   language_value);
158}
159
160void LanguageOptionsHandlerCommon::SpellCheckLanguageChangeCallback(
161    const ListValue* args) {
162  const std::string language_code = UTF16ToASCII(ExtractStringValue(args));
163  CHECK(!language_code.empty());
164  const std::string action = base::StringPrintf(
165      "LanguageOptions_SpellCheckLanguageChange_%s", language_code.c_str());
166  content::RecordComputedAction(action);
167}
168
169}  // namespace options
170