• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright (c) 2011 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/command_line.h"
14 #include "base/utf_string_conversions.h"
15 #include "base/values.h"
16 #include "chrome/browser/browser_process.h"
17 #include "chrome/browser/metrics/user_metrics.h"
18 #include "chrome/browser/prefs/pref_service.h"
19 #include "chrome/browser/ui/browser_list.h"
20 #include "chrome/common/chrome_switches.h"
21 #include "chrome/common/pref_names.h"
22 #include "chrome/common/spellcheck_common.h"
23 #include "grit/chromium_strings.h"
24 #include "grit/generated_resources.h"
25 #include "ui/base/l10n/l10n_util.h"
26 
LanguageOptionsHandlerCommon()27 LanguageOptionsHandlerCommon::LanguageOptionsHandlerCommon() {
28 }
29 
~LanguageOptionsHandlerCommon()30 LanguageOptionsHandlerCommon::~LanguageOptionsHandlerCommon() {
31 }
32 
GetLocalizedValues(DictionaryValue * localized_strings)33 void LanguageOptionsHandlerCommon::GetLocalizedValues(
34     DictionaryValue* localized_strings) {
35   DCHECK(localized_strings);
36   string16 product_name = GetProductName();
37   RegisterTitle(localized_strings, "languagePage",
38                 IDS_OPTIONS_SETTINGS_LANGUAGES_DIALOG_TITLE);
39   localized_strings->SetString("add_button",
40       l10n_util::GetStringUTF16(IDS_OPTIONS_SETTINGS_LANGUAGES_ADD_BUTTON));
41   localized_strings->SetString("languages",
42       l10n_util::GetStringUTF16(IDS_OPTIONS_SETTINGS_LANGUAGES_LANGUAGES));
43   localized_strings->SetString("please_add_another_language",
44       l10n_util::GetStringUTF16(
45           IDS_OPTIONS_SETTINGS_LANGUAGES_PLEASE_ADD_ANOTHER_LANGUAGE));
46   localized_strings->SetString("remove_button",
47       l10n_util::GetStringUTF16(IDS_OPTIONS_SETTINGS_LANGUAGES_REMOVE_BUTTON));
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("this_language_is_currently_in_use",
64       l10n_util::GetStringFUTF16(
65           IDS_OPTIONS_SETTINGS_LANGUAGES_THIS_LANGUAGE_IS_CURRENTLY_IN_USE,
66           product_name));
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("restart_required",
77           l10n_util::GetStringUTF16(IDS_OPTIONS_RELAUNCH_REQUIRED));
78   localized_strings->SetString("enable_spell_check",
79           l10n_util::GetStringUTF16(IDS_OPTIONS_ENABLE_SPELLCHECK));
80   localized_strings->SetString("enable_auto_spell_correction",
81           l10n_util::GetStringUTF16(IDS_OPTIONS_ENABLE_AUTO_SPELL_CORRECTION));
82   localized_strings->SetString("add_language_title",
83           l10n_util::GetStringUTF16(IDS_OPTIONS_LANGUAGES_ADD_TITLE));
84   localized_strings->SetString("add_language_select_label",
85           l10n_util::GetStringUTF16(IDS_OPTIONS_LANGUAGES_ADD_SELECT_LABEL));
86   localized_strings->SetString("restart_button",
87       l10n_util::GetStringUTF16(
88           IDS_OPTIONS_SETTINGS_LANGUAGES_RELAUNCH_BUTTON));
89 
90   // The following are resources, rather than local strings.
91   localized_strings->SetString("currentUiLanguageCode",
92                                g_browser_process->GetApplicationLocale());
93   localized_strings->Set("spellCheckLanguageCodeSet",
94                          GetSpellCheckLanguageCodeSet());
95   localized_strings->Set("uiLanguageCodeSet", GetUILanguageCodeSet());
96 
97   const CommandLine& command_line = *CommandLine::ForCurrentProcess();
98   bool experimental_spell_check_features =
99       command_line.HasSwitch(switches::kExperimentalSpellcheckerFeatures);
100   localized_strings->SetBoolean("experimentalSpellCheckFeatures",
101                                 experimental_spell_check_features);
102 }
103 
RegisterMessages()104 void LanguageOptionsHandlerCommon::RegisterMessages() {
105   DCHECK(web_ui_);
106   web_ui_->RegisterMessageCallback("languageOptionsOpen",
107       NewCallback(
108           this,
109           &LanguageOptionsHandlerCommon::LanguageOptionsOpenCallback));
110   web_ui_->RegisterMessageCallback("spellCheckLanguageChange",
111       NewCallback(
112           this,
113           &LanguageOptionsHandlerCommon::SpellCheckLanguageChangeCallback));
114   web_ui_->RegisterMessageCallback("uiLanguageChange",
115       NewCallback(
116           this,
117           &LanguageOptionsHandlerCommon::UiLanguageChangeCallback));
118 }
119 
GetUILanguageCodeSet()120 DictionaryValue* LanguageOptionsHandlerCommon::GetUILanguageCodeSet() {
121   DictionaryValue* dictionary = new DictionaryValue();
122   const std::vector<std::string>& available_locales =
123       l10n_util::GetAvailableLocales();
124   for (size_t i = 0; i < available_locales.size(); ++i) {
125     dictionary->SetBoolean(available_locales[i], true);
126   }
127   return dictionary;
128 }
129 
GetSpellCheckLanguageCodeSet()130 DictionaryValue* LanguageOptionsHandlerCommon::GetSpellCheckLanguageCodeSet() {
131   DictionaryValue* dictionary = new DictionaryValue();
132   std::vector<std::string> spell_check_languages;
133   SpellCheckCommon::SpellCheckLanguages(&spell_check_languages);
134   for (size_t i = 0; i < spell_check_languages.size(); ++i) {
135     dictionary->SetBoolean(spell_check_languages[i], true);
136   }
137   return dictionary;
138 }
139 
LanguageOptionsOpenCallback(const ListValue * args)140 void LanguageOptionsHandlerCommon::LanguageOptionsOpenCallback(
141     const ListValue* args) {
142   UserMetrics::RecordAction(UserMetricsAction("LanguageOptions_Open"));
143 }
144 
UiLanguageChangeCallback(const ListValue * args)145 void LanguageOptionsHandlerCommon::UiLanguageChangeCallback(
146     const ListValue* args) {
147   const std::string language_code = UTF16ToASCII(ExtractStringValue(args));
148   CHECK(!language_code.empty());
149   const std::string action = StringPrintf(
150       "LanguageOptions_UiLanguageChange_%s", language_code.c_str());
151   UserMetrics::RecordComputedAction(action);
152   SetApplicationLocale(language_code);
153     web_ui_->CallJavascriptFunction("options.LanguageOptions.uiLanguageSaved");
154 }
155 
SpellCheckLanguageChangeCallback(const ListValue * args)156 void LanguageOptionsHandlerCommon::SpellCheckLanguageChangeCallback(
157     const ListValue* args) {
158   const std::string language_code = UTF16ToASCII(ExtractStringValue(args));
159   CHECK(!language_code.empty());
160   const std::string action = StringPrintf(
161       "LanguageOptions_SpellCheckLanguageChange_%s", language_code.c_str());
162   UserMetrics::RecordComputedAction(action);
163 }
164