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