• 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.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/i18n/rtl.h"
15 #include "base/utf_string_conversions.h"
16 #include "base/values.h"
17 #include "chrome/browser/browser_process.h"
18 #include "chrome/browser/metrics/user_metrics.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 "grit/chromium_strings.h"
24 #include "grit/generated_resources.h"
25 #include "ui/base/l10n/l10n_util.h"
26 
LanguageOptionsHandler()27 LanguageOptionsHandler::LanguageOptionsHandler() {
28 }
29 
~LanguageOptionsHandler()30 LanguageOptionsHandler::~LanguageOptionsHandler() {
31 }
32 
GetLocalizedValues(DictionaryValue * localized_strings)33 void LanguageOptionsHandler::GetLocalizedValues(
34     DictionaryValue* localized_strings) {
35   LanguageOptionsHandlerCommon::GetLocalizedValues(localized_strings);
36 
37   localized_strings->SetString("restart_button",
38       l10n_util::GetStringUTF16(
39           IDS_OPTIONS_SETTINGS_LANGUAGES_RELAUNCH_BUTTON));
40   localized_strings->Set("languageList", GetLanguageList());
41 }
42 
RegisterMessages()43 void LanguageOptionsHandler::RegisterMessages() {
44   LanguageOptionsHandlerCommon::RegisterMessages();
45 
46   web_ui_->RegisterMessageCallback("uiLanguageRestart",
47       NewCallback(this, &LanguageOptionsHandler::RestartCallback));
48 }
49 
GetLanguageList()50 ListValue* LanguageOptionsHandler::GetLanguageList() {
51   // Collect the language codes from the supported accept-languages.
52   const std::string app_locale = g_browser_process->GetApplicationLocale();
53   std::vector<std::string> language_codes;
54   l10n_util::GetAcceptLanguagesForLocale(app_locale, &language_codes);
55 
56   // Map of display name -> {language code, native_display_name}.
57   // In theory, we should be able to create a map that is sorted by
58   // display names using ICU comparator, but doing it is hard, thus we'll
59   // use an auxiliary vector to achieve the same result.
60   typedef std::pair<std::string, string16> LanguagePair;
61   typedef std::map<string16, LanguagePair> LanguageMap;
62   LanguageMap language_map;
63   // The auxiliary vector mentioned above.
64   std::vector<string16> display_names;
65 
66   // Build the list of display names, and build the language map.
67   for (size_t i = 0; i < language_codes.size(); ++i) {
68     string16 display_name =
69         l10n_util::GetDisplayNameForLocale(language_codes[i], app_locale,
70                                            false);
71     base::i18n::AdjustStringForLocaleDirection(&display_name);
72     string16 native_display_name =
73         l10n_util::GetDisplayNameForLocale(language_codes[i], language_codes[i],
74                                            false);
75     base::i18n::AdjustStringForLocaleDirection(&native_display_name);
76     display_names.push_back(display_name);
77     language_map[display_name] =
78         std::make_pair(language_codes[i], native_display_name);
79   }
80   DCHECK_EQ(display_names.size(), language_map.size());
81 
82   // Sort display names using locale specific sorter.
83   l10n_util::SortStrings16(app_locale, &display_names);
84 
85   // Build the language list from the language map.
86   ListValue* language_list = new ListValue();
87   for (size_t i = 0; i < display_names.size(); ++i) {
88     const LanguagePair& pair = language_map[display_names[i]];
89     DictionaryValue* dictionary = new DictionaryValue();
90     dictionary->SetString("code",  pair.first);
91     dictionary->SetString("displayName", display_names[i]);
92     dictionary->SetString("nativeDisplayName", pair.second);
93     language_list->Append(dictionary);
94   }
95 
96   return language_list;
97 }
98 
GetProductName()99 string16 LanguageOptionsHandler::GetProductName() {
100   return l10n_util::GetStringUTF16(IDS_PRODUCT_NAME);
101 }
102 
SetApplicationLocale(const std::string & language_code)103 void LanguageOptionsHandler::SetApplicationLocale(
104     const std::string& language_code) {
105   PrefService* pref_service = g_browser_process->local_state();
106   pref_service->SetString(prefs::kApplicationLocale, language_code);
107 }
108 
RestartCallback(const ListValue * args)109 void LanguageOptionsHandler::RestartCallback(const ListValue* args) {
110   UserMetrics::RecordAction(UserMetricsAction("LanguageOptions_Restart"));
111 
112   // Set the flag to restore state after the restart.
113   PrefService* pref_service = g_browser_process->local_state();
114   pref_service->SetBoolean(prefs::kRestartLastSessionOnShutdown, true);
115   BrowserList::CloseAllBrowsersAndExit();
116 }
117