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